Normalize lights, programmatically

Here’s a script I made for my Linkind A19 RGBTW Wi-Fi Smart Light Bulbs. Depending on what your lights are or how they’re added, you may have to change things. It will:

  • Loop through a list of lights
  • Set Brightness
  • Set Kelvin Color
  • Return light to previous on/off state
alias: Normalize Lights
description: 'Normalize brightness and color, returns lights to previous on/off state'
variables:
  bright: 100 # 0 - 100, percentage of brightness you want to set
  k_temp: 4800 # kelvin color temperature you wish to set
sequence:
  - repeat:
      for_each: # These are my light entities I want to manage, replace with yours
        - light.living_room_entry
        - light.hall_hall
        - light.kitchen_sink
        - light.dining_room_dining_room
      sequence:
        - variables:
            l_id: "{{ repeat.item }}"
            # Recall if the light was off later
            was_off: "{{ is_state(l_id, 'off') }}"

        # Turn on light, set brightness, color
        - action: light.turn_on
          target:
            entity_id: "{{ l_id }}"
          data:
            brightness_pct: "{{ bright }}"
            color_temp_kelvin: "{{ k_temp }}"

        # If the light was off, turn it back off
        - if:
            - condition: template
              value_template: "{{ was_off }}"
          then:
            - delay: "00:00:01" # needed, for me, otherwise lights weren't turning off
            - action: light.turn_off
              target:
                entity_id: "{{ l_id }}"

If you want to specify an RGB color value instead, replace color_temp_kelvin with rgb_color and change the variable to a 0-255 RGB value like [255,255,0] rather than the 4800 kelvin number.

I’m not a YAML person so I did not know program syntax or capabilities. I struggled through asking the internet my questions and it gave me back a script that technically did what I wanted but was absolutely terrible in every regard. It had explicit values and just repeatedly copy/pasted blocks. TBH it’ll probably keep me up at night. Hopefully this helps spare someone that agony.

If you’re interested, here’s another way to do the same thing but without using a repeat for_each.

alias: Normalize Lights
description: 'Normalize brightness and color, returns lights to previous on/off state'
variables:
  bright: 100 # 0 - 100, percentage of brightness you want to set
  k_temp: 4800 # kelvin color temperature you wish to set
  lights:
    - light.living_room_entry
    - light.hall_hall
    - light.kitchen_sink
    - light.dining_room_dining_room
  lights_off: "{{ lights | select('is_state', 'off') | list }}"
sequence:
  - action: light.turn_on
    target:
      entity_id: "{{ lights }}"
    data:
      brightness_pct: "{{ bright }}"
      color_temp_kelvin: "{{ k_temp }}"
  - delay:
      seconds: 1
  - action: light.turn_off
    target:
      entity_id: "{{ lights_off }}"

I tested it in my home with four lights and confirmed it works correctly; it changed brightness and color temperature of all four lights and then restored two of them back to their initial state (off).

That is shorter yet, thank you for sharing!