'this' reference in automations

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.

Hmm, testing this the error I get is that the attribute last_triggered does not exist.

What does seem to work however is rewriting the template trigger like this:

{{ now() - state_attr(this.entity_id, 'last_triggered') > timedelta(minutes=25) }}

Giving me the same error that this is undefined, but again only if I have the UI editor open at this trigger. I guess I will ignore it for now :sweat_smile:

To trigger an automation every 25 minutes, it would be easier to use a time pattern.

There are more triggers. However it should trigger at least once if no other trigger became active within the last 25m.

Every time the automation runs, you could start (or restart) a 25 minute timer and have the timer finishing as one of the triggers.

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.

https://www.home-assistant.io/docs/automation/templating/

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.

1 Like

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.

Try this version:

{{ now() - this.attributes.last_triggered | default(as_datetime(0), true) > timedelta(minutes=25) }}
alias: Set actual temperature for TRV
description: ""
triggers:
  - trigger: state
    entity_id:
      - sensor.0x842712fffe387925_temperature
    for:
      hours: 0
      minutes: 0
      seconds: 5
    id: tempchange
  - trigger: homeassistant
    event: start
    id: ha_start
  - trigger: event
    event_type: automation_reloaded
    id: reload
  - alias: If automation did not run for the last 25m
    trigger: template
    value_template: >+
      {{ (now() - state_attr(this.entity_id, 'last_triggered') | default(0 |
      as_datetime)) > timedelta(minutes=25) }}

  - alias: Sensor not updated for more than 3h
    trigger: template
    value_template: >-
      {{ states('sensor.0x842712fffe387925_last_seen') not in ['unavailable',
      'unknown', None] and (now() -
      as_datetime(states('sensor.0x842712fffe387925_last_seen')) >=
      timedelta(hours=3)) }}
    id: sensor_offline
  - trigger: state
    entity_id:
      - sensor.0x842712fffe387925_last_seen
    to: unavailable
    for:
      hours: 3
      minutes: 0
      seconds: 0
    alias: Sensor not available for more than 3h
    id: sensordata_offline
conditions: []
actions:
  - alias: If temperature between 0 and 55 and sensor is available
    if:
      - condition: numeric_state
        entity_id: sensor.0x842712fffe387925_temperature
        above: 0
        below: 55
      - condition: not
        conditions:
          - condition: state
            entity_id: sensor.0x842712fffe387925_temperature
            state: unknown
        alias: Sensor not unknown
      - alias: Sensor available
        condition: not
        conditions:
          - condition: state
            entity_id: sensor.0x842712fffe387925_temperature
            state: unavailable
    then:
      - alias: Set sensor to external
        if:
          - condition: not
            conditions:
              - condition: state
                entity_id: select.0x54ef4410007f487b_sensor
                state: external
            alias: If TRV sensor is not external
        then:
          - device_id: e487ab84643ba5279287fb5863750c01
            domain: select
            entity_id: ab12ce4f2729bee073b23b8fa3c0225b
            type: select_option
            option: external
          - delay:
              hours: 0
              minutes: 0
              seconds: 10
              milliseconds: 0
      - action: number.set_value
        target:
          entity_id: number.0x54ef4410007f487b_external_temperature_input
        data:
          value: >-
            {{ states('sensor.0x842712fffe387925_temperature') | float(0) |
            round(1) }}
    else:
      - alias: Set sensor to internal
        if:
          - alias: If sensor is not internal
            condition: not
            conditions:
              - condition: state
                entity_id: select.0x54ef4410007f487b_sensor
                state: internal
        then:
          - device_id: e487ab84643ba5279287fb5863750c01
            domain: select
            entity_id: ab12ce4f2729bee073b23b8fa3c0225b
            type: select_option
            option: internal
mode: single

The errors appear in the system log when I open the automation editor UI for this automation.

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.

I can see that as a problem.
It should be suppressed then :wink: At least I have not found a way to manually suppress errors.

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.

1 Like

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

FYI

I created this simplified version and it produces errors in the Log at the moment I switch the Automation Editor from Visual mode to YAML mode.

alias: test example abc123
description: ""
triggers:
  - trigger: template
    value_template: >-
      {{ now() - this.attributes.last_triggered | default(as_datetime(0)) >
      timedelta(minutes=25) }}
conditions: []
actions:
  - action: notify.persistent_notification
    data:
      message: test example abc123

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.

EDIT: It’s far more subtle than that …