I’m trying to build an action that cycles through a series of pre-defined Kelvin values for a light from WiZ. The use case is that I want to click on my wall switch (which can send requests to REST API endpoints) and cycle through a series of Kelvin values for the lights of the room (defined in the body of the request).
I build this script but it doesn’t work. Apparently it can’t detect the current kelvin value. I thought that by doing
map(‘state_attr’, ‘kelvin’)
I got the kelvin value of the entity, but maybe I am wrong.
Can you please help? Thanks!
alias: Step Cycle Color Temperature
mode: parallel
description: |
Step adjust the color temperature of the lights.
fields:
entity_id:
description: The light(s)
required: true
selector:
entity:
multiple: true
domain: light
variables:
lights: "{{ entity_id if entity_id is list else [entity_id] }}"
current_kelvin: >-
{{ lights | map('state_attr', 'kelvin') | select('is_number') | list |
average }}
next_kelvin: |-
{% if current_kelvin == 2700 %}
3500
{% elif current_kelvin == 3500 %}
6000
{% elif current_kelvin == 6000 %}
2700
{% endif %}
sequence:
- action: light.turn_on
target:
entity_id: "{{ lights }}"
data:
kelvin: "{{ next_kelvin }}"
icon: mdi:lightbulb-outline```
I managed to get one step forward. Now this is my version of the script:
alias: Step Cycle Color Temperature 3
mode: parallel
description: |
Step adjust the color temperature of the lights.
fields:
entity_id:
description: The light(s)
required: true
selector:
entity:
multiple: true
domain: light
variables:
lights: "{{ entity_id if entity_id is list else [entity_id] }}"
current_kelvin: >-
{{ lights | map('state_attr', 'color_temp_kelvin') | select('is_number') | list |
average }}
next_kelvin: |-
{% if current_kelvin == 2700 %}
3500
{% elif current_kelvin == 3500 %}
6000
{% elif current_kelvin == 6000 %}
2700
{% endif %}
sequence:
- action: light.turn_on
target:
entity_id: "{{ lights }}"
data:
kelvin: "{{ next_kelvin }}"
icon: mdi:lightbulb-outline
However, I get the error now that:
Failed to perform the action script/step_cycle_color_temperature_3.
expected int for dictionary value @ data['kelvin']
Any idea?
Also - would be very useful to know why the enttity’s state has an attibute called “color_temp_kelvin” whereas if I want to edit it with an action, I need to refer to “kelvin”…
I think that you’re setting a numeric string in next_kelvin: rather than an integer, the error suggests that it needs to be an integer. Maybe try {{ 3500 | int }} instead of just 3500 (also applies to 6000 and 2700). If that doesn’t work you might need to set it to an integer before using it. I’ve run into something similar before with timestamps.
HA 2025.1.0 transitions to requiring kelvin rather than mireds from an integration point of view, that’s likely being handled by mapping from the integration since the bulb itself reports color_temp_kelvin? Not sure, might be worth opening an issue with the integration. I know from beta that Wyze bulbs throw a depreciation error but Wiz bulbs do not.
alias: Step Cycle White Temperature v1
mode: parallel
description: |
Step adjust the white temperature of one (or a list of) light(s)
fields:
entity_id:
description: The light(s) to adjust
required: true
selector:
entity:
multiple: true
domain: light
variables:
lights: "{{ entity_id if entity_id is list else [entity_id] }}"
kelvin_values:
- 2200
- 2700
- 3000
- 3200
- 4000
- 5000
- 6000
current_kelvin: |-
{{
lights | map('state_attr', 'color_temp_kelvin')
| select('is_number') | list | average | default(-1) | int
}}
next_kelvin: >-
{% set index = kelvin_values.index(current_kelvin) if current_kelvin in
kelvin_values else -1 %} {% set next_index = (index + 1) % kelvin_values |
length %} {{ kelvin_values[next_index] }}
sequence:
- data:
level: info
message: |
Current Kelvin: {{ current_kelvin }}, Next Kelvin: {{ next_kelvin }}
action: system_log.write
- target:
entity_id: "{{ lights }}"
data:
kelvin: "{{ next_kelvin }}"
action: light.turn_on
icon: mdi:lightbulb-outline
And this is a version that edits all lights in a room:
alias: Step Cycle White Temperature v5
mode: parallel
description: |
Step adjust the white temperature of the lights in a room
fields:
room_id:
description: The room whose lights should be adjusted
required: true
selector:
area:
multiple: false
variables:
lights: |-
{{
area_entities(room_id) | select('match', 'light') | list
}}
kelvin_values:
- 2200
- 2700
- 3000
- 3200
- 4000
- 5000
- 6000
current_kelvin: |-
{{
lights
| map('state_attr', 'color_temp_kelvin')
| select('is_number')
| list
| average(default=-1)
| int
}}
next_kelvin: >-
{% set index = kelvin_values.index(current_kelvin) if current_kelvin in
kelvin_values else -1 %} {% set next_index = (index + 1) % kelvin_values |
length %} {{ kelvin_values[next_index] }}
sequence:
- data:
level: info
message: |
Current Kelvin: {{ current_kelvin }}, Next Kelvin: {{ next_kelvin }}
action: system_log.write
- target:
entity_id: "{{ lights }}"
data:
kelvin: "{{ next_kelvin }}"
action: light.turn_on
icon: mdi:lightbulb-outline
Room for improvement: right now if the current Kelvin value is not one of those in the list, the index will go to the first one. It would be better if it went to the closest value in the array rather than always the first. If I’ll manage to improve it I’ll provide a solution here.