OK. I finally cracked this.
How HA treats every variable in data_template as a f***ing string was the most annoying culprit here.
First a little backstory on my use case. I have a Deconz zigbee dongle that delivers a daylight sensor. This sensor tracks the daylight phase. Up until now I’ve used this sensor to trigger different scenes, but there are many annoyances with using scenes. Such as lack of transitions or the possibility to adjust one or many lights at the same time.
So…
- My Deconz deliver the daylight phase
- I created a platform sensor with attributes containing rgb values for each phase (+ variations - ie, different moods on some bulbs, different season etc)
- I created a script to parse the daylight phase and deliver the correct rgb color as a variable to a second script. This due to the f***-up of
rgb_color: >
- I created a script to actually set the current rgb value to the bulbs of my choice with the transition of my choice
The attributes sensor containing all the RGB values (you don’t need to encapsulate the attributes in “”, but I chose to):
# Daylight color temperatures
sensor
- platform: template
sensors:
daylight_color_temps:
value_template: "{{ states.sensor.daylight.state }}"
attribute_templates:
sunrise_start_v1: "255, 220, 184"
sunrise_start_v2: "255, 184, 184"
sunrise_end_v1: "255, 228, 189"
sunrise_end_v2: "255, 194, 189"
golden_hour_1_v1: "255, 236, 195"
golden_hour_1_v2: "255, 205, 194"
solar_noon_v1: "255, 244, 200"
solar_noon_v2: "238, 255, 199"
golden_hour_2_v1: "253, 229, 183"
golden_hour_2_v2: "252, 192, 182"
sunset_start_v1: "250, 214, 165"
sunset_start_v2: "250, 172, 165"
sunset_end_v1: "253, 94, 83"
sunset_end_v2: "252, 83, 157"
dusk_v1: "120, 99, 97"
dusk_v2: "111, 136, 146"
nautical_dusk_v1: "96, 80, 87"
nautical_dusk_v2: "97, 82, 80"
night_start_v1: "72, 61, 76"
night_start_v2: "65, 61, 76"
nadir_v1: "72, 52, 117"
nadir_v2: "52, 65, 117"
night_end_v1: "118, 94, 134"
nautical_dawn_v1: "164, 136, 151"
nautical_dawn_v2: "162, 135, 163"
dawn_v1: "209, 178, 167"
dawn_v2: "209, 167, 177"
alarm_v1: "192,0,0"
alarm_v2": "15,0,191"
The parsing script (with fallback values just so it wouldn’t fail):
register_light:
sequence:
- service: script.turn_on
entity_id: script.set_light
data_template:
variables:
entities: "{{ entities | default('light.light_hue_livingroom_tv_1, light.light_hue_livingroom_tv_2', true) }}"
transition: "{{ transition | default(3, true) }}"
color: >
{%- set period = period | default("sunrise_start") -%}
{%- set version = version | default("v1") -%}
{%- set attrib = period ~ "_" ~ version -%}
"{{ state_attr("sensor.daylight_color_temps", attrib) }}"
The last script that actually sets the color:
set_light:
sequence:
- service: light.turn_on
data_template:
entity_id: '{{ entities | default("light.light_hue_livingroom_tv_1", true) }}'
transition: '{{ transition | default(0, true) }}'
rgb_color:
- '{{ color.split(",")[0].strip() | default(100, true) | int }}'
- '{{ color.split(",")[1].strip() | default(50, true) | int }}'
- '{{ color.split(",")[2].strip() | default(200, true) | int }}'