blueprint:
  name: Match Outside Colour Temperature (Kelvin)
  description: >
    When a light turns on, or the sun elevation changes, set its colour temperature
    to match the outside light. Applies to any bulb that supports colour temperature.
    Triggers: whenever a managed bulb turns on, on sun elevation changes, and every 5 minutes.
    Compatible with Home Assistant 2026.1+.
  domain: automation
  source_url: https://gist.github.com/HarvsG/cc86381dc99169159d6d4cd870c36e46

  input:
    target_lights:
      name: Lights
      description: The lights to be controlled.
      selector:
        entity:
          domain:
            - light
          multiple: true

    cool_color_temp:
      name: Coolest colour temperature
      description: >
        The highest (coolest/bluest) colour temperature your bulbs will be set to,
        used when the sun is high in the sky. Higher K = cooler/whiter light.
      default: 4000
      selector:
        number:
          min: 1500
          max: 7000
          step: 50
          unit_of_measurement: K
          mode: slider

    warm_color_temp:
      name: Warmest colour temperature
      description: >
        The lowest (warmest/most orange) colour temperature your bulbs will be set to,
        used at night or when the sun is below the horizon. Lower K = warmer light.
      default: 2202
      selector:
        number:
          min: 1500
          max: 7000
          step: 50
          unit_of_measurement: K
          mode: slider

    brightness_action:
      name: Brightness correction action
      description: >
        Setting colour temperature resets brightness on some bulbs. This action runs
        500 ms after the colour-temperature change to restore brightness.
        The default restores the bulb's current brightness (minimum 2 to avoid issues
        with certain bulbs).
      default:
        - action: light.turn_on
          data:
            brightness: >
              {{ [trigger.to_state.attributes.brightness | int(0), 2] | max }}
          target:
            entity_id: "{{ trigger.entity_id }}"
      selector:
        action: {}

variables:
  target_lights: !input target_lights
  cool_color_temp: !input cool_color_temp
  warm_color_temp: !input warm_color_temp
  # All managed lights that are currently on AND support colour temperature
  target_on_lights: >
    {{ states.light
       | selectattr('state', 'eq', 'on')
       | selectattr('attributes.supported_color_modes', 'search', 'color_temp')
       | map(attribute='entity_id')
       | select('in', target_lights)
       | list }}
  # Sun-elevation-based colour temperature calculation.
  # Formula maps elevation (clamped 0–90°) to a Kelvin value via a photometric
  # approximation, then clamps the result between warm_color_temp and cool_color_temp.
  color_temp: >
    {% set elevation = [([0, state_attr('sun.sun', 'elevation')] | max), 90] | min %}
    {% set raw = (4791.67 - 3290.66 / (1 + 0.222 * elevation ** 0.81)) | int %}
    {{ [[raw, warm_color_temp] | max, cool_color_temp] | min }}

trigger:
  - platform: state
    entity_id: !input target_lights
    to: "on"
    id: single_light_on

  - platform: state
    entity_id:
      - sun.sun
    attribute: elevation
    id: elevation_change

  - platform: time_pattern
    minutes: "5"
    id: every_5_min

condition:
  # Only proceed if at least one light is currently on
  - condition: template
    value_template: >
      {{ (states.light | selectattr('state', 'eq', 'on') | list) != [] }}

action:
  - choose:
      # A single light just turned on: set its colour temperature, then restore brightness
      - conditions:
          - condition: trigger
            id: single_light_on
        sequence:
          - action: light.turn_on
            target:
              entity_id: "{{ trigger.entity_id }}"
            data:
              color_temp_kelvin: "{{ color_temp }}"
          - delay:
              milliseconds: 500
          - sequence: !input brightness_action

      # Periodic or elevation-change update: apply to all managed on-lights with a smooth transition
      - conditions:
          - condition: or
            conditions:
              - condition: trigger
                id: every_5_min
              - condition: trigger
                id: elevation_change
        sequence:
          - action: light.turn_on
            target:
              entity_id: "{{ target_on_lights }}"
            data:
              color_temp_kelvin: "{{ color_temp }}"
              transition: 5

mode: parallel
