Hi,
Here is what I did to auto-adjust color temperature for all the lights I want:
First, I have an automation that triggers every 10 minutes, whenever any lights change, or if the global color temperature changes. It triggers a “update_color_temperature” event.
- alias: Trigger Color Temperature Update
id: trigger_color_temperature_update
mode: single
trigger:
- platform: time_pattern
minutes: "/10"
- platform: state
entity_id: sensor.on_lights
- platform: state
entity_id: input_select.global_color_temp_k
action:
- delay: "00:00:05"
- event: update_color_temperature
Then I have a script that goes through all lights and looks for lights that are:
“on” AND “not groups” AND “in color_temp mode” AND “have a different color temp” AND "do not have no_ct in their name.
For all those bulbs, it sets the color temp to the value of input_select.global_color_temp_k.
This prevents any lights that are off from turning on, doesn’t change lights that are in color mode, and only updates lights that need to be updated. When a light is turned on, the update gets triggered and the light automatically adjusts.
For some lights that I don’t want to change, I just add “no_ct” to the end of their name and they are skipped.
- alias: Auto Update Lights Color Temperature
id: auto_update_lights_color_temperature
mode: restart
trigger:
- platform: event
event_type: update_color_temperature
action:
# - service: system_log.write
# data:
# message: >
# {% for state in states.light
# if is_state(state.entity_id, 'on')
# and (state_attr(state.entity_id, 'entity_id') == None)
# and (state_attr(state.entity_id, 'color_mode') == 'color_temp')
# and ('no_ct' not in state.entity_id) -%}
# {{state.entity_id}}{{ "," if not loop.last else "" }}
# {%- endfor %}
# level: warning
## 26-Aug-2022: Use a choose so we don't do anything if there are no lights to change
- choose:
# Check if there are any lights to adjust and only do so if there are!
- conditions: >
{% for state in states.light
if is_state(state.entity_id, 'on')
and (state_attr(state.entity_id, 'entity_id') == None)
and (state_attr(state.entity_id, 'color_mode') == 'color_temp')
and (state_attr(state.entity_id,'color_temp')|int(370) != ((1000000/(states('input_select.global_color_temp_k')|int(2700)))|int))
and ('no_ct' not in state.entity_id) -%}
{%- if loop.first -%}
{{"true" if loop.length>0 else "false"}}
{%- endif -%}
{%- endfor %}
sequence:
# - service: system_log.write
# data:
# message: "Need to adjust temperature."
# level: warning
- service: light.turn_on
data:
entity_id: >
{% for state in states.light
if is_state(state.entity_id, 'on')
and (state_attr(state.entity_id, 'entity_id') == None)
and (state_attr(state.entity_id, 'color_mode') == 'color_temp')
and (state_attr(state.entity_id,'color_temp')|int(370) != ((1000000/(states('input_select.global_color_temp_k')|int(2700)))|int))
and ('no_ct' not in state.entity_id) -%}
{{state.entity_id}}{{ "," if not loop.last else "" }}
{%- endfor %}
color_temp: "{{(1000000/(states('input_select.global_color_temp_k')|int(2700)))|int}}"
transition: 10
However, I have some lights that don’t match in color temperature, so for them I have a custom calibration automation that is also triggered:
- alias: Update Color Temperature for Calibrated Lights
id: update_color_temperature_for_calibrated_lights
trigger:
- platform: event
event_type: update_color_temperature
action:
repeat:
for_each:
- light: light.living_room_white_led_strip_no_ct
calibration: [2700, 2128, 4000, 3015]
- light: light.living_room_tv_led_strip_no_ct
calibration: [2700, 2066, 4000, 4000]
- light: light.led_strip_2_no_ct
calibration: [2700, 2066, 4000, 4000]
sequence:
# Check if it is on, not a group, and in color_temp mode
- if: "{{ is_state(repeat.item.light, 'on') and (state_attr(repeat.item.light, 'entity_id') == None) and (state_attr(repeat.item.light, 'color_mode') == 'color_temp') }}"
then:
# - service: system_log.write
# data:
# message: >
# {%- set desired_low = repeat.item.calibration[0]|int(2700) -%}
# {%- set actual_low = repeat.item.calibration[1]|int(2700) -%}
# {%- set desired_high = repeat.item.calibration[2]|int(4000) -%}
# {%- set actual_high = repeat.item.calibration[3]|int(4000) -%}
# {%- set scale = (actual_high-actual_low)/(desired_high-desired_low)|float(1.0) -%}
# {%- set offset = (actual_low)-(desired_low)*scale|float(0.0) -%}
# {%- set target = states('input_select.global_color_temp_k')|float(2700) -%}
# {%- set use = target*scale+offset -%}
# "Calibrated color temperature for {{repeat.item.light}} is {{use}} with scale: {{scale}}, offset: {{offset}}, target: {{target}}."
- service: light.turn_on
data:
entity_id: "{{ repeat.item.light }}"
kelvin: >
{%- set desired_low = repeat.item.calibration[0]|int(2700) -%}
{%- set actual_low = repeat.item.calibration[1]|int(2700) -%}
{%- set desired_high = repeat.item.calibration[2]|int(4000) -%}
{%- set actual_high = repeat.item.calibration[3]|int(4000) -%}
{%- set scale = (actual_high-actual_low)/(desired_high-desired_low)|float(1.0) -%}
{%- set offset = (actual_low)-(desired_low)*scale|float(0.0) -%}
{%- set target = states('input_select.global_color_temp_k')|float(2700) -%}
{{ target*scale+offset|int(target) }}
transition: 10
I also have automations that change the color temperature during the day. This also triggers an update of all bulbs:
- alias: "Auto Color Temp Wakeup 1"
id: auto_color_temp_wakeup_1
trigger:
platform: time
at: "06:00:00"
action:
- service: input_select.select_option
data:
entity_id: input_select.global_color_temp_k
option: "2700"
- alias: Auto Color Temp Morning 2
id: auto_color_temp_morning_2
trigger:
platform: time
at: "06:55:00"
action:
- service: input_select.select_option
data:
entity_id: input_select.global_color_temp_k
option: "3000"
- alias: Auto Color Temp Day 3
id: auto_color_temp_day_3
trigger:
platform: time
at: "08:00:00"
action:
- service: input_select.select_option
data:
entity_id: input_select.global_color_temp_k
option: "3300"
- alias: Auto Color Temp Afternoon 4
id: auto_color_temp_afternoon_4
trigger:
platform: time
at: "17:00:00"
action:
- service: input_select.select_option
data:
entity_id: input_select.global_color_temp_k
option: "3000"
- alias: Auto Color Temp Evening 5
id: auto_color_temp_evening_5
trigger:
platform: time
at: "20:00:00"
action:
- service: input_select.select_option
data:
entity_id: input_select.global_color_temp_k
option: "2700"
- alias: Auto Color Temp Night 6
id: auto_color_temp_night_6
trigger:
platform: time
at: "22:00:00"
action:
- service: input_select.select_option
data:
entity_id: input_select.global_color_temp_k
option: "2500"
The problem of turning off lights and losing them from the mesh is a big one!
I have 14 bulbs on track lights, and when the wall switch goes off the mesh gets totally screwed up.
So what I did was have one ZigBee network for those bulbs and one for remotes. This means even when the wall switch is shut off, the network the remotes are on does not get affected.