Multiple light switches / multiple triggers


#1

1) Give a description of the problem
Piston 1 turn on outside lights at sunset, wait 2 hours, turn off. Piston 2 if presence changes to present after sunset, turn on outside lights, porch light, driveway lights, wait 5 minutes, turn off. But if the outside lights are supposed to still be on due to piston 1, piston 2 still turns them off.
2) What is the expected behavior?
Outside lights turn on at sunset, wait 2 hours, turn off. Other piston, if presence changes to present after sunset, turn on driveway lights, outside lights, front porch light, wait 5 minutes, turn off.
3) What is happening/not happening?
Individually each piston works correctly, but if presence changes to trigger the second piston to turn on the 3 lights during the time that the 1st piston is waiting its 2 hours, then it will turn off the outside lights after its 5 minute wait, instead of letting the first piston continue its 2 hour wait. There is also a motion triggered “piston 3” that will interfere with the 2 hour wait as well.

**4) Post a Green Snapshot of the piston![image|45x37]


#2

The second and third piston don’t know anything about the first piston, so if they are triggered, they turn on Switch 13 (which has no effect if it is already on), wait 2 minutes, and then turn it off, whether it was on to start with or not. There are several ways to attack this problem. One would be for the 2nd and 3rd piston to check to see if Switch 13 is on to start with, and if so, then don’t touch it (or at least don’t turn if off). Another way to attack this problem is to use global variables, but you sill see some very experienced people on this forum that recommend against using them unless absolutely necessary. Also they are only updated upon exit of a piston, so they may not be updated when you think they should be. A third way to attack the problem is to put everything into a single piston and use local variables, but the Wait command has some behavior that I am still trying to figure out. So my advice for this particular case would be the first option I mentioned.


#3

My suggestion would be to create an “off_time” date/time variable for each switch (13, 10, and 3) that are affected by this issue. Those would have to be global so that all of the pistons can see them. The have to be date/time rather than just time so that they’ll handle crossing midnight properly. You’ll also have to create an initializing routine to set all these values to the current date/time as an intentionally lower value than the trigger events will have.
When, when you turn the lights on, check to see if the current time + the delay (2 minutes, 2 hours, etc.) is after each switch’s current off time. If it is, replace that switches off time with the current time + the delay. Then, instead of the straight “Turn off”, you’ll have to test to see if the current time equals the switch’s off time (Rounded to within a minute because the date/time stores fractional seconds.) If it is, execute the “Turn off”, otherwise skip it and it’ll be handled by the “Turn off” that executes later.


#4

I initially was trying that by having the outside lights on its own “if” line but then the piston would not turn on those lights until after the “wait” from the previous “if” command had completed.