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
.
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
did you figure this out? im testing myself but cant get it to work
Yes, tobi-bo example and code works perfect. You have to use a group (not a light group) to make this work. Good luck!
got the two first parts… but when im doing the script i get
“Message malformed: extra keys not allowed @ data[‘sequence’][0][‘set_brightness’]”
Not sure if you are trying to achieve the same. I use the template for changing the brightness of the group for the lights that are actually on.
In my conf.yaml I have the following template under light:
- platform: template
lights:
gang_on:
friendly_name: "Gang On"
level_template: >
{%- set data = namespace(level=0) -%}
{% for s in state_attr('group.gang', '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.gang', 'entity_id') %}
{% if is_state(s, 'on') %}
{%- set data.res = "on" -%}
{% endif %}
{%- endfor -%}
{{data.res}}
turn_on:
- service: light.turn_on
data:
entity_id: group.gang
turn_off:
- service: light.turn_off
data:
entity_id: group.gang
set_level:
- service: light.turn_on
target:
# wat te doen als alles uit staat? If statement om alles aan te zetten
entity_id: "{{ expand('group.gang') | selectattr('state','eq','on') | map(attribute='entity_id') | join(',') }}"
data:
brightness: "{{ brightness }}"
And a group defined in group.yaml
gang:
name: Gang
entities:
- light.dimmer_bg_hal
- light.dimmer_bg_gang
- light.dimmer_1v_gang
- light.dimmer_2v_gang
As said, you need a group and not a light group. I am not an expert, but this works for me.
Cheers!
Thank you but still no success,
i have the template in config.yaml figured out, and also got the group in group.yaml
but its the script that i get an error from…
maybe i have missunderstood how i should connect the template to the actual dimmer?
I would expect this to work but don’t have the code to help you. I have not linked it to an actual dimmer but are just using it on my mobile (in HA)
And how you control it from HA? Light entity?
yes, the template creates a group (in my example light.gang_on) and you can control it with a slider / dimmer. There is one issue with the template, if everything is off… not sure how to resolve.
Ah yes…that actually works! Thank you! Will have to try more to get it with a actual dimmer. Thank you
I habe the same question with controlling the lights which are currently ON.
I understand that there is an existing solution as described above. But this was all written before the changes of group behaviour in HA release 2022.4. So I tried to find something easier. I added the template mentioned above which gives me a list of all lights which are currently on in the script to „set group“. Unfortunately template seems to be not accepted here.
Does anyone have a more simple Solution than described above by using the new Features implemented in R2022.4?
im also looking for a reliable solution