Template: access attributes of entity based on entity id

Hi,

I have an automation and in its action I want to use a template that refers to the event entity friendly name.

I can access the current state via the states() call via states(trigger.event.data.entity_id) (note: no quotes). Does a similar mechanism exist do retrieve the attributes for the given entity id?

Thanks,
Adriaan

After some more investigation I was able to access the attributes using states[trigger.event.data.entity_id.split('.')[0]][trigger.event.data.entity_id.split('.')[1]].attributes.

A nice addition would be to add an attributes() call to simplify this code.

Problem is your trying to squeeze this ijnto a single line:

{% set domain, id = trigger.event.data.entity_id.split('.') %}
{{ states[domain][id].attributes.friendly_name }}

Much easier to read.

Also, there is an attribute filter in jinja. But you would still need to split the trigger data to get the entity id and domain.

1 Like