I’d like to use the template system to create various groups based on the attributes of different devices.
For example:
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.
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).
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:
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%}
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.
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.
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.
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).