New to HA. Trying to set up an automation for a light to turn on when I get home if I am gone longer than 10 seconds. The automation only works when I remove the “and if” not home for 10 seconds part. What missing?
Hi Gsserafino,
Your automations triggers when greg state changes to home.
Your condition for any action being run is that greg is not home for 10 seconds.
It cannot possibly get to actions because you cannot be there and gone for 10 seconds at the same instant.
Automations #1: trigger only fires when it changes from not true to true.
The trigger is the event that starts the timing, it’s the instant the parameters are met. It then checks conditions to see as a second level check if things are OK to Go.
Wow. I feel dumb. Thanks for the explanation
You’ll get it. It’s easier for me because I’ve seen hundreds of these.
When writing my own more complex ones, I do a simple flow chart and that helps get things in the right order, then go in the UI editor and start making it fit. With the plain language in there it helps you to tell the story of what it’s doing. YAML is a much steeper learning curve.
Welcome to the World of Home Assistant!
It’s easier than it seems, trust me.
The condition you need should check if you (person.greg
) were previously not_home
and that you weren’t home for more than 10 minutes.
To accomplish all of that, it needs to use the information available in the trigger
variable (which is generated after your automation’s State Trigger fires).
The trigger
variable contains information about person.greg
before and after it changed state (in its from_state
and to_state
properties, respectively).
A Template Condition is needed for this purpose.
conditions:
- condition: template
value_template: >
{{ trigger.from_state.state == 'not_home' and
now() - trigger.from_state.last_updated > timedelta(minutes=10) }}
References
@123 - OMG, thank you for this! I struggled for quite while to come up with changing the values to floats to make the time difference calculation work (and what I have does work), now I have learned about “timedelta(…)” !!! So, instead of:
{{ (float(as_timestamp(now())) - float(states('input_text.last_manual_off_bedroom_2'),0)) > 300 }}
I can just use:
{{ now() - as_datetime(states('input_text.last_manual_off_bedroom_2')) > timedelta(minutes=5) }}
now()
produces a datetime object.as_timestamp()
produces a float value.
You can’t subtract a float from a datetime.
What you need to use is as_datetime()
instead of as_timestamp()
.
Thanks - fixed my post