Complex presence logic in a rule

Hi,

Is there a description somewhere of the advanced “has stayed” operator for presence sensors in the rule engine?

I can infer, but an explanation would be nice. Like if I come home and the sensor changes from “not Present” to “present” and then this rule triggers, but I WAS away for > 5m, would the “if” pass?

Basically, what I’m trying to do is say “if there is motion outside the front door, unlock the front door ONLY if I have arrived at home in the last 5 minutes”.

If that makes any sense. I could do it when I arrive but I thought it might be cooler to have it unlock just as you’re walking up.

Related - and let me know if I should open a feature request - is to add an operator to the “advanced” section of presence tiles so that we have “arrived in the last N minutes” in addition to “stayed away for N minutes”. Would be awesome and I assume trivial to implement as the inverse of what’s there now.

Thanks !

Matt

Matt

1 Like

Hi Matt-
You can find a summary of the State Stays logic in the following release post:

There’s two different places this can be used (Triggers vs IF Conditions) and the concept is exactly the same, but there’s some nuance in the implications of how you use it.

:information_source: The concept is as it reads: the state of an attribute must stay the same value for the specified period of time.

Trigger

This is a really convenient item in a Trigger as it enables you to only run a rule if an attribute has stayed a certain value for a period of time. If we have a trigger for door stays open for 5 minutes, then the rule will only run if the door stays open for that full duration. If the door opens, then closes anytime within the 5 minute window, then the trigger is no longer valid and is discarded. The timer basically get’s reset to 0 and the door has to stay open for the full 5 minutes before the rule is actually triggered.

image

IF Condition

The concept is the same within IF Conditions, but rather than set a timer and wait for the time to elapse, the IF condition looks back in time. So when you have an IF condition of door has stayed closed for 5 minutes, then the door must have been closed for the past 5 minutes at the time the IF condition is evaluated in order for it to be true.

With that in mind, we see one common mistake with implementation of ‘state stays’ in IF Conditions. If you set your Trigger for door changes to open and then have an IF condition for door has stayed open for X minutes, then the IF condition will never evaluate to true. This is because the door will have just changed to open… and when the IF condition runs, it looks back 5 minutes and sees that the state has changed during that interval, so it returns false.

Depending on the goal, you could accomplish this today. The condition might be something like:

IF: Your Phone - presence is 'present'
THEN:
   IF: Your Phone - presence has stayed 'present' for 5 minutes
   THEN: //do nothing
   ELSE: 
     //take your action

The idea here being that your phone must currently be present, but it must have changed to present within the last 5 minutes – hence using the ELSE path as a ‘not stayed present for 5 minutes’. And when combined with the top-level IF condition, the nested ELSE path could be read as ‘changed to present within the past 5 minutes’.

PS. If it’s something you would reuse in several rules, you could use variables to track if the presence changed to present within 5 minutes and flag it true or false accordingly.

1 Like

@josh thank you so much man I really appreciate your in-depth reply! I will try it out! In the process of switching from ST to HA so the home automation is sort of in flux right now, but will try this as soon as I’m done. Thank you!

1 Like