Automate multiple lights, but only the ones that are on

Hi all

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)

You can use a template to define the target of your light.turn_on service call:

{{ states.light | selectattr(‘state’, ‘eq’, ‘on’) | map(attribute = ‘entity_id’)| list }}

If you post the automation you have so far it will be easier to figure out how best to use the template or if it needs to be modified.

Have you considered using the Flux integration?

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.

1 Like

Thanks.

@Didgeridrew, this is the automation I’m using:

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.

@123 Gonna check out Flux, and see how it goes.

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.

@Didgeridrew thanks. But I can;t seem to get it to work.

I added this to my configuration.yaml:

# Group of lights
group:
  circadian_lights:
    name: Circadian Lights
    entities:
      - light.entrance
      - light.stairs
      - light.living
      - light.dining
      - light.stairs
      - light.computer

and modified the automation as so:

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

This shouldn’t be necessary, but try it with the lights template defining a variable…

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: '{{active_lights}}'
mode: single
variables:
  active_lights: |
    {{ expand('group.circadian_lights') | selectattr('state', 'eq', 'on') | map(attribute = 'entity_id') | list }}

EDITED to address typo in template

No, it didn’t work. Maybe my group declaration is bad?

I found the issue… group.circadian is spelled wrong in the light template.

Thanks. That did it.

I think that adaptive lighting might be more actively developed and fully featured. I love its take_over_control option.