var Timer timer = null
rule "Presence Wohnzimmer"
when
Item alarm_multisensor_3 received update or
Item sw_tv_wohnzimmer changed
then
if (timer != null) {
timer.cancel
timer = null
}
Thread::sleep(10000)
presence_wohnzimmer = 1
timer = createTimer(now.plusMinutes(20)) [|
presence_wohnzimmer = 0
]
end
The rule is triggered from item alarm_multisensor_3 (I guess it’s a motion sensor). If timer is not null, timer will be cancelled and set to null, after that, the thread will going to sleep, and after 10 seconds, presence_wohnzimmer will be set to 1. there is no condition not to set it to 1, and timer is created without any conditions, too. The only thing is, if motion is detected after 20 minutes, there will be (potentially) 1 minute darkness, because of switching off lights (through timer) and delayed switching on (next minute).
thanks for you reply. Yes, alarm_multisensor_3 is a motion sensor and yes, there are no conditions preventing presence_wohnzimmer from switching to 1, because that motion sensor (and switching tv on/off) are the only conditions for presence_wohnzimmer.
Yes, the rule should work as you described it, and yes, there may be a dark minute with bad timing, as you have to decide a timeout in every light automatic, else the light will stay on forever.
But all that is not the point, i had the lights turning off, although there was an event of alarm_multisensor_3 three minutes before that - and that should not have happened with theese rules, right?
Greets,
Stefan
Edit: Is there a possibility to see the timers remaining time?
just to ensure, that every part is done well. You will find those logs in openhab.log. (I’m not sure if the time stamp in 2nd logInfo will be parsed correctly but this could be enforced)
So here is what it looks like… the motion sensor reported 3 times the movement at 12:27:
2016-10-16 12:27:33 - alarm_multisensor_3 state updated to 0
2016-10-16 12:27:34 - alarm_multisensor_3 state updated to 0
2016-10-16 12:27:34 - alarm_multisensor_3 state updated to 0
so three timer (with the same name) were created but only one was cancelled? So 20minutes later a get 2 executed timers and my presence_wohnzimmer would switch to off and so the lights could.
Testing to see if the timer is null is how I usually deal with these situations. However, make the last line of your Timer body setting timer to null so once the timer goes off it gets reset and the next time the rule runs a new timer is created.