Trigger object templating

Hi,

I’m trying to create some automations which use the trigger.to_state objects. I’m trying to use templates like the following, buth they return “none”.

  • {{ state_attr(‘trigger.to_state’, ‘friendly_name’) }}
  • {{ states(‘trigger.to_state’) }}

If I write it like this, it does work, but I’d like to use the template extensions.

  • {{ trigger.to_state.attributes.friendly_name }}
  • {{ trigger.to_state.state }}

You really don’t need to use the state_attr and states functions here. The point in using them is to handle the situation when the entity or attribute doesn’t exist. But that won’t (or generally won’t) be the case here. (I think the only case where trigger.to_state will not have a valid state object is when the entity is being removed, in which case trigger.to_state will be None.)

But if you really want to, you could use trigger.entity_id, e.g., {{ states(trigger.entity_id) }}.

Note that putting quotes around trigger... just turns it into a string. You want to use it as a variable, which it is.

Also note that trigger.to_state.name is a shortcut for trigger.to_state.attributes.friendly_name, or trigger.to_state.entity_id if the entity doesn’t have a friendly_name.

2 Likes

Thank you for explaining! Clears things up a lot :grinning: