Getting friendly name of entity_id that triggered automation

Hi all!

I’ve been thinking of creating an automation that triggers whenever there is a state change in any of my lights and in the action I want to get the friendly_name of the light that changed and therefore triggered the automation.

Is this possible with HA?

Thanks!

appdaemon 3.0 will do that in a couple of lines. I use it for a couple of leak sensors to send alerts if grampy leave the taps on

import appdaemon.plugins.hass.hassapi as hass
class Leakwarning(hass.Hass):
    def initialize(self):
        self.listen_state(self.leakwarning,"binary_sensor.water_leak_sensor_158d0001c3472b",new="on")
        self.listen_state(self.leakwarning,"binary_sensor.water_leak_sensor_158d0001c347c9",new="on")
    def leakwarning (self, entity, attribute, old, new, kwargs):
        mess= "Leak Detected by " + self.friendly_name(entity)
        self.log(mess)
        self.notify(mess, title = "Leak Warning", name = "notify.gmail_alert")
        self.call_service("persistent_notification/create", message = mess, title = "Leak Warning")

Appdeamon listen_state can listen for all objects or a domain of objects or a single object.

Listen for any state change and return the state attribute

self.handle = self.listen_state(self.my_callback)

Listen for any state change involving a light and return the state attribute

self.handle = self.listen_state(self.my_callback, “light”)

Listen for a state change involving light.office1 and return the state attribute

self.handle = self.listen_state(self.my_callback, “light.office_1”)

Hope this helps

Use this template

{{ trigger.to_state.attributes.friendly_name }}
1 Like

as NotoriousBDG said, use something similar to this in your action:

  - service: tts.google_say
    entity_id: media_player.kitchen
    data_template:
      message: Welcome home, {{ trigger.to_state.attributes.friendly_name }}!
1 Like

Thanks for the replies! I tried using the template but it did not really work. I will give you the full automation tomorrow.

here my 5 cents

- action:
  - data_template:
      message: ' {{ trigger.to_state.attributes.friendly_name }} was {{ trigger.to_state.state
        }} '