Group Templates

I’d like to use the template system to create various groups based on the attributes of different devices.

For example:

  1. A group called “Main Floor Lights”. I could choose what rooms to include and the system would automatically include any lights that are assigned to those rooms.
  2. A group called dimmable lights. I could assign it to include only lights with the dimmable attribute.

I’m sure I could think of other examples too but I think you probably get the idea. The goal is to install a light, assign it to a room and have it automatically included in the group and to be able to remove a light from the group when I change it to a different room.

I’d allow this for other domains too (such as switches and media players).

You can already do this with the group.set service and a template via an automation.

Via an automation?

When and how often would it need to run then? How would it get updated when I add or remove a device or move it from one location to another?

I make heavy use of auto-generated groups (rooms, binary sensors, helpers etc.).
You could use a time pattern trigger for example. I’m using the state change of a sensor.
This is my automation:


automation:

- id: auto_gruppen
  alias: Automatische Gruppen
  mode: restart

  trigger:
  - platform: event
    event_type: homeassistant_start_delay
  - platform: state
    entity_id:
    - sensor.aktive_gerate_alle
    - sensor.nicht_erreichbar

  action:
  - service: script.auto_gruppen

Depending on your use case, another option is to take advantage of template macros as constants to dynamically create the list of state objects or entity IDs.

# dynamic_groups.jinja
{% set kitchen_lights = (states.light | map(attribute='entity_id'))
| select('in', area_entities('Kitchen')) | list %}

{% set lr_lights = (states.light | map(attribute='entity_id'))
| select('in', area_entities('Living Room')) | list %}

{% set main_floor_lights = kitchen_lights + lr_lights%}
sequence:
  - service: light.turn_on
    target:
      entity_id: |
        {% from 'dynamic_groups.jinja' import main_floor_lights %}
        {{ main_floor_lights }}

That sounds like a viable option. I’ll dig into that in the near future. Thanks!

Based on a trigger, any trigger you want. Including restarting yaml or even a button press.

I know how to trigger based on HA rebooting. But I haven’t found a trigger for “device deleted”, “device added” or “device renamed”. I would need such an automation to run following each of these four events.

1 Like

You don’t need any of that. You can just trigger off state changes that fit your target domain(s) and update the list of entities only when it changes. That’s what I do.

The issue with groups created via group.set service is that they will appear under group domain rather than a more specific one like light which then cant be entered in fields that filter for light entities so this alternative doesn’t quite solve the issue.
If you manually create a light group from helper menu however then they do, so I think this feature request is still viable unless there’s some way to also programatically set the group’s domain.

1 Like

Couldn’t agree more with @Ninetou. Allowing to set light groups in the light domain with a unique_id allows to manage this group using the UI so you can assign it to areas and labels which goes a long way in automating your dashboards and organizing your entities.

@Didgeridrew is that “dynamic_groups.jinja” just a flat file in your config directory? an you can call it from any automation ?!

It needs to be in config/custom_templates like custom macros, and you can call it from anywhere that templates are fully allowed (AFAIK it won’t work in Limited Template situations).

that is pretty amazing, thank you