Door flash lights after some time left open and no movement until door is closed and no movement

Hello
So i am trying to blink the lights if the door is left open. Is this rule correct ?
thanks

var Timer blink_coffre = null
var Timer blink_door = null
rule "door is open blink"
when
    Time cron "* * * * * ? *"
then
    	if(blink_door === null && Magnet_coffre_fort_sensor.state == OPEN && Movement_coffre_fort_detector.state == CLOSED) {
            logInfo("blink rule door", "start the timer to start the blink of door")
            blink_door = createTimer(now.plusMinutes(1)) [|
            //start blinking script
            if(blink_coffre === null) {
                blink_coffre = createTimer(now.plusSeconds(1), [|
		        if(Coffre_fort_Light1.state == 100){Coffre_fort_Light1.sendCommand(0)}
		        else{Coffre_fort_Light1.sendCommand(100)}
                blink_coffre.reschedule(now.plusSeconds(1))
                logInfo("blink rule door", "door blinking lights")
                ])}	//end blinking light script
                logInfo("blink rule door", "1 minutes has gone there is no movement and gate open blinking has started")
                blink_door = null
    		]
    	} 
        else if(Magnet_coffre_fort_sensor.state == OPEN && Movement_coffre_fort_detector.state == OPEN) {
                if(blink_door === null && blink_coffre === null){
                    return;
                }
                else{
                    blink_door?.cancel()
                    blink_door = null
                    blink_coffre?.cancel()
                    blink_coffre = null
                }
        }
        else if(Magnet_coffre_fort_sensor.state == CLOSED && Movement_coffre_fort_detector.state == CLOSED) {
                if(blink_door === null && blink_coffre === null){
                    return;
                }
                else{
                    blink_door?.cancel()
                    blink_door = null
                    blink_coffre?.cancel()
                    blink_coffre = null
                    Coffre_fort_Light1.sendCommand(0)
                }
        }
    
end

You tell us? What happens if you run it?

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.

1 Like

yes it runs just fine

well i know that sorry really sorry

hmm i will try to do better one moment please …

So you mean something like this hmm doesn’t work it will trigger directly flashing the lights

            //start blinking script
            if(blink_coffre === null) {
		        if(Coffre_fort_Light1.state == 100){Coffre_fort_Light1.sendCommand(0)}
		        else{Coffre_fort_Light1.sendCommand(100)}
                        logInfo("blink rule door", "door blinking lights")             

you mean creating a new item just to hold the time when it changed ? something like this

DateTime Magnet_coffre_fort_sensor_LastUpdate "magnet sensor[%1$tF %1$tR] {channel="whatever_it_comes_from" [profile="timestamp-update"]}

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):

  1. Look to see if the door is OPEN
  2. If the door is OPEN, look to see when it opened by checking the timestamp.
  3. 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.
  4. When the door closes turn off the light if it’s ON (separate rule)
  5. When the door opens, set the timestamp (separate rule)

You could, or you could just save the timestamp as a global variable.

Right so

  1. Trigger the rule when the door changes from CLOSED to OPEN
  2. 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.

nice cant wait

ok now my time is up but tommorow i will test and report back Thank you for your time

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.