Hi there, I’m trying to setup an automation to gradually change my lights color over a period of hours but it includes a transition from kelvin to rgb_color at a certain time.
I’ve pasted the data_template part into the Developer Tools > TEMPLATE page and it appears to work, but when I try to load the automation I get the following error:
“Invalid config for [automation]: expected dict for dictionary value @ data[‘action’][0][‘data’]. Got None.”
I’m suspicious that it’s due to me using both kelvin and rgb_color in the same template, but I’m not sure…
Any help would be greatly appreciated.
- id: '1616135459355'
alias: Night Light Transition
description: 'If the lights are turned on, update the color every minute transitioning from kelvin to RGB at 20:00'
trigger:
- platform: time_pattern
minutes: /1
condition:
- condition: state
entity_id: light.bedroom_lights
state: 'on'
- condition: time
after: '18:00:00'
before: '23:00:00'
action:
- service: light.turn_on
entity_id: light.bedroom_lights
data_template: >-
{% set stime = [18, 0] %} # start time, 18:00:00
{% set ttime = [20, 0] %} # transition time, 20:00:00 (when we change from kelvin to RGB)
{% set etime = [23, 0] %} # end time, 23:00:00
{% set stemp = 4000 %} # start temp, 4000 kelvin
{% set etemp = 2800 %} # end temp, 2800 kelvin
{% set sgren = 100 %} # start rgb-green, 100 | red is always 255
{% set sblue = 50 %} # start rgb-blue, 50 | blue and green both end at 0
{% set ct = timedelta(hours = now().hour, minutes = now().minute) %} # current time as timedelta
{% set st = timedelta(hours = stime[0], minutes = stime[1]) %} # start time as timedelta
{% set et = timedelta(hours = etime[0], minutes = etime[1]) %} # end time as timedelta
{% set tt = timedelta(hours = ttime[0], minutes = ttime[1]) %} # transition time as timedelta
{% set zt = strptime("1970-01-01", "%Y-%m-%d") %} # zero time, needed to make timedeltas into datetimes
{% if ct < tt %} # if we are before transition time use kelvin
{% set ot = as_timestamp(as_local(zt + (tt - st))) %} # calc overall time (transition-start)
{% set rt = as_timestamp(as_local(zt + (tt - ct))) %} # calc remaining time (transition-current)
{% set dk = stemp - etemp %} # calc kelvin difference (4000-2800=1200)
{% set temp = (etemp + ((rt / ot) * dk))|int %} # calc current temp (fraction of dk, like 0.1 * 1200)
kelvin: {{temp}} # set kelvin
{% else %} # otherwise we use RGB
{% set ot = as_timestamp(as_local(zt + (et - tt))) %} # calc overall time (end - transition)
{% set dt = as_timestamp(as_local(zt + (ct - tt))) %} # calc elapsed time (current - transition)
{% set cg = (sgren - ((dt / ot) * sgren))|int %} # calc green value (fraction of sgren, like 0.1 * 100)
{% set cb = (sblue - ((dt / ot) * sblue))|int %} # calc blue value (fraction of sblue, like 0.1 * 50)
rgb_color: [255, {{cg}}, {{cb}}] # set rgb_color
{% endif %}
mode: single
I’m quite new to this YAML/jinja2 stuff so apologies if my calculations are over-complicated.