Getting specific sensor/entity_id friendly name from group in automation?

I did a search but came up empty sadly. Basically- I have a group of water sensors. I simply want to be able to update the group itself (or rather use a template for it at some point) so the group is always updated with water sensors (or anything I decide to make a group of later- ie. window sensors, etc.)

I have an automation that watches that group state to turn to on. What I would like to do is get the specific sensor that is changing so I can put it in the notification.

Here is my automation:

alias: Sensors -- Water -- Send to ntfy
description: Notify via ntfy high priority that a water sensor detects water.
trigger:
  - platform: state
    entity_id:
      - binary_sensor.water_sensors
    to: "on"
    for:
      hours: 0
      minutes: 0
      seconds: 10
condition: []
action:
  - data:
      variables:
        emoji: droplet
        priority: high
        title: Water detected!
        message: >-
          Detected moisture at {{ trigger.to_state.attributes.friendly_name }}
    target:
      entity_id: script.ntfy_notification
    alias: ntfy- high
    action: script.turn_on
mode: single

The template simply outputs “Water sensors” as the friendly name (which makes sense I suppose.) Is there some way to get this info directly or do I need to do a template where I loop over every member of the water sensor group and then check for state “on” using jinja filters? Didn’t know if there was a simple solution that Home Assistant provides like the trigger.to_state kind of method.

Hi wallaby,

Use the trigger variables. It knows which one has triggered and will tell you there.

Hey @Sir_Goodenough
Thank you for the link. I think I have looked at that before but no matter what combo of trigger.xxxx I use, it seems to return simply the entity_id/friendly name of the group itself and not the actual device/entity_id that turned the group to state ‘on’.

For now I’ve hacked this together and it works but just wondering if it’s doable/better using some combo of built in state/attributes specific to the automation. Mostly because translating it to other automations would be easier than building a template out for each.

{{ label_entities('Water sensor') | expand() | selectattr('state','eq','on') | map(attribute='name') | list | join(', ') }}

trigger.entity_id
Would tell you the entity that triggered I would think.

Otherwise…
Look at a trace from one that ran and see that it says.
click the top circle in the flow chart, then changed variables.

trigger:
  id: '0'
  idx: '0'
...
  to_state:
    entity_id: sensor.gw2000b_v2_2_4_lightning_strike_distance

trigger.to_state.entity_id