'this' reference in automations

I would say it is actually the template itself or now() (not sure, since I did not check the code) that is triggering the automation every minute to evaluate it, so per definition/documentation this should be available at the moment it is evaluated.

You can find this kind of solution all over the forum, so I am pretty sure it worked before.

Also with state_attr(this.entity_id, 'last_triggered') it is working correctly right now, just triggering log errors when editing.

I use this in event_data on an event trigger, though I added that code only like a week ago. This is on an automation trigger, not a wait_for_trigger. Pretty sure I tested this in a separate automation test case and it worked fine, though I never checked the logs for errors when editing the automation…

Edit: Here is a test case that shows this working in event_data of an event trigger. Traces shows that origin gets populated with this.entity_id and properly matched by the trigger.

triggers:
  - trigger: event
    event_type: testing_testing
    event_data:
      origin: "{{ this.entity_id }}"
actions:
  - if:
      - condition: template
        value_template: "{{ trigger.platform == 'event' }}"
    then:
      - action: notify.persistent_notification
        data:
          message: Yes this works as intended.
    else:
      - event: testing_testing
        event_data:
          origin: "{{ this.entity_id }}"

Please post a link to the documentation indicating this is available for use in an automation’s triggers section.

Please post a link to a working example.

From personal experience, I have used this in conditions and actions but never in triggers. I know I once tried to use it in the trigger of a Trigger-based Template Sensor and it failed to work (it’s undefined). However, that’s not the exactly the same as for an automation.


BTW, I asked another Regular user to test the example I posted above and they reported the same result (this is undefined errors). They used 2025.1.0 and I used 2024.12.5 for the test.

I did both before.

Please find the automation above for a working example.
Please find the documentation here:
https://www.home-assistant.io/docs/automation/templating/#available-this-data

The variable this is the state object of the automation at the moment of triggering the actions. 

If it should not be available at the moment of triggering then the wording should be changed (also, it is available)

  • Referencing this with your automation’s Template Trigger is clearly failing for you (and for me and for another person who tested it) on both the current version of Home Assistant and the previous one.

  • Mayhem_SWE reports that this is working in an automation’s Event Trigger.

We have conflicting reports for how this is being handled within an automation’s triggers section. It would appear that there’s a missing clue to explain the disparity.

event triggers are limited_templates only, I’ll review the code again, but I’m fairly positive that this will not work in any triggers (limited or not) inside an automation.

1 Like

Well, do try my test case above for the event trigger. Unless I am missing something it seems to be working as intended/expected.

I am not stating that it is not working for me with state_attr(this.entity_id, 'last_triggered'), there is a screenshot with the trigger successfully firing.

Right now I am just making aware of the fact that it triggers log errors, which should be fixed in my opinion. The trigger seems to fire on now() every minute and only then is evaluated, after which this should be available as per documentation.

Double checked the code, this is actually passed to all triggers in automations.

So OPs error is coming from this not having an attributes property.

2 Likes

Originally you said this:

The now() within the template caused it to be evaluated every minute, and every time it attempted to evaluate the template, it failed to be successful because this was reported to be undefined.

Is that what you mean by “successfully firing”?

Or do you mean that at some point in the template’s evolution (because it has clearly changed from what’s shown in your first post to later posts) the Template Trigger actually served to trigger the automation and produce a trace?

Thanks for the clarification! :+1:

It explains why the original template shown in the OP’s first post was subject to the error (it referenced attributes) whereas later, when the template was modified to NOT use attributes, its behavior changed. It also explains why this.entity_id works for Magnus.

  • The trigger is working as expected with state_attr(this.entity_id, 'last_triggered').
  • When editing it will still cause the log errors about rendering this
  • I guess it was coming up every minute because I still had the editor open while watching the log, which made it try to render it every minute again.

Right. It’s referencing this.entity and not this.attributes.last_triggered as shown in your first post.