How to write an automation to shift any lights that are on to soft white at sunset?

Hello, just set up my first home assistant hub and trying to script my first automation to start learning the syntax.

My ideal end goal is to have an automation that goes through each light in my office at sunset, if the light is on and has a temperature above 4000K it would then shift it to 2700/3000K.

So far, playing with one light only, I have not got it to read the current color, and only got it to turn off or set a scene. I would be okay if I can’t read the current color, but do want to keep it from turning on every light if possible, just shifting the ones that are on.

Any tips or pointers would be great, once I understand how these commands work I feel I will be off to the races. Thank you for any help/guidance!

My current state:

alias: Testing
description: ""
trigger:
  - platform: sun
    event: sunset
    offset: 0
condition:
  - condition: state
    entity_id: light.office_ceiling_one
    attribute: color_temp_kelvin
    state: "4000"
action:
  - type: turn_off
    device_id: 81ce27441da00bff19c0e3b5c04ee291
    entity_id: da42717b8189669ea21db5d2c65bb017
    domain: light
  - service: scene.turn_on
    target:
      entity_id: scene.office_cozy
    metadata: {}
mode: single

The following example assumes all the lights in your office have been assigned to an area named Office. If you haven’t done that then let me know and I can show you how to modify the automation accordingly.

alias: Testing
description: ""
trigger:
  - platform: sun
    event: sunset
condition: []
action:
  - variables:
      lights: >
        {{ expand(area_entities('Office') | select('match', 'light'))
          | selectattr('state', 'eq', 'on')
          | selectattr('attributes.color_temp_kelvin', 'defined')
          | selectattr('attributes.color_temp_kelvin', 'ge', 4000)
          | map(attribute='entity_id')  | list }}
  - condition: '{{ lights | count > 0 }}'
  - service: light.turn_on
    target:
      entity_id: '{{ lights }}'
    data:
      color_temp_kelvin: 2700
mode: single

The variable named lights creates a list of entity_ids for all lights in the Office area that are on and have a color temperature greater than or equal to 4000. If there are none then the automation exits. If there’s one or more, the automation sets their color temperature to 2700.

1 Like

This is what I use:

      - variables:
          daylight_light_list: >-
            {{ states.light|selectattr('state','==','on')|selectattr('attributes.supported_color_modes','contains','color_temp')|selectattr('attributes.color_mode','eq','color_temp')|map(attribute='entity_id')|list }}
          daylight_light_count: "{{ daylight_light_list|count }}"
      - if:
          - condition: template
            value_template: "{{ daylight_light_count > 0 }}"
        then:
          - service: light.turn_on
            data:
              transition: 10
              kelvin: 2200
            target:
              entity_id: "{{ daylight_light_list }}"
1 Like

This is working great! Though if any of my lights are set to a RGB value it seems to fail for all so I had to add a “| selectattr('attributes.color_temp_kelvin', '!=', None)” and now it all seems happy.

1 Like