Dynamic group of entities based on name pattern (e.g. ends with _water_leak)

Title: Dynamic group of entities based on name pattern (e.g. ends with _water_leak)

Body:

Hi everyone! :wave:

I’d love to see support in Home Assistant for creating dynamic groups based on entity ID patterns, like all binary_sensor entities that end with _water_leak.

:bulb: Why this feature is useful

Many sensors (especially from Zigbee2MQTT) follow a consistent naming scheme. In larger setups (20–50+ devices), it’s a hassle to keep manually updating group: or trigger.entity_id lists every time a new sensor is added.

For example, I have dozens of leak sensors named like:

binary_sensor.kitchen_water_leak  
binary_sensor.bathroom_water_leak  
binary_sensor.garage_water_leak  
...etc.

If I could automatically group these by pattern (_water_leak), it would save tons of time and reduce human error.

:white_check_mark: What I imagine

A simple group like:

group:
  water_leak_sensors:
    platform: dynamic
    domain: binary_sensor
    match: '_water_leak$'  # regex or endswith-style filter

Or maybe a template-driven group:

group:
  auto_leak_sensors:
    entities: >
      {{ states.binary_sensor
           | selectattr('entity_id', 'match', '_water_leak$')
           | map(attribute='entity_id')
           | list }}

Bonus points if this group can be reloaded dynamically or via automation/script!

:arrows_counterclockwise: Workarounds today

Right now I have to:

  • Manually list all matching entities in the group:
  • Or manually paste them into automations every time something changes
  • Or write a helper automation to update input_text and parse that

…but none of these truly replace a real, dynamic group.


Thanks for considering! :pray: I’d love to hear if others also face this need.

Don’t forget to vote for your own Feature Request. :grin:

  • Using labels which are designed to do exactly this. Labels
  • Using Templates which can do this and SO much more.

I use this script to create a leak sensor group, and I have an automation to run the script every time HA restarts (because the group will disappear). I think this is doing exactly what you want to do: it creates a group based on matching entity_id’s

It could all be done in the automation but I split it into a script so that I can easily run it after I mess with my leak sensors. Which I had been doing quite often until I found ones I like.

alias: Leak Sensor Group Creation
sequence:
  - action: group.set
    data_template:
      object_id: leak_sensors
      entities: >
        {{ (states.binary_sensor |
        selectattr('entity_id','search','_leak$|_leak_detected$')) |
        map(attribute='entity_id') | join(', ') }}
mode: restart
icon: mdi:water-alert