Need help automating a template for multiple sensors/group -> TTS announcement

Hi all –

I have multiple zigbee water sensors (Aqara) and Sonos speakers around the house. My alert worked for a water leak, unfortunately it happened to be while I was sleeping so I didn’t catch it in time before damage occurred. Enter, my automation to announce a leak on my Sonos speakers.

I currently have an announcement working for one sensor as follows (configured in web interface)

when basement leak sensor -> on, then:

- call service
- script.sonos_say

sonos_entity: group.sonos
volume: 0.7
message: 'Alert, there is a leak in the *basement*'
delay: '00:00:05'

It works great, however, my question is, if I have a group of sensors (group.leak_sensors):

  • basment-leak
  • kitchen-leak
  • utility-leak

Is there a way to make the automation announcement message dynamic using the group (state = on) with templating to use the entity name or assigned area so I don’t need to make a separate automation for each sensor? Could also be applicable to other sensors in my house (like smoke CO)

I’m relatively knowledgable in Home Assistant, but not with template structure. Thanks!

- service: script.sonos_say
    sonos_entity: group.sonos
    volume: 0.7
    # trigger.to_state will get us a state object. 
    # object_id will be the 2nd part of the name (domain.object_id)
    # split that at the dash and take the first part (0)
    message: "Alert, there is a leak in the {{ trigger.to_state.object_id.split('-')[0] }} "
delay: '00:00:05'

@jocnnor that worked perfectly, thank you!