See the following for determining when it is night.
Now that we know it is night we have a couple of challenges with a rule like this.
-
How frequently does the motion detector go off? Do you truly only receive on event when you go past the detector and only one when you come back? Or do you receive multiple events in this case? I’ll assume it is only one but if it is multiple you will need to add some debouncing.
-
The second challenge is we need to keep track of whether a given motion sensor event is the first or the second one in the sequence and do something different based on which it is.
So the over all approach would be something like the following:
Items:
Contact MotionSensor ...
Switch Light ...
// Create a Swtich to help us determine whether this is the first or second triggering of the MotionSensor
// This could also be implemented as a global var in the Rules file, I like to use Items because I can use
// restoreOnStartup which lets the rule support restarts more reliably and with fewer lines of code.
Switch MotionSensorFlag
Rule:
rule "MotionSensor triggered"
when
Item MotionSensor received update
then
if(TimeOfDay.state.toString == "Night") {
// First MotionSensor trigger in the sequence
// I use != ON so it works correctly in the case that MotionSensorFlag is Undefined
if(MotionSensorFlag.state != ON) {
Light.sendCommand(ON)
MotionSensorFlag.sendCommand(ON)
}
// Second MotionSensor trigger in the sequence
else {
MotionSensorFlag.sendCommand(OFF)
Thread::sleep(10000) // You could use a Timer but I think Thread::sleep results in simpler code and it works well here
Light.sendCommand(OFF)
}
}
end
The above only works if you truly only receive one event from the MotionSensor on each pass. If not it will get a whole lot more complicated. Search the forum for “debounce” and you will see lots of examples of ways to deal with it though.
EDIT: Forgot to add the test for Night