Configure so all lights use a sensor to set color temperature

I have a sensor defined to calculate the color temperature from a input_number ranging from 1 to 255.

    brightness_kelvin:
      friendly_name: Brightness Kelvin
      unit_of_measurement: "K"
      value_template: "{{ min(6500, max(2000, ((states('input_number.brightness') | int * 20) + 1680))) }}"
      # kelvin = (input_number.brightness * 20); # kelvin = 20 to 5100.
      # kelvin = kelvin + 1980; # kelvin = 2000 to 7080.
      # kelvin = kelvin -  300; # kelvin = 1700 to 6780.
      # kelvin = max(2000, min(6500, kelvin)); # kelvin = 2000 to 6500.

I can use the sensor in YAML scripts, automations and calls to python fine.

QUESTION:
How do I get home assistant to use this sensor to set the color temperature of all the lights I use automatically. If I toggle on a light from a dashboard, I want that light to use the sensor to calculate the color temperature for that light.

Triggering an automation when the light is turned on, will use the lights existing color temperature, then my override value. How can I make the light come on with my calculated color temperature?

You could have an automation that triggers on light turning on and a second trigger being when the sensor value changes.

Then condition that the light is on.

And lastly action turn on with color temperature set to {{ states(sensor.brightness_kelvin) }}.

Thanks for the fast reply.

Unless i’m missing something …

When the light gets turned on, it is turned on with the old color temperature. If I then trigger an automation to adjust the color temperature, the light will flash from the old state to the new state.

I currently have an input_number.brightness which has a value of 1 to 255. When I turn on a light with a script or automation, or a change the input_number.brightness, its all works fine.

HOWEVER…

When I just turn the light on from the dashboard, it uses the old brightness and color temperature.

Is there some way I can associate the sensor.brightness_kelvin and a new sensor called sensor.brightness with each light so it happens system wide, without me having to put it in the automations, and scripts. This would also mean, when I turn the light on from the dashboard, it would have the correct brightness and color temperature.

There’s no way to set the color temperature on a light that is off. So the only way to accomplish this is to never use the generic on switches/services, but only turn lights on using the light.turn_on service that can supply all parameters on how exactly you want the light to turn on.

Yes… and with the automation I described above it will turn on then quickly change. If the light is local then you will probably not notice it.
If it’s cloud based then you need to replace the light, there is no way around that. It will be too slow.

You could consider creating a template light that overrides the on behavior to use a script that calls the light.turn_on service with the proper color temperature. You can then hide the original light control.

How do I get a template associated with the default light controls from the generated dash board.

If I create my own Helper boolean, I can associate a script with it. How do I change the color and
brightness of the Helper icon, so it acts like the default light icons? For example if I change the color
of the light to red, and lower the brightness to 1%.

A boolean input can never truly mimic a light and may get its state out of sync with the real light, so I suggested a template light instead:

So something like this partial template might replace the actual light entity (which you then set to hidden):

light:
  - platform: template
    lights:
      my_temperature_controlled_light:
        level_template: "{{ state_attr('light.original', 'level')|int }}"
        value_template: "{{ states('light.original')  }}"
        temperature_template: "{{state_attr('light.original', 'temperature') | int}}"
        color_template: "{{ state_attr('light.original', 'color') | int}}"
        turn_on:
          service: light_turn_on
          target:
            entity_id: light.original
          data:
            kelvin: "{{ states(sensor.brightness_kelvin) }}"
        turn_off:
          # ... (redirect all possible actions to the original light here)

Sorry, I don’t own temperature controllable lights, so I didn’t write out the entire template because I couldn’t really test this to make sure it is correct. But it should make the idea clear.

Thanks for reply. Will look into it.