How can I create a light group programmatically?

I have been trying to do the above, without succeeding so far. The best I managed was to create a group like this :


sequence:
  - service: group.set
    data:
      object_id: new_music_room_lights
      name: New Music Room Lights
      unique_id: new_music_room_lights_unique_id
      entities:
        - light.music_room_ceiling_bulb_1
        - light.music_room_ceiling_bulb_2
        - light.music_room_ceiling_bulb_3
        - light.music_room_ceiling_bulb_4
        - light.music_room_ceiling_bulb_5
        - light.music_room_track_bulb_1
        - light.music_room_track_bulb_2
        - light.music_room_track_bulb_3
        - light.music_room_track_bulb_4

It works, but the resulting entity is a group, not a light group. Also, the Home Assistant GUI complains that it doesn’t have a unique ID.

I’m simply trying to achieve the same result as I would through the GUI. Is this possible ?

Ultimately, the goal is to use templates and automatically create light groups for each area. But the first step before I move to that is to be able to create one that’s hard-coded like the above.

Go to Devices and Sevices and select Helpers

Click Create Helper

Select Group, then Light Group.

Add your entities

Or in configuration.yaml:

light:
  - platform: group
    name: "New Music Room Lights"
    unique_id: new_music_room_lights_unique_id
    entities:
      - light.music_room_ceiling_bulb_1
      - light.music_room_ceiling_bulb_2
      - light.music_room_ceiling_bulb_3
      - light.music_room_ceiling_bulb_4
      - light.music_room_ceiling_bulb_5
      - light.music_room_track_bulb_1
      - light.music_room_track_bulb_2
      - light.music_room_track_bulb_3
      - light.music_room_track_bulb_4

You can not template this list of entities.

1 Like

As far as I can tell you are not trying to “create a light group from YAML”, which I think most people would interpret as doing it through configuration.yaml. You are attempting to do it from an automation or script.

Looking at the documentation, the action you are using seems to only create what is now referred to as “old style groups”. There are no equivalent actions to create the newer type of groups.

Nice catch, his code does sync with an action.

Yes but they did say:

Yes, I know that part. But I have 200 Wiz light bulbs in about 25 areas. I’m hoping to find a way to create the groups without manually selecting each bulb in the GUI.

The idea would be to key in on a given area, enumerate all the light bulbs in it, and group them. What I had in mind was something like a blueprint, with maybe an instance for each room. Or even one that takes a list of areas and creates all the light groups. Maybe even generating the group name keying off area names. Not sure if any of this is possible programmatically.

I spent a heck of a lot of time over the last few weeks, in the Wiz app, entering all 200 bulb names in HA with the correct areas. And in Unifi as well. And in pfSense, for the DHCP reservations. It’s all been a very manual process. Creating the groups manually isn’t really that much work in comparison to all that’s already been done. I wish there was a more streamlined process to integrate all this, but everyone is using different bulb types, wireless technologies, routers, APs, etc. What works ok for a couple of devices really does not work that well with hundreds.
It would be pretty cool if I could have a way to sync device names between pfsense/unifi/home assistant. I took a look at the Home assistant REST API earlier yesterday, but didn’t find the pieces that I think are needed. PfSense has a third party API package which i use. Likewise for Unifi, its all third party.

1 Like

You don’t need to create groups to target an area of lights. If you have already spent the time to set up the areas you can just target the area with the action.

e.g.

- action: light.turn_on
  target:
    area: Bedroom
1 Like

Please help yourself and stop using your overly generic “in YAML” term. A script/automation and the configuration file configuration.yaml both use YAML as their container language, but that is where their likeness ends. They are completely different things with completely different possibilities and limitations.

So exactly what is it you want to do – create groups from a script/automation (only possible for old style groups), or find a way to generate/update configuration files?

The REST API is limited to the same actions available to automations and scripts. To extend what is possible, install a suitable integration, for example Spook which does allow for changing of entity names and ids among many other things.

HA in my opinion was designed for Home Automation with an emphasis on Home. It sounds like you are beyond that scope. The information you initially provided was limited and did not focus on your core issue. Expediting an extremely large group of devices via other methods.

This is for my home. It just happens to be a very large one.

I believe it can be templated, actually. I spent hours with chatgpt and gemini yesterday before posting, and they came up with YAML template code that included the intended list. I would have to go back to the long session and find out which version it was that had proper syntax. I wish these LLMs had version control, sigh.

However, the common thread with the simpler hard-coded version I posted here in my OP is that even with this template version, the target entity created was a generic group, not a light group. That’s the part I’m stuck on, dynamically creating a light group outside the GUI, not the template piece.

Thanks.

I did not know lights could be turned on or off that way, by area.

I still want these light groups for other functionality, though. The Wiz bulbs have many properties besides on/off. They have the ability to change colors, set effects, and dim. I often want to set them manually using the dashboard. Many rooms have a large number of bulbs. For example, my bedroom has 19 bulbs. My home theater has 21 bulbs. I don’t want to show all individual bulbs in my dashboard, as it would be impractical. Is there another way to accomplish this without light groups ? The generic groups unfortunately do not show the bulb properties I want to change.

I experimented with the auto entities card, in order to automate this without light groups.

views:
  - title: Home
    sections:
      - type: grid
        cards:
          - type: heading
            heading: Bulbs
            heading_style: title
          - type: custom:auto-entities
            card:
              type: entities
              title: WiZ Light Bulbs
              show_header_toggle: false
            filter:
              include:
                - domain: light
                  integration: wiz
            sort:
              method: name

