Condition Template help - trigger an action if an automation was not triggered in a specific time

Hi there!

How do I write a Condition Template to trigger an Action if an Automation was not triggered in a specific time?

Example. I know that this condition below will trigger an action if the Automation_2 was triggered in the last 15 seconds, right?

But how do you make the opposite of this condition? What if I want the Automation_1 to trigger IF Automation_2 was not triggered in the last 15 seconds?

- alias: Automation_1
  trigger:
  - platform: state
    entity_id: iphone
    to: 'home'
  condition:
  - condition: template
    value_template: "{{ (as_timestamp(now()) - as_timestamp(state_attr('automation.Automation_2', 'last_triggered')) | int(0)) < 15 }}"
  action:
  - service: switch.toggle
    entity_id: switch.light

Thanks.

I’m confused. And it’s not getting into my head right now. But can I just write it as…

condition:
  - condition: template
    value_template: "{{ (as_timestamp(now()) - as_timestamp(state_attr('automation.Automation_2', 'last_triggered')) | int(0)) > 15 }}"

that seems like it should work to me. :man_shrugging:

It was the solution I immediately thought to post when I first read it.

But I thought “it couldn’t be that easy. I must be missing some other requirement that the OP didn’t mention…”. :wink:

Just as I thought. It took me a long time staring at the codes to really understand it.

by the way, what is the difference between state_attr and is_state? I mean can I change the state_attr as like this other example:

  - condition: template
    value_template: "{{ (as_timestamp(now()) - as_timestamp(is_state('device_tracker.iphone', 'home')) | int(0)) > 20 }}"

or should I still use the state_attr?

  - condition: template
    value_template: "{{ (as_timestamp(now()) - as_timestamp(state_attr('device_tracker.iphone', 'home')) | int(0)) > 20 }}"

That is asking if the actual state of the entity is equal to ‘home’.

That is asking what is the value of the entity’s attribute ‘home’?

they aren’t the same thing.

As a matter of fact the second one will likely always be 0 because the if the attribute is not defined so it will default to 0 (which is what the int(0) does - it defaults the returned value to 0 if the value is not an integer).

And even if on the off chance that the entity did have an attribute called ‘home’ it’s likely not going to be a datetime object and so it can’t extract a timestamp from it. So again the value would default to 0.

Unless of course, the entity ‘device_tracker.iphone’ does actually have an attribute called ‘home’ and it is in fact a valid datetime object then that will work fine.

But again those two values aren’t going to be the same.

the first will return a boolean (true/false) then convert it to a 0 because a boolean isn’t a number.

the second could be a valid timestamp depending on if the attribute is defined and is a valid datetime object or it could default to 0 if not.

So I should use is_state to determine if the “iphone” was home for more than 20 seconds?

  - condition: template
    value_template: "{{ (as_timestamp(now()) - as_timestamp(is_state('device_tracker.iphone', 'home')) | int(0)) > 20 }}"

What I’m trying to do is to avoid two phones to trigger an automation when both arrive home at the same time. Like if iphone_1 arrived first in the first 20 seconds, it will prevent iphone_2 to trigger the same automation if it arrives at home in less than 20 seconds from the time iphone_1 arrived home. And I think this is the easiest solution in my case.

iphone_1:
...
  - condition: template
    value_template: "{{ (as_timestamp(now()) - as_timestamp(is_state('device_tracker.iphone_2', 'home')) | int(0)) > 20 }}"
  action:
  - service: switch.toggle
    entity_id: switch.light

iphone_2:
...
  - condition: template
    value_template: "{{ (as_timestamp(now()) - as_timestamp(is_state('device_tracker.iphone_1', 'home')) | int(0)) > 20 }}"
  action:
  - service: switch.toggle
    entity_id: switch.light

Do you think this condition could work?

No.

you need a thing that actually contains a datetime object that you can pull a timestamp from to compare to the timestamp of now().

the test for the state of home is going to be a boolean that doesn’t have any date attached to it.

but you can test the last_changed attribute.

so create a template that says “is the state ‘home’ and is the ‘last_changed’ attribute timestamp longer than 20 secoinds ago?”.

Thanks for the guide. I think I know how to make this work. Hopefully. I’ll try it out first, if it doesn’t work, I’ll come back. :smiley:

1 Like