Is it possible to print the relevant friendly entity/device name in a notification?

I’ve recently added some smoke detectors to my setup and I would like to be notified when their batteries are about to run out. I’ve set up a single automation with these rules:

- id: '1581432210567'
  alias: Smoke detector battery level notification
  description: Notify me when a smoke detector needs a battery change.
  trigger:
  - below: 10
    device_id: <irrelevant>
    domain: sensor
    entity_id: sensor.smoke_detector_hallway_battery
    platform: device
    type: battery_level
  - below: 10
    device_id: <irrelevant>
    domain: sensor
    entity_id: sensor.smoke_detector_hobby_room_battery
    platform: device
    type: battery_level
  condition: []
  action:
  - data_template:
      message: Replace batteries for {{ trigger.entity_id }}.
      title: Smoke detector battery level below {{ trigger.below }}%
    service: notify.notify

As you can see I have two different triggers, one for each smoke detector. I want to be notified if either one of them triggers but I want to include the name of the entity that triggered the notification as well. The setup above works, but it shows the ID rather than its corresponding human-friendly alias. Is there any way for me to show the friendlier text? I read the documentation of course, which seems to imply that it’s impossible but I wanted to make sure.

Also, is there a way to make this automation trigger on all battery_level events or do I have to manually add every new device I get that reports a battery status?

3 Likes

{{ trigger.to_state.attributes.friendly_name }}

5 Likes

Half the battle is knowing which documentation you need to read, I swear!

You’ll want {{trigger.name}} to get the friendly name. Or, attributes.friendly_name would work, but “trigger.name” will fall back to the entity_id if friendly_name doesn’t exist, so it’s safer.

Thanks, @tom_l and @jocnnor! I saw the state object in passing but didn’t think the state would be the property I’d need as it’s stateless data. But trigger.to_state.attributes.friendly_name works beautifully, thanks. :slight_smile:

trigger.name doesn’t seem to work, using that results in an empty space in the notification. But that’s fine, I’ll just use the friendly_name instead.

Thanks a lot for linking the thread about making a generic way to do this. I tried finding a solution for this but I couldn’t find anything, I probably used search terms that were too generic.