Time condition fails when there is no last trigger of automation

I have this condition for opening the door when we come with one of the Blutooth tags or or phones, to make sure that it doesn’t happen more than once in two minutes:

{{ now() - state_attr('automation.unlock_door_for_mastiffs_phone', 'last_triggered') > timedelta(minutes=2) }}

We use guest tags as well, and the problem arises when there is no “last triggered” in the database because the tag hansn’t been used for a few months. Is there a way to add something to that line that makes the condition true if there is no last_triggered as well?

Can you do an OR as part of the condition and

{{ state_attr('automation.unlock_door_for_mastiffs_phone', 'last_triggered') == 'None' }}
1 Like

I’m pretty sure I can, thank you very much!

FWIW, there is a more general version of this template using the variable this that can be used as a throttle condition in any automation:

{% set last_run = this.attributes.last_triggered|default(as_datetime(0),1) %}
{{ now() >=  last_run + timedelta(minutes = 2) }}
1 Like