Rule Condition: Last Triggered

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

If you use the this variable to self reference instead of using the state_attr() function, there is no need to update.

How would that work?

{% set last = this.last_triggered %}
{{ last is none or (now() - last).total_seconds() > 60 * 60 }}

or

{% set last = this.attributes.last_triggered %}
{{ last is none or (now() - last).total_seconds() > 60 * 60 }}

or something else?

Your second option should work… I normally use timedelta() because I find it easier to comprehend when reading quickly.

{% set last = this.attributes.last_triggered %}
{{ last is none or now() > last + timedelta(hours = 1) }}
1 Like
In 'template' condition: UndefinedError: 'this' is undefined

Get this error when using this in a condtion

{% set last = this.attributes.last_triggered %}
{{ last is none or now() > last + timedelta(hours = 1) }}

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

1 Like

facepalm

I have overlooked the or. Had and in my head.

1 Like

i used the visual editor and tried testing the condition from the GUI.

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