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