It is not possible to create a light group âon the flyâ by executing some logic and computing the list of entities, as far as I know.
There is, however, the ability to generate scenes this way. The scene.apply action lets you specify the entity_ids and their various properties as a dictionary passed to the action with the entiy_ids being keys of a dictionary, with each value itself being a dictionary that specifies the parameters.
By itself, that doesnât do much more than a static YAML configuration that lists entities, which you donât want to do. But what the documenation doesnât explicitly say is that you can use a template to generate the dictionary that gets passed as the âentitiesâ parameter.
For example, hereâs a script that identifies those entities with entiity_ids in a starting list that are on, and calls scene.apply with a scene that increases the brightness of each light that is on by 50 (capping it at the maximum value of 254):
- action: scene.apply
data:
entities: >
{% set NS = namespace(entities={}) %}
{% for entity_id in ['light.den', 'light.hall', 'light.bedroom'] %}
{% if is_state(entity_id, 'on') %}
{% set int_brightness = min(254, state_attr(entity_id, 'brightness')|int + 50) %}
{% set NS.entities = dict(NS.entities, ** {entity_id : { 'state' : 'on',
'brightness': int_brightness }}) %}
{% endif %}
{% endfor %}
{{ NS.entities }}
Beware, though. This requires quite a bit of comfort with Jinja, and debugging this approach is probably going to prove pretty painful.
One thing I seriously suggest looking into before investing in this approach. I donât know anything about the bulbs youâre using, but Home Assistantâs groups and scenes feature is really just a way to create lists. It is very likely that by the time the integration that deals with the Wiz bulb gets a request that started as a request to do something to a Home Assistant group or a Home Assistant scene, it will be a series of requests, one per light. That means if youâre adjusting a lot of lights at once, you might find that they donât change state instantaneously, but rather over a period of time as the individual requests are processed by the integration and sent to the bulbs. This is probably more noticeable for Zigbee bulbs than Wifi ones.
Often, the manufacturer itself gives you a native way to create groups in that manufacturerâs ecosystem that will respond to a single request. For example, with Hue bulbs, rooms and zones created in the Hue app will appear as individual âlightâ entities, and scenes created in the Hue app appear as âscenesâ in Home Assistant. But these are not Home Assistant light groups or scenes; theyâre just products of the integration. So when a request to turn on a light group or to set a scene is issued to one of those entities, the integration can handle it as a single request for multiple bulbs, improving the user experience a lot by making the change happen instantaneously across the relevant lights.
So if youâre going to use HAâs groups, or scene.apply, you should first make sure that when youâre using these approaches with a large group of lights, like it seems youâll be doing, the performance is acceptable. Alternatively, check whether there is a way to create groups within the native app, and then whether the HA integration will know how to work with them. (Though, Iâm guessing that you specifically want to avoid managing groups by hand, so the native approach is probably out as a full solution. But if there are lights you often donât manipulate individually but rather in a group (like a room or an area of a room), you might at least be able to improve the responsiveness by setting up SOME native groups.)