Using xy_color in data_template ... how?

Hi, I am trying to get the following automation running:

- id: 'weekendeveregaladjust'
  alias: Weekend Eve Regal adjust
  trigger:
  - platform: time
    minutes: '/10'
    seconds: 00
  condition:
  - condition: state
    entity_id: group.stube
    state: 'on'
  - condition: numeric_state
    entity_id: sun.sun
    value_template: '{{ state.attributes.elevation }}'
    below: 14
    above: -70.0
  - condition: time
    after: '15:15:00'
    before: '22:45:00'
    weekday:
      - wed
      - fri
      - sat
      - sun
  - condition: template
    value_template: '{{ as_timestamp(now()) - as_timestamp(states.automation.weekend_eve_regal_adjust.attributes.last_triggered) | int > 898 }}'
  action:
    service: light.turn_on
    data:
      entity_id: light.led_regal
      transition: 900
      brightness_pct: 100
    data_template:
      xy_color: >
        {% if state_attr('sun.sun', 'elevation') | float | round(0) >= 16 and states('sensor.time') <= '18:30' %}
          0.16,0.504 
        {% elif state_attr('sun.sun', 'elevation') | float | round(0) >= 12 and states('sensor.time') <= '19:00' %}
          0.375,0.557
        {% elif state_attr('sun.sun', 'elevation') | float | round(0) >= 8 and states('sensor.time') <= '19:30' %}
          0.608,0.373
        {% elif state_attr('sun.sun', 'elevation') | float | round(0) >= 0 and states('sensor.time') <= '20:00' %}
          0.40530,0.39072
        {% elif state_attr('sun.sun', 'elevation') | float | round(0) >= -8 and states('sensor.time') <= '20:30' %}
          0.42336,0.39895
        {% elif state_attr('sun.sun', 'elevation') | float | round(0) >= -24 and states('sensor.time') <= '21:00' %}
          0.43693,0.40407
        {% elif state_attr('sun.sun', 'elevation') | float | round(0) >= -30 and states('sensor.time') <= '21:30' %}
          0.45186,0.40863
        {% elif state_attr('sun.sun', 'elevation') | float | round(0) >= -36 and states('sensor.time') <= '22:00' %}
          0.46823,0.41230
        {% else %}
          0.48614,0.41467
        {% endif %}

Unfortunately, it does not work. Using [] around the numbers also does not work. I think generally it is correct, because with Kelvin: and just one number it works, but xy_color needs a list and I am not sure how to do it.
Thanks for help.

Classic problem. This forum is loaded with questions like this.

The problem is that a Jinja template can only output a string. So even though you try to have it result in something like [x,y], it really outputs "[x,y]". And the reason it works for a single number is, when the service data is processed, there is a “schema” that is used that forces a string representation of a number to be converted to an actual number. That’s why (for things like kelvin that take a single number), "123" works as well as 123.

What really needs to happen is for the light service to be enhanced to accept a string (for xy_color, rgb_color, etc.) that contains representations of numbers separated by commas, like "0.16,0.504". But until someone does that, you can’t use a single Jinja template to generate a list of numbers.

What I’ve seen most people do is either use multiple templates – ["{{ ... }}", "{{ ... }}"] – or they use something else like color_name, a scene, etc.