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?

1 Like

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.

1 Like

I’m having the same issue. everything recent-ish seems to indicate the use of {{ trigger.name }} but that does nothing for me (older suggestions produce errors). I’ve also tried trigger.event.name and several other references. I am using the actual button to test. what am I missing?

alias: Button 2 - Double - Test
description: ""
triggers:
  - device_id: eee951759051736957595c248f34953c
    domain: zha
    type: remote_button_double_press
    subtype: button_2
    trigger: device
conditions: []
actions:
  - action: notify.persistent_notification
    metadata: {}
    data:
      message: "-{{ trigger.name }}-"
mode: single

Here’s the trace.

{
  "trace": {
    "last_step": "action/0",
    "run_id": "80418515c8fd38227f9e4f2e2467c8ae",
    "state": "stopped",
    "script_execution": "finished",
    "timestamp": {
      "start": "2025-08-03T21:10:14.770973+00:00",
      "finish": "2025-08-03T21:10:14.772063+00:00"
    },
    "domain": "automation",
    "item_id": "1754253664292",
    "trigger": "event 'zha_event'",
    "trace": {
      "trigger/0": [
        {
          "path": "trigger/0",
          "timestamp": "2025-08-03T21:10:14.771065+00:00",
          "changed_variables": {
            "this": {
              "entity_id": "automation.button_2_doublee_test",
              "state": "on",
              "attributes": {
                "id": "1754253664292",
                "last_triggered": "2025-08-03T21:09:56.011262+00:00",
                "mode": "single",
                "current": 0,
                "friendly_name": "Button 2 - Double - Test"
              },
              "last_changed": "2025-08-03T21:09:52.989303+00:00",
              "last_reported": "2025-08-03T21:09:56.012136+00:00",
              "last_updated": "2025-08-03T21:09:56.012136+00:00",
              "context": {
                "id": "01K1RWA35BE9WFHZWVY1GG89JW",
                "parent_id": "01K1RWA359YXCA6KTHE57K8BTK",
                "user_id": null
              }
            },
            "trigger": {
              "id": "0",
              "idx": "0",
              "alias": null,
              "platform": "device",
              "event": {
                "event_type": "zha_event",
                "data": {
                  "device_ieee": "6c:fd:22:ff:fe:e8:75:12",
                  "device_id": "eee951759051736957595c248f34953c",
                  "unique_id": "6c:fd:22:ff:fe:e8:75:12:2:0x0006",
                  "endpoint_id": 2,
                  "cluster_id": 6,
                  "command": "remote_button_double_press",
                  "args": [],
                  "params": {}
                },
                "origin": "LOCAL",
                "time_fired": "2025-08-03T21:10:14.769633+00:00",
                "context": {
                  "id": "01K1RWANFHVZ6M7Q4FA5WBHX6D",
                  "parent_id": null,
                  "user_id": null
                }
              },
              "description": "event 'zha_event'"
            }
          }
        }
      ],
      "action/0": [
        {
          "path": "action/0",
          "timestamp": "2025-08-03T21:10:14.771330+00:00",
          "changed_variables": {
            "context": {
              "id": "01K1RWANFJJVY0KMWP51DAY3QE",
              "parent_id": "01K1RWANFHVZ6M7Q4FA5WBHX6D",
              "user_id": null
            }
          },
          "result": {
            "params": {
              "domain": "notify",
              "service": "persistent_notification",
              "service_data": {
                "message": "--"
              },
              "target": {}
            },
            "running_script": false
          }
        }
      ]
    },
    "config": {
      "id": "1754253664292",
      "alias": "Button 2 - Double - Test",
      "description": "",
      "triggers": [
        {
          "device_id": "eee951759051736957595c248f34953c",
          "domain": "zha",
          "type": "remote_button_double_press",
          "subtype": "button_2",
          "trigger": "device"
        }
      ],
      "conditions": [],
      "actions": [
        {
          "action": "notify.persistent_notification",
          "metadata": {},
          "data": {
            "message": "-{{ trigger.name }}-"
          }
        }
      ],
      "mode": "single"
    },
    "blueprint_inputs": null,
    "context": {
      "id": "01K1RWANFJJVY0KMWP51DAY3QE",
      "parent_id": "01K1RWANFHVZ6M7Q4FA5WBHX6D",
      "user_id": null
    }
  },
  "logbookEntries": [
    {
      "name": "Button 2 - Double - Test",
      "message": "triggered by event 'zha_event'",
      "source": "event 'zha_event'",
      "entity_id": "automation.button_2_doublee_test",
      "context_id": "01K1RWANFJJVY0KMWP51DAY3QE",
      "domain": "automation",
      "when": 1754255414.771145
    }
  ]
}

{{ device_name(trigger.event.data.device_id) }}

1 Like

That worked! Thank you!

I’m not sure what to make of the clip you quoted though. I did look though a lot of documentation, including the Event documentation, but I’m still not seeing how I could have determined the reference trigger.event.data.device_id and I don’t even know where to look for the function. Is there an actual API somewhere? Any tips on how to better utilize the documentation?

I was just trying to show you where in the json of your Trace you could see the available trigger data for your trigger. If you look through it, you’ll see there is no key name under trigger, so there’s nothing for {{ trigger.name }} to return.

While the automation uses a Device trigger, those do not have their own section in the docs linked above because they just inherit them from whichever underlying trigger type they are actually using. In your case that would be an Event trigger.

The basic templating functions can be found in the Jinja docs; while Home Assistant-specific filters and functions for Jinja, like device_name(), can be found on the “Building Templates” page.

There is a REST API, though that is unrelated to this post.

Because HA integrates with so many things, the documentation is vast. The only way to approach it is one subject at a time and lots of repetition.

1 Like

Thanks a ton. This is very helpful.