Automation with condition: if <other automation> triggered in the previous 2 minutes

Hi,

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?

My attempt:

  - condition: template
    value_template: '{{ as_timestamp(now()) - as_timestamp(states.automation.automation1.attributes.last_triggered) | int < 120 }}'
1 Like

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

You can test in Developer toolsTemplates to see if that works. I use the following similar construct for this kind of thing:

      value_template: "{{ (now()-state_attr('automation.automation1','last_triggered')).seconds < 120 }}"

You were close. Try:

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

1 Like

Yes, this is the situation on startup so any tests that check last_triggered must take this into consideration.

1 Like

Sorry Phil, but doesn’t that only test if it’s not none.? You need to get it to pass if it is none don’t you ?

Ie if none or secs > 120 or whatever

If it’s none, then it hasn’t triggered, and if it hasn’t triggered, then it certainly hasn’t triggered within the last two minutes. :wink:

The requirement is less than, not more than.

Is my get out of jail card
Plus I didn’t read the post I just scanned it seeing the condition wasn’t robust
:man_shrugging:

1 Like

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”

Can you please post your whole automation as diagnosing this from a distance is not helped by examining just one line at a time etc.

Well it doesn’t work because you failed my test. I purposely included an error you didn’t spot and fix.

:sweat_smile:

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.

2 Likes