Working with the trigger object

Hello,

I have an automation like that:

trigger:
  - platform: state
    entity_id: person.florian
action:
  - service: notify.mobile_app_handy_florian
    data:
      message: >-
        {{ state_attr('trigger.entity_id', 'friendly_name') }} ist jetzt {{
        states('trigger.entity_id') }}

I tried some variations of the message but never got anything out but “None ist jetzt unknown”, what is want is something like “Florian ist jetzt home”.

Tried also:

  message: >-
    {{ state_attr('trigger.to_state', 'friendly_name') }} ist jetzt {{
    states('trigger.to_state') }}

and more…

How can achieve that message?
Thanks!

You can access those two pieces of information with the name and to_state.state trigger variables.

trigger:
  - platform: state
    entity_id: person.florian
action:
  - service: notify.mobile_app_handy_florian
    data:
      message: >-
        {{ trigger.name }} ist jetzt {{ trigger.to_state.state }}

For reference, you use trigger variables like other variables, they shouldn’t be put in quotes. When you put them in quotes they are rendered as a string so the stored value of the vriable isn’t available to your template.

If you were to use them in a template like you posted above it should be as follows:

message: >-
        {{ state_attr(trigger.entity_id, 'friendly_name') }} ist jetzt {{
        states(trigger.entity_id) }}
2 Likes

Thanks! The second variant worked for me. The first, {{ trigger.name }} returned an empty string.

Strange… trigger.name should render the friendly_name if one is assigned and fall back on the entity_id if one isn’t assigned. Another method is to get the friendly name is: {{ trigger.to_state.attributes.friendly_name }}.