Light Temperature colour change according to time of the day

Hi,
I have an Mi LED Desk Lamp 1S, and till now I manage to switch it on and off, and change brightness and colour temp using the following automation:

- id: '1608972899489'
  alias: Switch on Desk Lamp when PC On
  description: Switch on Desk Lamp when PC On
  trigger:
  - platform: state
    entity_id: binary_sensor.sandro_pc
    from: 'off'
    to: 'on'
  condition: []
  action:
  - service: light.turn_on
    data:
      brightness_pct: 60
      color_temp: 350
    entity_id: light.mi_desk_lamp_1s
  mode: single

Now I would like to create an automation, that will change the color_temp value according to the time of the day or sun position. I read that I need to use a template, but to be honest I never got the hang how to use templates in automation. Anyone can provide some help on this?

Check out this custom component, ehich does exactly that:

1 Like

thanks … can this be found in the official addons?

No, it’s not an add-on, it’s a custom component that can easily be installed through HACS

never used HACS, will have to read about it.

Managed to make it work with the help of this post

Details Below:

Automation

  - alias: 'Circadian Lights'
    id: 'Circadian Lights'
    initial_state: 'off'
    trigger:
      platform: time
      minutes: '/15'
      seconds: 00
    condition:
      condition: template
      value_template: >
        {{ is_state('light.mi_desk_lamp_1s', 'on')}}
    action:
service: light.turn_on
entity_id: light.mi_desk_lamp_1s
data_template:
  color_temp: >
    {{ states('sensor.circadian_light') }}

Tempate Sensors in Configuration.yaml

# factor for circadian light calculation
  - platform: template
    sensors:
      length_of_day_factor:
        friendly_name: 'Length of day' 
        value_template: >
          {% set daylength = ( (as_timestamp(state_attr('sun.sun','next_setting')) - 
                                as_timestamp(state_attr('sun.sun','next_rising')) ) / 
                                 3600) + 24 %}
              {{ ((daylength*-0.0063616)+0.11131) | round(5) }}

# Circadian Light Value
  - platform: template
    sensors:
      circadian_light:
        friendly_name: 'Circadian light'
        unit_of_measurement: 'mired'
        value_template: >
          {% if is_state('sun.sun' , 'above_horizon') %}
            {{ ((states('sensor.length_of_day_factor') | float) * 
               ((state_attr('sun.sun','azimuth') )-180)**2 + 175) | round }}
          {% elif (as_timestamp(state_attr('sun.sun','next_dusk'))) - 
                  (as_timestamp(state_attr('sun.sun','next_setting'))) < 0 or 
                  (as_timestamp(state_attr('sun.sun','next_rising'))) - 
                  (as_timestamp(state_attr('sun.sun','next_dawn'))) < 0 %}
            350
          {% else %}
            390
          {% endif %}
1 Like

Sorry for raising this topic from the grave but just wanted to say thank you so much for those template sensors. I’ve been trying Flux and Adaptive Lighting and both have either failed miserably (Flux) or worked for a day and then started turning things on and off for no reason and not making any logs of it (Adaptive Lighting). Your template sensors on the other hand did almost exactly what I wanted on the first try and have continued to work flawlessly where both Flux and Adaptive Lighting both failed. Thank you.

1 Like

I am glad this was useful :slight_smile:

I’m having trouble with this. It maxes out to -390 mired at the same time each day. Please help.

Not sure if this is still an open topic for you, but in case someone else finds this thread in the future: the problem happens at the moment the sun sets, then the day length becomes negative:

I still want to tweak the logic in the template, haven’t wrapped my head around it yet though. Brute force “fix”: check in the automation if the value is positive and only then use it for setting the lamp color.