I’m new to HA, and Im trying to get a group of lights to change the light color during the day. So at noon lights turn to 5000k, at 3pm to 4400k, and so on.
The thing is that I only want the lights that are on to change color, and ignore the lights that are off.
So far I figured out that if i add a condition to check if a ligth is on, then change the color, it works. But I have to do an automation for each individual light. Which is kind of difficult to mantain.
Is there a way to make one automation that would check each light and execute the light change if the condition for said light is met?
Something like:
for each light in groupOfLights {
if (light is on)
light.chageColor(newColor);
}
(I know this pseudocode is not how HA work, but it’s the best I can think of to explain it)
The flux switch platform will change the temperature of your lights similar to the way flux works on your computer, using circadian rhythm. They will be bright during the day, and gradually fade to a red/orange at night. The flux switch restores its last state after startup.
alias: Sun Color Stairs
description: 'Changes the color of light to match the sun'
trigger:
- platform: time_pattern
minutes: /5
condition:
- condition: device
type: is_on
device_id: ce9d5c7c1e7e9b4729f23ef5acd1dc07
entity_id: light.stairs
domain: light
action:
- service: light.turn_on
data_template:
color_temp: |
{{ (states('sensor.circadian_light') | int) }}
target:
entity_id:
- light.stairs
- light.living
- light.dining
- light.stairs
- light.computer
mode: single
The problem is that I’m only checking for light.stairs to be on, and if it is, then all the other lights turn on and change color. I guess I could do this 5 times, one for each light, but I don’t think that’s ideal.
Alright, the template I posted before will check all light entities in your Home Assistant instance.
To limit this automation to just 5 lights, the best thing to do is to put them in a group. You can then use the group as your condition (groups are “on” if at least one entity from the group is “on”, and “off” only when all entities are “off”). You can also use a template to extract which lights from the group are “on” so that those lights are targeted by the service call to change their color temp.
alias: Sun Color Stairs
description: 'Changes the color of light to match the sun'
trigger:
- platform: time_pattern
minutes: /5
condition:
- condition: state
entity_id: group.circadian_lights
state: 'on'
action:
- service: light.turn_on
data:
color_temp: |
{{ (states('sensor.circadian_light') | int) }}
target:
entity_id: |
{{ expand('group.cicadian_lights') | selectattr(‘state’, ‘eq’, ‘on’) | map(attribute = ‘entity_id’) | list }}
mode: single
If, later on, you want to have more lights controlled by this automation you just need to add them to the group.
alias: Sun Color Stairs
description: 'Changes the color of light to match the sun'
trigger:
- platform: time_pattern
minutes: /5
condition:
- condition: state
entity_id: group.circadian_lights
state: 'on'
action:
- service: light.turn_on
data_template:
color_temp: |
{{ (states('sensor.circadian_light') | int) }}
target:
entity_id: |
{{ expand('group.cicadian_lights') | selectattr('state', 'eq', 'on') | map(attribute = 'entity_id') | list }}
mode: single
But the automation doesn’t do anything.
If I change the entity_id to a light, it does, so my guess is there’s something in the expand gropup or the selectattr