## misterhouse-users

 [mh] what am I doing wrong? From: - 2008-05-14 20:44:31 ```I've been working on addind a motion sensor to my house. Nothing fancy compared to what a lot of others are doing.. I can't figure out what's wrong with my code. What I want to do is only have the garage light turned on when it's dark enough in my garage to actually need the light. That's roughly between sunrise and sunset. It works if I don't try do the time comparison, so there must be something I don't have coded right. Anyone have any suggestions? ---BEGIN INCLUDE--- # 03/29/08 # Code to detect motion from the MR14A on the kitchen door. This code # was # copied from the example code on the Wiki # http://misterhouse.wikispaces.com/MotionDetection # my \$Door_Motion_timer = new Timer; my \$Door_Motion_state; if (\$state = state_now \$Door_Motion) { set \$Door_Motion_timer 120; if (\$Door_Motion_state ne \$state) { if (\$state eq 'on') { speak "Someone just opened the Kitchen Door."; if (time_greater_than "\$Time_Sunset - 0:15" or time_less_than "\$Time_Sunrise - 0:15"){ set \$Garage_Light 'on'; } else { speak "The Kitchen door is closed."; set \$Garage_Light 'off'; set \$Door_Motion_timer 0; } \$Door_Motion_state = \$state; } } } --- END INCLUDE--- -- -Monte ```
 Re: [mh] what am I doing wrong? From: Andy - 2008-05-14 23:26:50 ```Hi, I think the problem is in the statement below. The time_greater_than() function cannot handle clock based mathematics. if (time_greater_than "\$Time_Sunset - 0:15" or time_less_than "\$Time_Sunrise - 0:15") So try using the time_add() function to first do the mathematics. if (time_greater_than(time_add "\$Time_Sunset - 0:15") or time_less_than(time_add "\$Time_Sunrise - 0:15")) Let us know how you get on. Andy. montef@... wrote: > I've been working on addind a motion sensor to my house. Nothing fancy > compared to what a lot of others are doing.. > > I can't figure out what's wrong with my code. > > What I want to do is only have the garage light turned on when it's dark > enough in my garage to actually need the light. That's roughly between > sunrise and sunset. > > It works if I don't try do the time comparison, so there must be > something I don't have coded right. > > Anyone have any suggestions? > > ---BEGIN INCLUDE--- > > # 03/29/08 > # Code to detect motion from the MR14A on the kitchen door. This code > # was > # copied from the example code on the Wiki > # http://misterhouse.wikispaces.com/MotionDetection > # > my \$Door_Motion_timer = new Timer; > my \$Door_Motion_state; > > if (\$state = state_now \$Door_Motion) { > set \$Door_Motion_timer 120; > > if (\$Door_Motion_state ne \$state) { > if (\$state eq 'on') { > speak "Someone just opened the Kitchen Door."; > > if (time_greater_than "\$Time_Sunset - 0:15" or > time_less_than "\$Time_Sunrise - 0:15"){ > set \$Garage_Light 'on'; > } else { > speak "The Kitchen door is closed."; > set \$Garage_Light 'off'; > set \$Door_Motion_timer 0; > } > \$Door_Motion_state = \$state; > } > } > } > > --- END INCLUDE--- > > ```
 Re: [mh] what am I doing wrong? From: - 2008-05-15 00:51:36 ```Hi Andy, Thanks for the reply/suggestion! > Hi, > > I think the problem is in the statement below. The time_greater_than() > function cannot handle clock based mathematics. > > if (time_greater_than "\$Time_Sunset - 0:15" or > time_less_than "\$Time_Sunrise - 0:15") > > So try using the time_add() function to first do the mathematics. > > if (time_greater_than(time_add "\$Time_Sunset - 0:15") or > time_less_than(time_add "\$Time_Sunrise - 0:15")) The good thing about Perl is that there are 1001 ways to do something. The bad thing about Perl is that there are 1001 ways to do something. :) I'd not found 'time_add' before. Good one to know about. > Let us know how you get on. Sadly, that method doesn't turn my garage light on either. The code loads, meaning that Perl/mh doesn't see anything wrong with the syntax; but apparently my logic is flawed. if (time_greater_than(time_add "\$Time_Sunset - 0:15") or time_less_than(time_add "\$Time_Sunrise - 0:15")) { set \$Garage_Light 'on'; } > Andy. > -- -Monte ```
 Re: [mh] what am I doing wrong? From: Dave Stenhouse - 2008-05-15 10:44:32 ```montef@... wrote: > Hi Andy, > > Thanks for the reply/suggestion! > > >> Hi, >> >> I think the problem is in the statement below. The time_greater_than() >> function cannot handle clock based mathematics. >> >> if (time_greater_than "\$Time_Sunset - 0:15" or >> time_less_than "\$Time_Sunrise - 0:15") >> >> So try using the time_add() function to first do the mathematics. >> >> if (time_greater_than(time_add "\$Time_Sunset - 0:15") or >> time_less_than(time_add "\$Time_Sunrise - 0:15")) >> > > The good thing about Perl is that there are 1001 ways to do something. > The bad thing about Perl is that there are 1001 ways to do something. > :) > > I'd not found 'time_add' before. Good one to know about. > > >> Let us know how you get on. >> > > Sadly, that method doesn't turn my garage light on either. > > The code loads, meaning that Perl/mh doesn't see anything wrong with > the syntax; but apparently my logic is flawed. > > if (time_greater_than(time_add "\$Time_Sunset - 0:15") or > time_less_than(time_add "\$Time_Sunrise - 0:15")) > { > set \$Garage_Light 'on'; > } > > > >> Andy. >> >> > > How about using time_between instead? if (time_between \$Time_Sunset and \$Time_Sunrise) { set \$Garage_Light 'on'; } Then play with the offsets. I don't have any code that does exactly what you're trying to do here, but I have some that has "\$Time_Sunset - 0:30" and that piece works well, and I have a lot of "time_between" in my private code. Good Luck! -Dave ```
 Re: [mh] what am I doing wrong? From: Gregg Liming - 2008-05-15 02:22:29 ```Andy wrote: > I think the problem is in the statement below. The time_greater_than() > function cannot handle clock based mathematics. I'm pretty sure that's not quite right. Anecdotally, I do it all the time--albeit not complex math--just offset. You might want to look at bin/mh and specifically the my_str2time function to refresh thinking. > if (time_greater_than "\$Time_Sunset - 0:15" or > time_less_than "\$Time_Sunrise - 0:15") I've been staring at this a while and wondering why my "almost equivalent" works. The difference for me is that I flip the logic around to surround the time between sunrise and sunset vice sunset and sunrise. (Maybe I'm more of a day person than a night person) For example: if ((time_greater_than "\$Time_Sunrise - 0:15") && (time_less_than "\$Time_Sunset - 0:15")) { # do whatever you do when light } else { # do whatever you do when dark } Thinking about this a bit further, it seems that the time being greater than some offset from sunset could evaluate true for quite a while and that likewise time being less than sunrise also evaluating true depending upon when the actual evaluation of each variable gets reset. What I'm thinking is that your "seemingly logical" expression is caught up in a semi-non-logical evaluation. Anyway, consider flipping the logic around as that has always worked for me (assuming that you don't have large offsets and you don't live near one of the poles such that you adjust off a day boundary). Gregg ```
 Re: [mh] what am I doing wrong? From: - 2008-05-15 02:41:19 ```> > Thinking about this a bit further, it seems that the time being greater > than some offset from sunset could evaluate true for quite a while and > that likewise time being less than sunrise also evaluating true > depending upon when the actual evaluation of each variable gets reset. > What I'm thinking is that your "seemingly logical" expression is caught > up in a semi-non-logical evaluation. Anyway, consider flipping the > logic around as that has always worked for me (assuming that you don't > have large offsets and you don't live near one of the poles such that > you adjust off a day boundary). > Gregg I see what you mean about the possible evaluation quandry. I'll try inverting the logic as you suggested and see what happens. While I'm working on that, a little clarification on what I want to happen; in case someone knows of a much simpler method. Instead of having a true garage on my house, that is enclosed on all sides with a door that goes up and down; I have what in the South is commonly called a "carport". It's open to the outside world to the East and the South. There is a door into the kitchen on the North wall. I installed an X10 motion detector in the space between the wooden door to the house and the glass storm door. The idea is that when that motion detector senses motion, I want it to turn on the "\$Garage_Light" (yes, I know I should really rename it to be \$Carport_Light ;) ) if it's dark enough to need the light. My original code without any kind of time comparison works great. When the motion detector senses motion, it turns the light on, waits 120 seconds, and turns it back off again. Trying to save a little electricity as well as maybe make the light bulbs, which are a pain to change, last a little longer; I want to add in the time condition. I'm just doing something wrong. The part that's really strange to me is that I *thought* my original code was just like the code someone posted in an example section of the WiKi: http://misterhouse.wikispaces.com/MotionDetection so I thought it should be a piece of cake. *confused* -- -Monte ```
 Re: [mh] what am I doing wrong? From: Gregg Liming - 2008-05-15 03:23:01 ```montef@... wrote: > While I'm working on that, a little clarification on what I want to > happen; in case someone knows of a much simpler method. First, do make sure that \$Time_Sunrise and \$Time_Sunset are correct for your specified lat and long ini parms. Second, although you won't get offsets, consider using the global var, \$Dark: if (\$Dark) { # turn light on } \$Dark depends on \$Time_Sunrise_Twilight and \$Time_Sunset_Twilight. For me, you'll likely still want a light on around the edges of \$Dark as it's a bit overly aggressive. But, at least \$Dark simplifies things a bit if you are still having issues. BTW: I use a more complex method (not simpler) of determining "darkness" where I apply a dynamic offset derived as a function of the \$Weather{Clouds} var since cloud cover significantly affects real light levels for me. Some might suggest just using a photocell; but, invariably, my photocells aren't quite that accurate and/or are influenced by artificial lighting. Gregg ```
 Re: [mh] what am I doing wrong? From: - 2008-05-15 03:33:57 ```> First, do make sure that \$Time_Sunrise and \$Time_Sunset are correct for > your specified lat and long ini parms. Second, although you won't get > offsets, consider using the global var, \$Dark: > > if (\$Dark) { > # turn light on > } > > \$Dark depends on \$Time_Sunrise_Twilight and \$Time_Sunset_Twilight. For > me, you'll likely still want a light on around the edges of \$Dark as > it's a bit overly aggressive. But, at least \$Dark simplifies things a > bit if you are still having issues. > > Gregg Ohh! That sounds helpful. I didn't know we had that global var. I'll give that a shot tomorrow and see what happens. I'm giving up on it for the night and going to bed, since it is; indeed, \$Dark. ;) Thanks Gregg -- -Monte ```
 Re: [mh] what am I doing wrong? From: Andy M. - 2008-05-16 02:33:31 ```
I have just done some testing using your original "if" statement code. I was wrong (gulp!) - as Gregg said. The time based mathematics in the time_greater_than() does work.

I think you should confirm that you have your latitude and longitude set correctly (as Gregg also suggested).

If still no joy then start adding heaps of debug statements, starting with the following (which also makes a great permanent piece of code):

print_log "Sunrise is \$Time_Sunrise,
Sunset is \$Time_Sunset";
}

Andy.

PS. If "still still no joy" then put the following in a test file as a demo. Play with the offsets to make the tests switch over during the time of day when you are testing.

if (time_less_than(time_add "\$Time_Sunset - 0:15") or
print_log "test true";
} else {
print_log "test false";
}

if (time_less_than("\$Time_Sunset - 0:15") or
time_greater_than("\$Time_Sunrise - 0:15")) {
print_log "test true";
} else {
print_log "test false";
}