Insert entity friendly name into notification

I trying to get the name of the entity that triggers the below automation into the message bod, without success.

All works fine if I just put plain text in the message.

What am I missing here?

- alias: Notify pump state is clear
  trigger:
    - platform: state
      entity_id: sensor.balance_pump_state
      to: Clear
    - platform: state
      entity_id: sensor.main_pump_state
      to: Clear
    - platform: state
      entity_id: sensor.waterfall_pump_state
      to: Clear
  action:
    - service: notify.mobile_app_ash_iphone
      data:
        message: "{{ state_attr(trigger.event.data.entity_id, 'friendly_name') }}"
        title: Koi pond
    - service: notify.lounge_tv
      data:
        message: "{{ state_attr(trigger.event.data.entity_id, 'friendly_name') }}"
        title: Koi pond
  mode: single

You’re making it a more complicated than it is… :slight_smile:

- alias: Notify pump state is clear
  trigger:
    - platform: state
      entity_id: sensor.balance_pump_state
      to: Clear
    - platform: state
      entity_id: sensor.main_pump_state
      to: Clear
    - platform: state
      entity_id: sensor.waterfall_pump_state
      to: Clear
  action:
    - service: notify.mobile_app_ash_iphone
      data:
        message: "{{ trigger.name }}"
        title: Koi pond
    - service: notify.lounge_tv
      data:
        message: "{{ trigger.name }}"
        title: Koi pond
  mode: single

trigger.name will return the “friendly name” if one has been assigned and otherwise return the entity_id.

Thank you!

or use:

      message: "{{ state_attr(trigger.entity_id, 'friendly_name') }}"

if you want the friendly name

5 Likes

@ghassan A lot of thanks, workme perfectly :slight_smile:

The above suggestions are not working here. The notifications are being triggered from an event being fired and I have tried the following code:

service: notify.all
data:
  message: >-
    {{ state_attr(trigger.entity_id, 'friendly_name') }} detected {{ trigger.event.data.name
    }} with confidence {{ trigger.event.data.confidence }}

Which results in no notification being sent and an error in the log.

Template variable error: ‘dict object’ has no attribute ‘entity_id’ when rendering ‘{{ state_attr(trigger.entity_id, ‘friendly_name’) }} detected {{ trigger.event.data.name }} with confidence {{ trigger.event.data.confidence }}’

service: notify.all
data:
  message: >-
    {{ trigger.name }} detected {{ trigger.event.data.name
    }} with confidence {{ trigger.event.data.confidence }}

Which results in the notification being displayed only from the word ‘detected’ onwards.

Any ideas?

Hi @Cergon
The original code works if the trigger is entity state change, then you can operate on the related entity and fetch its friendly name etc.

In your case it is an event.

Could you please add more details on what kind of event it is, and what kind of information you would like to extract and include in your message from it or some how related entity

I’m still not getting the trigger name or door name in the automation action notification:

action: notify.mobile_app_pixel_9_pro_xl
metadata: {}
data:
  data:
    channel: door
  message: "\"{{ trigger.name }}\" opened"

Any advice would be greatly appreciated.

Too many quotation marks.

action: notify.mobile_app_pixel_9_pro_xl
metadata: {}
data:
  data:
    channel: door
  message: "{{ trigger.name }} opened"

Be aware that attempting to test actions that use the trigger variable using the “Run” command or the automation.trigger action will fail, you need a real trigger event to populate the variable.

1 Like

The brief formatted code I showed earlier was what was the visual editor produced. I remove the extra quotes and the notification still doesn’t include the trigger name or entity name. It just says opened

This is my automation:

alias: "Door opened chime "
description: ""
triggers:
  - trigger: state
    entity_id:
      - binary_sensor.slider_door_opening
      - binary_sensor.front_door_opening
      - binary_sensor.garage_mudroom_door_opening
    to: "on"
    from: "off"
conditions: []
actions:
  - action: notify.mobile_app_pixel_9_pro_xl
    metadata: {}
    data:
      data:
        channel: door
      message: "{{ trigger.name }} opened"
mode: single
 

How are you testing the automation?

Actual triggers in the automation, for example a door contact sensor opening.

Download the trace json file from a failed run of the automation and share it here so we can see what’s going on.