Light group for lights that are already on

Hi,

I am new to Home Assistant and I have the following challenge:

I want to have a control for all my lights, that are already on. For this I want to use a simple light card. With “controlling” the lights, I mean turn on/off, set brightness and color temperature.

I have already found a solution, to control multiple lights with one light card, by creating a Light Group and adding the lights to this group.

Unfortunately, when I change the brightness, all lights in that group are turned on and I found information, that this behaviour is normal. But as mentioned before: I only want to change the brightness for lights, that are already on, while leaving the others off. So I think I have to find a way, to create a dynamic Light Group with all my lights, that are turned on.

After some researching, I found the possitility to create templates. So I thought the solution for my problem would be, to put a template inside my Light Group definition:

light:
  - platform: group
    name: Turned on lights
    entities: >
      {{ (states.light|selectattr('state','eq','off')|map(attribute='entity_id')|list)|join(',') }}

Unfortunately this does not work.

So here my questions:

Is this the correct way to solve my problem, or is there another (simpler) solution for this? If I am on the correct way: What am I doing wrong?

I hope someone can help me, I did a lot of research, but found no solution for my issue. Thank you in advance for your answers.

You can not use templates there.

You can use that template in an automation to change the brightness.

Thank you for your answer. Now that I know, that it will not work with this approach, could you please give me a hint, how I can achieve the desired fuctionality?

Create an input number that goes from 0 to 255, with a step of 1.

Trigger on any change of the state of the input number (no to or from).

Action service is light.turn_on, with your template to select the entities and another template to set the brightness from the input number.

Okay, thank you for the description.

I don’t know if I understood you right, but there are a few issues, I see:

  • how can I properly represent the brightness value of that group, if the brightness is changed outside of home assistant?
  • adding color values would make things lot more complicated, i think.
  • how can I get this into a lovelace “light” card?

As far as I understood, a light group would be a simple way to go, because it processes all the states and capabilities of the associated lights and congregates them into one entity. This entity can be used as one light, without defining additional services and / or automations.

Is there really no way to dynamically create and modify a light group?

I believe you may have misunderstood the purpose of a Light Group. It creates a virtual light entity to provide control over a fixed group of light entities. Although each member of the Light Group can continue to be controlled independently, its state influences the Light Group’s state. In other words, if only one member of the Light Group is on, the Light Group’s state is also on.

You won’t be able to do that with a Light Group. You will have to control each member individually, as described by tom_I.

Thank you for your reply.

Yes, it seems like I misunderstood the purpose of a Light Group and also for the standard Group, if there is no way to modify its members while the system is running. I simply did not take into account, that such a basic thing like is not possible.

I simply did not find any word on where it is possible to use Templates and where it is not possible, so I was convinced, that it works basically in every place.

Sorry for the dumb question and thank you all for the clarification, I will try to make it the way, you described before.

If you already have a Light Group (defined with a fixed list of light entities), a template can use it to determine which of its members is currently on or off.

For example, if your Light Group is called light.first_floor then this template will select all members that are off and present them as a comma-delimited string of entity_ids.

{{ expand(state_attr('light.first_floor', 'entity_id')) | selectattr('state','eq','off') | map(attribute='entity_id') | join(',') }}

Hi, I think one solution could be a template light, which handle the “group” internally. But there are some questions you need to think about:

  • What is the current brightness level of the group entity, when not all lights in the group have the same brightness?
  • The state of the group light will be ‘on’ when any of the lights is on. But what happens, when no light is on, so the group light is off, and then its turned on? At this moment there are no lights in the group, because none is on.

Anyway, this is a solution, which works sort of. First, make a group of all lights you possibly want to control, when they are on.

all_lights:
  name: All Lights
  entities:
    - light.one_light
    - light.another_light
    - ...

Then create a template light, which handles the states:

  • Light is on, when any of the lights is on
  • Brightness level is the maximum of brightness level from all lights in the group (maybe average is better?).
  • Turn off just turns the group off.
  • Set level calls a script, which sets the level of all lights in the group, that are on.
light:
  - platform: template
    lights:
      on_lights:
        friendly_name: "On Lights"
        level_template: >
          {%- set data = namespace(level=0) -%}
          {% for s in state_attr('group.all_lights', 'entity_id') %}
            {% if is_state(s, 'on') %}
              {%- set data.level = [state_attr(s, "brightness"), data.level]|max -%}
            {% endif %}
          {%- endfor -%}
          {{data.level}}
        value_template: >
          {%- set data = namespace(res="off") -%}
          {% for s in state_attr('group.all_lights', 'entity_id') %}
            {% if is_state(s, 'on') %}
              {%- set data.res = "on" -%}
            {% endif %}
          {%- endfor -%}
          {{data.res}}
        turn_on:
          # what to do if a group with all "on" lights is off and turned on then?
        turn_off:
          - service: light.turn_off
            data:
              entity_id: group.all_lights
        set_level:
          - service: script.set_brightness
            data:
              brightness: "{{ brightness }}"

And here is the script:

set_brightness:
  sequence:
    - repeat:
        count: "{{ state_attr('group.all_lights', 'entity_id')|length }}"
        sequence:
          - choose:
              - alias: "light on"
                conditions: "{{ states(state_attr('group.all_lights', 'entity_id')[repeat.index - 1]) == 'on' }}"
                sequence:
                  - service: light.turn_on
                    data_template:
                      entity_id: "{{ state_attr('group.all_lights', 'entity_id')[repeat.index - 1] }}"
                      brightness: "{{ brightness }}"

The script can be replaced with a service call that uses a slightly modified version of the template I posted.

        set_level:
          - service: light.turn_on
            target:
              entity_id: "{{ expand('group.all_lights') | selectattr('state','eq','on') | map(attribute='entity_id') | join(',') }}"
            data:
              brightness: '{{ brightness }}'

A similar template can replace the for-loop used in level_template and value_template.

1 Like

This is exactly what I am looking for. Can anybody share the final code used for this template so that I can adjust the brightness of the lights (in a group) that are turned on? Thanks