Light transition template issue - different attributes?

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.

I suspect your comments are breaking the template. Also, in recent HA versions you can just use data:, don’t need data_template.

Thanks for the tips, I’ve switched to data: now - I’m on core-2021.3.4 / supervisor-2021.03.6
Also just tried removing comments and different indentations but no difference unfortunately.

Here’s what I’m trying at the moment:

- 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: >-
        {% set stime = [18, 0] %}
        {% set ttime = [20, 0] %}
        {% set etime = [23, 0] %}
        {% set stemp = 4000 %}
        {% set etemp = 2800 %}
        {% set sgren = 100 %}
        {% set sblue = 50 %}
        {% set ct = timedelta(hours = now().hour, minutes = now().minute) %}
        {% set st = timedelta(hours = stime[0], minutes = stime[1]) %}
        {% set et = timedelta(hours = etime[0], minutes = etime[1]) %}
        {% set tt = timedelta(hours = ttime[0], minutes = ttime[1]) %}
        {% set zt = strptime("1970-01-01", "%Y-%m-%d") %}
        {% if ct < tt %}
          {% set ot = as_timestamp(as_local(zt + (tt - st))) %}
          {% set rt = as_timestamp(as_local(zt + (tt - ct))) %}
          {% set dk = stemp - etemp %
          {% set temp = (etemp + ((rt / ot) * dk))|int %}
          kelvin: {{temp}}
        {% else %}
          {% set ot = as_timestamp(as_local(zt + (et - tt))) %}
          {% set dt = as_timestamp(as_local(zt + (ct - tt))) %
          {% set cg = (sgren - ((dt / ot) * sgren))|int %}
          {% set cb = (sblue - ((dt / ot) * sblue))|int %}
          rgb_color: [255, {{cg}}, {{cb}}]
        {% endif %}
  mode: single

I reckon that I’ve just complicated this too much though, I’ll try again tomorrow using choose: and two separate blocks of calculation.