Automation template trigger using last_triggered, timedelta and now()

I have a blueprint that defines the following trigger:

- alias: Run at least once every 30 minutes
  platform: template
  value_template: "{{ (this.attributes.last_triggered + timedelta(minutes=30)) < now() }}"

It appears this trigger never fires for automations that I create from the blueprint. I tested the condition in the developer tools template renderer (replacing the this.attributes.last_triggered with state_attr('automation.an_automation', 'last_triggered')) and it appears to work there with that modification.

The documentation says that it is ok to use this in automation trigger_variables and that last_triggered only gets updated once the actions run, so I assumed it is also ok to use it in triggers themselves, considering that trigger_variables get set before triggers are evaluated.

Maybe I am missing something? Is this supposed to work this way? If so, what am I doing wrong?

Thanks!

If you have a brand-new automation, it cannot have an attribute last_triggered, because it was never triggered before. So


    {%- set x = 'automation.my_brandnew_automation' %}

    {{ (states[x].attributes.last_triggered + timedelta(minutes=30)) < now() }}

will give you: TypeError: unsupported operand type(s) for +: ‘NoneType’ and ‘datetime.timedelta’ because


    {{ (states[x].attributes.last_triggered }}

results in none.

Using a default can avoid this by using


    {{ (states[x].attributes.last_triggered + timedelta(minutes=30)) < now() if states[x].attributes.last_triggered is not none else true }}

or


    {{ (states[x].attributes.last_triggered  |default(states[x].last_changed, true) + timedelta(minutes=30)) < now() }}

or


    {{ (states[x].attributes.last_triggered  |default(now(), true) + timedelta(minutes=30)) < now() }}

@pedolsky Thank you for the detailed response and you are of course correct: if I had a brand new automation this would be the case.

In my case though (and I understand I didn’t specify this before) I’ve made a change to an existing Blueprint (and existing automation instances) that introduced the trigger. The last_triggered attribute exists on the automations in question (and I was able to correctly evaluate the template with the correct result in the template tab in the developer tools). At least with the this.attributes.last_triggered idiom replaced with state_attr(‘automation.my_blueprint_automation’, ‘last_triggered’) there are no runtime errors and the template evaluates as expected. I did check my logs for other runtime errors during actual automation execution and didn’t find any. Other triggers of the automation continue to function as expected.

Since this.attributes.last_triggered behaves as described here

Avoid using states.sensor.temperature.state , instead use states('sensor.temperature') . It is strongly advised to use the states() , is_state() , state_attr() and is_state_attr() as much as possible, to avoid errors and error message when the entity isn’t ready yet (e.g., during Home Assistant startup).

source

I’d try with

state_attr(this.entity_id, 'last_triggered')

which I for my part am using in countless automations without any problems.

1 Like