In general, we here on the forum are not computers. We are humans that can interpret the behavior of a rule given both the code of the rule and the output from running the rule.
At first glance it seems a bit overly complicated but it’s hard to tell. It would be easier to read with better spacing. See Coding Conventions in Rules.
I’m not sure why you would need a timer and to run the rule every second. It’s running every second anyway so why not use that to blink the light instead of creating a second Timer?
Why use Timers at all? You can just take a timestamp when the door opens and when it’s been open for long enough start blinking the light.
Or, alternatively, trigger the rule when the door changes from CLOSED to OPEN and then using a single Timer you can blink the light until the door closes.
that is the problem i want to blink when there is no movement and door has been left open but if someone notice the blinking then cancel the blinking and restart the timers
If you keep the rule running every second (which is usually frowned upon because it means most of the time the rule is consuming resources doing nothing):
Look to see if the door is OPEN
If the door is OPEN, look to see when it opened by checking the timestamp.
If the timestamp is far enough in the past, turn on the light if it’s off or turn off the light if it’s on.
When the door closes turn off the light if it’s ON (separate rule)
When the door opens, set the timestamp (separate rule)
You could, or you could just save the timestamp as a global variable.
Right so
Trigger the rule when the door changes from CLOSED to OPEN
Create a Timer to go off in what ever time you want to wait before flashing the light.
a. When the timer goes off, look to see if the door is OPEN.
b. if OPEN, turn on the light if OFF or turn off the light if ON, reschedule the timer for one second
c. if CLOSED, turn off the light if ON and exit.
You don’t need polling and you don’t need two Timers. All you need is something like:
var Timer doorReminderTimer = null
rule "Door reminder"
when
Item Magnet_coffre_fort_sensor changed
then
if(newState == OPEN && doorReminderTimer !== null) {
logError('door reminder', 'Door opened but timer exists! This shouldn't be able to happen. Ignoring.')
return;
}
if(newState == OPEN) {
logInfo('door reminder', "The door opened! Scheduling the timer for the reminder.")
doorReminderTimer = createTimer(now.plusMinutes(5), [ |
// Still open
if(Magnet_coffre_fort_sensor.state == OPEN) {
val currState = Coffre_fort_Light1.getStateAs(OnOffState)
Coffre_fort_Light1.sendCommand(if(currState == ON) OFF else ON) // toggle the light
doorReminderTimer.reschedule(now.plusSeconds(1))
}
// Now closed
else {
if(Coffre_fort_Light1.getStateAs(OnOffType) == ON) {
Coffre_fort_Light1.sendCommand(OFF)
}
doorReminderTimer = null
}
])
}
end
I just typed in the above. It likely has typos.
Watch the Marketplace over the next week or so. I’ve already plans to publish an alerting rule template there which will do this for you. All you’d have to do is point it at the Item (e.g. the door) and the state for it to be in (e.g. OPEN) and how long it needs to be in that state before alerting.
Ok i tested your rule adapted works but the problem is like this if someone goes in leaves the door open it flashes but then he notices walks in its reset but then walks out by leaving again the door open and then the rule doesnt trigger because the state of the magnet never changed
var Timer doorReminderTimer = null
rule "Door reminder"
when
Item Magnet_coffre_fort_sensor changed or Item Movement_coffre_fort_detector changed
then
if(newState == OPEN && doorReminderTimer !== null) {
logError('door reminder', 'Door opened but timer exists! This shouldnt be able to happen. Ignoring.')
return;
}
if(newState == OPEN) {
logInfo('door reminder', "The door opened! Scheduling the timer for the reminder.")
doorReminderTimer = createTimer(now.plusMinutes(1), [ |
// Still open
if(Magnet_coffre_fort_sensor.state == OPEN && Movement_coffre_fort_detector.state == CLOSED) {
val currState = Coffre_fort_Light1.getStateAs(OnOffType)
Coffre_fort_Light1.sendCommand(if(currState == ON) OFF else ON) // toggle the light
doorReminderTimer.reschedule(now.plusSeconds(1))
}
// Now closed
else if(Magnet_coffre_fort_sensor.state == CLOSED && Movement_coffre_fort_detector.state == CLOSED){
if(Coffre_fort_Light1.getStateAs(OnOffType) == ON) {
Coffre_fort_Light1.sendCommand(OFF)
}
doorReminderTimer = null
}
else if(Magnet_coffre_fort_sensor.state == OPEN && Movement_coffre_fort_detector.state == OPEN){
if(Coffre_fort_Light1.getStateAs(OnOffType) == OFF) {
Coffre_fort_Light1.sendCommand(ON)
}
doorReminderTimer = null
}
])
}
end
Then extend the rule to look for that case. As my rule was written, the only way to stop the light from flushing is to actually close the door which is by design. The reminder is for the door being open after all.
All you would need to do is check to see if the door is still open and it’s been long enough since the last motion before toggling the light. If it’s not been long enough since the last motion, just reschedule the timer until enough time has passed. Only exit the timer when the door closes. That’s still way simpler than managing two timers.
Since the reminder is that the door is open, never cancel it exit the timer until the door closes.