I have an two automations to open two gates.
Automation1: The outside gate needs to open when I arrive within a certain bigger zone - this works well. Trigger is simple: arrive in big zone. There is a single condition; open if it is not open yet.
Automation2: The inner gate needs to open when I arrive in a smaller zone (inside the bigger zone). Trigger is thus, arrive in small zone. There are two conditions: open if it is not open yet AND open only if Automation1 triggered within the previous 2 minutes.
The idea is that I can arrive and go through the two gates but for safety’s sake, only open the inner gate if the outer already opened. Due to an alarm and inaccuracy in GPS detection/updates, I’d like this extra safeguard so that the inner gate also only opens as late as possible but just in time.
The Automation2 works if I only add the first condition, but I can’t figure out how to specify the second conditition.
I tried a couple of things with a data template, but it doesn’t work. Can you help?
Or in condition you make a state when outside gate is open. (The gate can be open or closed)
then the second automation will only fire when the outside gate have state open
- condition: template
value_template: >
{% set last = state_attr('automation.automation1', 'last_triggered') %}
{{ last is not none and (now() - last).total_seconds() < 120 }}
The last_triggered attribute is a Python datetime object, just like the return value of now(). Subtracting them results in a Python timedelta object, whose total_seconds() method returns the equivalent number of seconds.
EDIT: Updated per the comments below to account for last_triggered being None.
EDIT 2: Forgot to remove call to as_timestamp() around call to now() from original post. Fixed.
Small Warning here : -
If any of these automations use ‘initial: true’ (or has just never run before) then the last triggered attribute of the automation will be lost and you will need special tests to even get your automation to fire for the first time (if the condition of last triggered is being tested)
Thanks for the replies but it doesn’t seem to work.
Copy/pasting the below
{% set last = state_attr('automation.gate_open_mom_arrival_script', 'last_triggered') %}
{{ last is not none and
(as_timestamp(now()) - last).total_seconds() < 120 }}
In the template checker in developer tools gives “Unknown error rendering template”
Well it doesn’t work because you failed my test. I purposely included an error you didn’t spot and fix.
Just kidding! There is an error I overlooked. Try this:
- condition: template
value_template: >
{% set last = state_attr('automation.automation1', 'last_triggered') %}
{{ last is not none and (now() - last).total_seconds() < 120 }}
I had forgotten to remove the call to as_timestamp() around the call to now(). Sorry about that.