Delay check of Presense.state for Alarm trigger

  • Platform information:
    • RPi4
    • OS: openHABian latest stable

I’m would like to send a notification when a door is opened and no one is home.

For presence I use items:

Group:Switch:OR(ON,OFF)   Presence     "Presence"   <icon_presence>
Switch  MarkHome           "Mark is home"         <network>       (Presence)    {channel="network:pingdevice:192_168_2_40:online" }
Switch  SamanthaHome       "Samantha is home"     <network>       (Presence)    {channel="network:pingdevice:192_168_2_41:online" }

For door opened I use:

Group:Contact:OR(OPEN, CLOSED) gDoor “The doors are [%s]” `

For the rule I use:

rule "Alert when a Door is opened and no one is home"
when
    Item gDoor changed to OPEN
then
    if (Presence.state == OFF) {
    sendBroadcastNotification('A door was opened and no one is home')
    }
end

The problem is that our phones will take some time to connect upon returning home so I want to set a two-minute delay before the presence.state check. I feel like this should be very simple to do but after searching I still haven’t found the answer. Am I going about this the right way? Every example I have seen to create an “alarm” for when no one home seems to be much more complicated.

Set a two minute timer that runs the code you have in the rule. When you come home and open the door, the timer will start, but the presence will update before the timer executes, so Presence.state will be ON. There are lots of timer examples out there.

If you have smart locks, set the presence to Home when one of your codes are used.

It was figuring out how to set the timer that was giving me problems but after spending more time I changed the rule to be:

var Timer timer = null
rule “Alert when a Door is opened and no one is home”
when
Item gDoor changed to OPEN
then
if(timer===null) {
timer = createTimer(now.plusMinutes(2)) [|
if (Presence.state == OFF) {
sendBroadcastNotification(‘A door was opened and no one is home’)
}
timer = null
]
}
end

So far this seems to be working but I wonder why other solutions seemed so much more complicated.

Thank you!

Looks good, but best to give the timer a more specific name to avoid conflicts. Also, think about rescheduling the timer if the door is opened while it is running.

Thank you for the tip about the timer name.

I’m not understanding the suggestion to reschedule of timer. My goal is was to add a two-minute delay for my presence to be updated after any door is opened while returning home. Why would I reset in this case?

My next goal was to add the name of the door to the notification. To accomplish that I needed to change the triggering event from Item changed to Member of Group.

rule "Alert when a Door is opened and no one is home"
when
    Member of gDoor changed to OPEN
then
    if(doortimer===null) {
    doortimer = createTimer(now.plusMinutes(2)) [|
        if (Presence.state == OFF) {
            sendBroadcastNotification("Door " + triggeringItem.label + " has opened")
        }
        doortimer = null
        ]
    }
end

I was thinking about quick trips out, where your presence state will not be updated to OFF before returning home. Maybe you forgot something and ran back into the house? By rescheduling the timer, the notification would be sent 2 minutes after the last door opening. However, have you actually tested this rule out? After leaving the house, it looks like you will always receive a notification from the closing of the door on the way out.

I think you are going to need to remove the Presence check from inside the timer and add another trigger to cancel the timer when Presence changes. And reschedule the timer when the door is opened and the timer is running.