I have an automation trigger like {{ now() - this.attributes.last_triggered > timedelta(minutes=25) }}
and I am pretty sure this worked before.
Since 2025.1 this creates a log error Template variable error: 'this' is undefined when rendering '{{ now() - this.attributes.last_triggered > timedelta(minutes=25) }}'. The fault comes up every minute, so this is not on first run only.
Is this a known problem or is there something I am doing wrong?
Seems to only happen if I have the editor open while it triggers every minute. So it probably still is working.
Not sure if that should throw an error though.
It is working now with {{ (now() - state_attr(this.entity_id, 'last_triggered') | default(0 | as_datetime)) > timedelta(minutes=25) }}
However it will still show those errors in the log when editing for some reason.
Why do you think that “this” variable is defined within an automation?
I do not recall this in Docs.
What I do recall is that “this” is meaningful inside “template” entities.
The variable this is the state object of the automation at the moment of triggering the actions. State objects also contain context data which can be used to identify the user that caused a script or automation to execute. Note that this will not change while executing the actions.
While you have your automation visible within the Automation Editor, when and where exactly does the error message appear?
Can you post your automation so I can try it on my system?
BTW, when you create a new automation and it hasn’t ever triggered yet, it will NOT have a last_triggered attribute. It will get one only after it has triggered at least once.
Your Template Trigger is designed to trigger only when 25 minutes have passed since the last time it triggered.
It checks the template every minute. When the automation is new, it won’t have a last_triggered attribute so every minute that it checks the template, it fails to find a last_triggered attribute (and posts an error).
The way to fix this is to add a default time value so that the very first time the template is evaluated it will trigger immediately. This will establish the last_triggered attribute and afterward the template will work the way you expect, triggering every 25 minutes.
I now recall someone else reporting a similar problem where opening the automation in the Automation Editor caused it to immediately evaluate some of its templates.
Your template refers to the this variable which is defined only when the automation has been triggered. In all other situations, it is undefined.
I believe that is what is happening here as well. It attempts to evaluate the template but this hasn’t been defined yet so it reports it as an error.
To be clear, it’s undesirable behavior; it shouldn’t attempt to evaluate the template while in the editor.
There is a way to prevent certain messages from appearing in the log. However, it’s not advisable for this case because it would suppress all template related messages.
I suggest creating a very simple version of the automation (the least amount of code possible) that can still produce the error message then post it as a bug in Home Assistant’s Github Frontend repository. The developers need to know that this is happening.
My impression of HASS GitHub Issues handling is… not great.
If it is obvious that the issue concerns one of the more popular integrations, like say ZHA and MQTT, it is most often quickly labeled as such and integration owners tagged, who I admit are generally quite attentive. So far so good.
But any issues concerning HASS Core itself? At most they get quickly labeled by a person or perhaps a bot, but that label is most often not quite correct, no code owners gets tagged, and no developer ever seems to read the issues or at least never comment on them.
This honestly makes me very unmotivated to bother with reporting HA Core issues at all.
In this case I believe it affects the Frontend because the issue occurs while operating the Automation Editor via the UI.
Nevertheless, I do agree there are more reported Issues, in both repos, than available developers to address them. However, it’s not a reason to avoid reporting a problem; it goes on record and others experiencing the same issue know they’re not alone (and can add details if needed).
Can you confirm that you have successfully used this within an automation’s Template Trigger in previous versions?
Why I ask is because I have consulted with petro and, after he checked the underlying code in Home Assistant, reported that this cannot be used in an automation’s triggers section.
It can be used in an automation’s conditions or a wait_for_trigger within actions but not in the triggers section.
The reason is because this exists only after the automation is triggered by one of its triggers within triggers.
So the error message isn’t actually a bug but a way of reporting (arguably not ideal way) that you can’t use this in the automation’s main Template Trigger.