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