Trigger.entity_id versus trigger.to_state.state not working

HI, using this trigger in an automation:

  - alias: Dorm Wifi off when Daughters not home
    id: 'Dorm Wifi off when Daughters not home'
#    initial_state: off
    trigger:
      platform: state
      entity_id: group.daughters
      to: 'not_home'
      for:
        minutes: 15

and this action service:

  - service: notify.notify
    data_template:
      message: >
        Dorm Wifi switched Off while 
        {{trigger.entity_id.attributes.friendly_name}} is {{trigger.to_state.state}}

results in this error:

Error rendering data template: UndefinedError: 'str object' has no attribute 'attributes'

while the group.daughters friendly_name is:

and

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

worked before.

why does this error out?
reading this doesnt help: https://www.home-assistant.io/docs/automation/templating/#template especially since the state object has the attributes friendly_name: https://www.home-assistant.io/docs/configuration/state_object/

trigger.entity_id does just return a string, a string does not have attributes.

You can use trigger.to_state.name to get what you want.
ref: https://www.home-assistant.io/docs/configuration/state_object/ state.name

or potentially state_attr(trigger.entity_id, 'friendly_name')

1 Like

thank you,

I see, and understand that now… sorry .

will try the latter, but need some mind bending why state_attr(trigger.entity_id, ‘friendly_name’) would work opposed to trigger.entity_id.attributes.friendly_name

state_attr() takes two strings, and does a lookup in the states, if it fail the lookup it returns None.

state_attr(string, string) is what you actually give it.
Since trigger.entity_id is a string we can use it here.

1 Like

thanks! noted.