Currently (unless i’m mistaken) the easiest way to prevent a automation from running multiple times within a certain period is to use a yaml template like
{% set nw = now() %}
{% set last = state_attr('automation.ventilate_game_room', 'last_triggered') %}
{{ 17 <= nw.hour < 21 and
(last is none or (nw - last).total_seconds() > 4 * 60 * 60) }}
Which is ok, but means, you have to do it in YAML, and therefore remember the code, or look up the code. And if you rename the automation, you have to update the state_attr aswell.
I request, this be added to the UI as an condition. Last triggered, with a time component you can select seconds/minutes/hours.
The use case for this is for things like morning/night routines (but obviously not limited to these), you may have a bed sensor to trigger a a action like that, and you want to only trigger these once a day.
For example
If bed presence goes from detected to clear
if time of day is between 6:30am and 8am
If last triggered > 2 hours ago (therefore outside the window)
start morning routine
You can make the mode single and add a delay at the end of the automation.
That will prevent the automation from running again while the delay is still running
I,ve made the experience that the this variable isn’t 100% reliable. Always working:
condition: template
value_template: |
{%- set last = state_attr(this.entity_id, 'last_triggered') %}
{{ last is not none and now() - last > timedelta(seconds=5) }}
Comprehension question: Shouldn’t it be last is not none ?
How did you test the automation that contains that Template Condition?
Post the entire automation.
If it’s none it means last_triggered has no value which is the case when the automation has never been triggered.
If it has never triggered yet, the Template Condition reports true.
If you change it to not none, the Template Condition will always report false for an automation that has never triggered yet. In other words, it will prevent the new, untriggered automation from ever updating its last_triggered property (because that property only gets updated after the automation’s trigger is executed and its condition evaluates to true).
I don’t recommend relying on that so-called condition-tester. Its results aren’t always correct and it can’t resolve variables (like the trigger variable and, apparently, thethis variable).
Test the automation by ensuring its trigger is triggered (you haven’t posted the entire automation so I don’t know what kind of trigger(s) it’s using).
Coming in late on this since I couldn’t find a similar thread. I even went as far as trying to make a custom component to create a UI condition but couldn’t get it to work.
I still use a template like this on almost every automation to prevent unintended runs, and agree with OP it’s super annoying to copy/paste.
It feels like this would make sense as a UI condition option…
condition: template
value_template: |-
{% set last = state_attr('automation.entity_id', 'last_triggered') %}
{{ last is none or (now() - last) > timedelta(hours=1) }}
alias: Don't run if last triggered within time range