Automation that tells you which entity triggered it

So I have made an Automation that sends a Telegram message when one of my battery powered devices report a low battery.

how can I make the message say which device exactly triggered it?

That’s what I have right now:

- id: 'batteriealarm'
 alias: Batteriealarm
 trigger:
   - platform: numeric_state
     entity_id:
       - sensor.aqara_thp_01_battery
       - sensor.aqara_thp_02_battery
       - sensor.door_contact_01_battery
       - sensor.door_contact_02_battery
       - sensor.aqara_wandschalter_01_battery
       - sensor.aqara_wandschalter_02_battery
       - sensor.aqara_wandschalter_03_battery
       - sensor.aqara_wandschalter_04_battery
       - sensor.aqara_wandschalter_05_battery
       - sensor.aqara_wandschalter_06_battery
       - sensor.aqara_button_01_battery
       - sensor.aqara_button_02_battery
     below: 80
 action:
 - service: telegram_bot.send_message
   data:
     disable_notification: false
     target: mytelegramid
     message: '*Battery empty: xxx*' #replace xxx with entity_id that triggered the Automation

So if for example ‘sensor.aqara_button_02_battery’ reports 74, the message should say “Battery empty: sensor.aqara_button_02_battery

Is there a simple way to do that?

Additionally it would be cool if it also adds the percentage (74% in this case)

1 Like

I was just looking at another thread yesterday, for telegram automations, and I think I saw the kind of thing you are looking for.

In the threads linked below, they have multiple entity IDs triggering a single automation, but want to say in a notification the name of the entity that triggered it. They used the template {{trigger.entity_id}} to specify this.

I haven’t tried it myself, but I think this might point you in the right direction.

(And another thread, also with notification based on the trigger’s entity ID)

1 Like

this was exactly what I needed!
Thanks

The action now looks like this and works as expected

  action:
    - service: telegram_bot.send_message
      data_template:
        target: 'mytelegramid'
        message: '*Battery empty: {{trigger.entity_id}}*'

An additional question - is there a way to not write out the entity_id, but the name of that entity?

So it doesn’t say: “ Battery empty: sensor.aqara_button_02_battery ” but “ Battery empty: Doorbell

2 Likes
{{ trigger.to_state.attributes.friendly_name }}
3 Likes

this just gives out the entity_id :thinking:

2 Likes

Does your entity have a friendly_name set?

oh - of course… I’m an idiot… the button does have a friendly name, but the “battery entity” did not :slight_smile:

Now it works

1 Like

I just set my smoke alarms off to make sure :slight_smile:

4 Likes
trigger.to_state.name

The State Object has several properties including name

Name of the entity. Based on friendly_name attribute with fall back to object ID. Example: Kitchen Ceiling .

Notice that if the entity has no friendly_name attribute, it falls back to using the object ID.

climate_hall_heat_on: Error executing script. Error rendering template for call_service at pos 6: UndefinedError: 'trigger' is undefined

  trigger:
    - platform: numeric_state
      entity_id: sensor.temperature_158d00019cf466
      below: 22
    - platform: numeric_state
      entity_id: sensor.savednipro_temperature
      below: 17
    - platform: state
      entity_id: input_boolean.heat_hall
      to: 'on'
- service: notify.mobile_app_motorola_one_action

      data:

        title: "Home Assistant"

        message: "Запущено {{trigger.to_state.name}}"
1 Like