This successfully listed all 200 Wiz smart bulbs in my home.

(note that some bulbs are offline due to being on vacancy sensors that automatically cut power)

I could probably narrow down the YAML to create one auto-entity card for each room, listing all the bulbs in each. I didn’t spend the time to do that yet, because this approach still has 3 problems :

  1. I don’t want to list individual bulbs on the dashboard, because there are so many of them. I want to show one entry per room.
  2. the auto-entity card lacks a global switch at the top to turn every member entity on/off . That switch exists in the regular entities card. Is there a way to add it ?
  3. there are also no global settings for changing colors, effects, or dimming at the card level, either in auto-entities or the entities card. These settings are however available on individual light group entities within an entities card.

Here is the UI result for a manually created light group, inside an Entities card. In this case, the sauna has no other smart devices, so there is just one entry in the card, but most rooms have other devices of different types.

The syntax for this card is :

type: entities
entities:
  - entity: light.sauna_lights
title: Sauna lights

I cannot view/edit the YAML corresponding to light.sauna_lights in the HA GUI . light.sauna_lights is a light group that I manually created in the GUI with hard-coded members. Here are screenshots showing how it was created :

I’m just trying to accomplish the exact same thing as shown in these last 2 screenshots, but programmatically instead of through the GUI. Even without using a template approach, and hard-coding the members, I cannot find a way to do so.

1 Like

Sorry about using the wrong terminology.

I’m not trying to manually edit any configuration files.

What I’m trying to do is to create light groups, identical to what’s possible through the HA GUI for “Helpers”, but programmatically.

First, I would like to create a light group using a hard-coded member list, just the same as in the GUI. But ultimately, I want to do the same using templates for member.

The approach I tried was to write a script (that happens to be written in YAML) to create the light group. But I have only succeeded in creating a generic group, not a light group.

It seems you are saying this is not possible. Is this an intentional limitation, or something that could be addressed in the future ?

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.)

1 Like

dominic,

Thanks for your reply !

That is unfortunate.

Thanks for this. I have been using HA for 3 years, and have yet to create a scene. There are many other features of HA I just haven’t used yet. While scenes could be helpful, I don’t think they are going to satisfy my requirements for manually changing color/effects per room from the dashboard, unfortunately.

The HA Wiz integration is fully local, as is the Matter integration that also works for some of them. There is delay on some bulbs vs others, especially as they roam between different APs, even while on the same circuit. I have analyzed the Wifi signal for all 200 bulbs in my Unifi controller, and forced those with the worst signal onto the AP where they got the best signal, which was often not their automatic choice. Even with 6 APs, some outdoor bulbs still have significant packet loss. I have seen ping times as high as 4 seconds for individual packets (not average). However, the commands eventually all go through, even though they are not synchronized. I’m not sure if there is a way to fix this besides adding more APs, especially outdoor APs. There is no network wiring in my house except between 2 rooms. 4 of the APs are meshed as a result. I may consider relocating some of the 4 Meshed indoor APs to improve the signal on the 2.4 GHz. I have already done a channel optimization, and played with AP signal strength.

Maybe if the Wiz bulbs ran NTP and the automation could give it a set time to execute the command. There would still be a delay, but at least they would all be synced. This is probably not possible without custom firmware for Wiz.

I have not tried Zigbee. I don’t have a Zigbee stick. I’m not sure it would work with the size and thick construction materials of my house. Even some of the most distant Z-Wave LR and Yolink devices still struggle in some locations.

The Wiz app does have a way to create groups. It can either save them locally on the Android device, or optionally in the cloud. I opted to create a cloud account so I could share the configuration with my husband while he was helping setup and test the bulbs. However, the HA Wiz integration does not have a cloud login option, so it cannot retrieve the groups from the Wiz app. All the light groups have to be re-created in HA. And that is actually, OK with me, as I prefer a fully local integration. It’s important for my lights to be able to run locally when the ISP is down. I perform testing that way, actually.

I chose not to buy into Hue to them using Zigbee or especially Bluetooth, which I think may struggle in my house, as well as the far higher cost per bulb, which makes a big difference for the quantity I’m using.

I can definitely create the 20 or so groups manually, after all the work I have done to setup the existing 200 bulbs. The manual creation certainly could have been done by hand by now, with all the time I spent in this thread. But manual creation is more error prone, especially when replacing devices, so I still see some value in being able to have a script to update the group. It would be more resilient to changes.

I’m not fully sure what you mean by this, but it is certainly possible to trigger scripts that you write yourself via the dashboard, as well as to pass them values that they can use to dynamically create the scenes.

But note also, you don’t even really need to use the scene.apply method here.

I bet that your integration supports light.turn_on getting a list of entity_ids, and that that list can be generated via a template.

What I mean is that I want to be able to use the following light control in the dashboard :

If I manually create a light group, and add it to the dashboard, as shown earlier with Sauna lights, I can then click the entity, and manually set all the properties from the dashboard using this GUI control. No scripts are needed, no value needs to be passed to it. I just want to create the light group automatically to add to my dashboard.

I’m not sure how it would work with scripts on the backend. I don’t know if this control can be shown without any associated entity. If so, I would still need to retrieve all the values, and pass them to a script, which I also would have to write. That seems like a lot of plumbing to perform, even if it could work.

Yes, the Wiz integration does support light.turn_on . And I believe the list can indeed be created by a template, as you say. But the issue is then, how can I show the group in the dashboard, with a visual GUI to set the color/brightness/effects.