Advanced Light template Help

Updated: Working code in post 2. Ive come to realize jinja will ALWAYS return a string, and a list cannot and will not ever be output. Post 2 has the only way to create a list and use jinja.

I am working on creating a light template (for philips hue bulbs) utilizing zha groups. I have it mostly working, but am struggling to get color working, and i think it has to do with formatting.
args should be a list, and the macro is returning a list [x, y, transition]
I know with template lights, the variables are supposed to be contained within double quotes, and i think somewhere this is where it is failing. (h and s are from the color picker)
The math is all correct, and the returned values work in the dev tools>services.
Any help would be greatly appreciated.

light:
  - platform: template
    lights:
      zha_group_living_room:
        # light.living_room_hue_1, light.living_room_hue_1, light.living_room_hue_1, light.living_room_hue_1
        friendly_name: "Living Room Lights"
        turn_on:
          service: zha.issue_zigbee_group_command
          data:
            group: 0x0001
            cluster_id: 6
            command: 1
            args: []
        turn_off:
          service: zha.issue_zigbee_group_command
          data:
            group: 0x0001
            cluster_id: 6
            command: 0
            args: []
        set_level:
          service: zha.issue_zigbee_group_command
          data_template:
            group: 0x0001
            cluster_id: 8
            command: 4
            args:
              - "{{ brightness }}"  # level (0-255)
              - 5                   # transition time
        set_color:
          service: zha.issue_zigbee_group_command
          data_template:
            group: 0x0001
            cluster_id: 768
            command: 7
            args: >-
              {%- macro hs_to_xy(iH, iS, iV, transition) -%}
                  {# Convert from hue/saturation to RGB #}
                  {# Hue is scaled 0-360 #}
                  {# Sat is scaled 0-100 #}
                  {# Val is scaled 0-100 #}
                  {%- set h = iH / 360 -%}
                  {%- set s = iS /100 -%}
                  {%- set v = iV / 100 -%}
                  {%- if s == 0.0: -%}
                  {%-     set rgb = v, v, v -%}
                  {%- endif -%}
                  {%- set i = (h*6.0)|int -%}
                  {%- set f = (h*6.0) - i -%}
                  {%- set p = v*(1.0 - s) -%}
                  {%- set q = v*(1.0 - s*f) -%}
                  {%- set t = v*(1.0 - s*(1.0-f)) -%}
                  {%- set i = i%6 -%}
                  {%- if i == 0: -%}
                  {%-     set fRGB = v, t, p -%}
                  {%- elif i == 1: -%}
                  {%-     set fRGB = q, v, p -%}
                  {%- elif i == 2: -%}
                  {%-     set fRGB = p, v, t -%}
                  {%- elif i == 3: -%}
                  {%-     set fRGB = p, q, v -%}
                  {%- elif i == 4: -%}
                  {%-     set fRGB = t, p, v -%}
                  {%- elif i == 5: -%}
                  {%-     set fRGB = v, p, q -%}
                  {%- endif -%}
                  {%- set rgb = ((fRGB[0] * 255)|int, (fRGB[1] * 255)|int, (fRGB[2] * 255)|int) -%}
                  {#{ rgb }#}
                  {# Convert from RGB color to XY color #}
                  {%- set iR = rgb[0] -%}
                  {%- set iG = rgb[1] -%}
                  {%- set iB = rgb[2] -%}
                  {%- if iR + iG + iB == 0: -%}
                  {%-    set xy = 0.0, 0.0, 0 -%}
                  {%- endif -%}
                  {%- set R = iR / 255 -%}
                  {%- set B = iB / 255 -%}
                  {%- set G = iG / 255 -%}
                  {# Gamma correction #}
                  {%- set R = ((R + 0.055) / (1.0 + 0.055) ** 2.4) if (R > 0.04045) else (R / 12.92) -%}
                  {%- set G = ((G + 0.055) / (1.0 + 0.055) ** 2.4) if (G > 0.04045) else (G / 12.92) -%}
                  {%- set B = ((B + 0.055) / (1.0 + 0.055) ** 2.4) if (B > 0.04045) else (B / 12.92) -%}
                  {# Wide RGB D65 conversion formula #}
                  {%- set X = R * 0.664511 + G * 0.154324 + B * 0.162028 -%}
                  {%- set Y = R * 0.283881 + G * 0.668433 + B * 0.047685 -%}
                  {%- set Z = R * 0.000088 + G * 0.072310 + B * 0.986039 -%}
                  {# Convert XYZ to xy #}
                  {%- set x = X / (X + Y + Z) -%}
                  {%- set y = Y / (X + Y + Z) -%}
                  {# Brightness #}
                  {%- set Y = 1 if Y > 1 else Y -%}
                  {%- set brightness = (Y * 255)|round -%}
                  {%- set xy = x|round(3), y|round(3), brightness -%}
                  {#{ xy }#}
                  [{{ (xy[0] * 65535)|int }}, {{ (xy[1] * 65535)|int }}, {{ transition }}]
              {%- endmacro -%}
              "{{ hs_to_xy(h, s, 100, 5) }}"
        set_temperature:
          service: zha.issue_zigbee_group_command
          data_template:
            group: 0x0001
            cluster_id: 768
            command: 10
            args:
              - "{{ color_temp }}"  # color temperature (153-500)
              - 5                   # transition time

Being unable to get an actual list to populate, this is how i have it working… even though it seems like overkill…

light:
  - platform: template
    lights:

      zha_group_living_room:
        # light.living_room_hue_1, light.living_room_hue_1, light.living_room_hue_1, light.living_room_hue_1
        friendly_name: "Living Room Lights"
        turn_on:
          service: zha.issue_zigbee_group_command
          data:
            group: 0x0001
            cluster_id: 6
            command: 1
            args: []
        turn_off:
          service: zha.issue_zigbee_group_command
          data:
            group: 0x0001
            cluster_id: 6
            command: 0
            args: []
        set_level:
          service: zha.issue_zigbee_group_command
          data_template:
            group: 0x0001
            cluster_id: 8
            command: 4
            args:
              - "{{ brightness }}"  # level (0-255)
              - 5                   # transition time
        set_color:
          service: zha.issue_zigbee_group_command
          data_template:
            group: 0x0001
            cluster_id: 768
            command: 7
            args:
              - >-
                {%- macro hs_to_xy(iH, iS, iV) -%}
                    {%- set h = iH / 360 -%}
                    {%- set s = iS /100 -%}
                    {%- set v = iV / 100 -%}
                    {%- if s == 0.0: -%}
                    {%-     set rgb = v, v, v -%}
                    {%- endif -%}
                    {%- set i = (h*6.0)|int -%}
                    {%- set f = (h*6.0) - i -%}
                    {%- set p = v*(1.0 - s) -%}
                    {%- set q = v*(1.0 - s*f) -%}
                    {%- set t = v*(1.0 - s*(1.0-f)) -%}
                    {%- set i = i%6 -%}
                    {%- if i == 0: -%}
                    {%-     set fRGB = v, t, p -%}
                    {%- elif i == 1: -%}
                    {%-     set fRGB = q, v, p -%}
                    {%- elif i == 2: -%}
                    {%-     set fRGB = p, v, t -%}
                    {%- elif i == 3: -%}
                    {%-     set fRGB = p, q, v -%}
                    {%- elif i == 4: -%}
                    {%-     set fRGB = t, p, v -%}
                    {%- elif i == 5: -%}
                    {%-     set fRGB = v, p, q -%}
                    {%- endif -%}
                    {%- set rgb = ((fRGB[0] * 255)|int, (fRGB[1] * 255)|int, (fRGB[2] * 255)|int) -%}
                    {%- set iR = rgb[0] -%}
                    {%- set iG = rgb[1] -%}
                    {%- set iB = rgb[2] -%}
                    {%- if iR + iG + iB == 0: -%}
                    {%-    set xy = 0.0, 0.0, 0 -%}
                    {%- endif -%}
                    {%- set R = iR / 255 -%}
                    {%- set B = iB / 255 -%}
                    {%- set G = iG / 255 -%}
                    {%- set R = ((R + 0.055) / (1.0 + 0.055) ** 2.4) if (R > 0.04045) else (R / 12.92) -%}
                    {%- set G = ((G + 0.055) / (1.0 + 0.055) ** 2.4) if (G > 0.04045) else (G / 12.92) -%}
                    {%- set B = ((B + 0.055) / (1.0 + 0.055) ** 2.4) if (B > 0.04045) else (B / 12.92) -%}
                    {%- set X = R * 0.664511 + G * 0.154324 + B * 0.162028 -%}
                    {%- set Y = R * 0.283881 + G * 0.668433 + B * 0.047685 -%}
                    {%- set Z = R * 0.000088 + G * 0.072310 + B * 0.986039 -%}
                    {%- set x = X / (X + Y + Z) -%}
                    {%- set y = Y / (X + Y + Z) -%}
                    {%- set Y = 1 if Y > 1 else Y -%}
                    {%- set brightness = (Y * 255)|round -%}
                    {%- set xy = (x|round(3) * 65535)|int, (y|round(3) * 65535)|int, brightness|int -%}
                      {{ xy[0] }}
                {%- endmacro -%}
                {{ hs_to_xy(h, s, 100) }}
              - >-
                {%- macro hs_to_xy(iH, iS, iV) -%}
                    {%- set h = iH / 360 -%}
                    {%- set s = iS /100 -%}
                    {%- set v = iV / 100 -%}
                    {%- if s == 0.0: -%}
                    {%-     set rgb = v, v, v -%}
                    {%- endif -%}
                    {%- set i = (h*6.0)|int -%}
                    {%- set f = (h*6.0) - i -%}
                    {%- set p = v*(1.0 - s) -%}
                    {%- set q = v*(1.0 - s*f) -%}
                    {%- set t = v*(1.0 - s*(1.0-f)) -%}
                    {%- set i = i%6 -%}
                    {%- if i == 0: -%}
                    {%-     set fRGB = v, t, p -%}
                    {%- elif i == 1: -%}
                    {%-     set fRGB = q, v, p -%}
                    {%- elif i == 2: -%}
                    {%-     set fRGB = p, v, t -%}
                    {%- elif i == 3: -%}
                    {%-     set fRGB = p, q, v -%}
                    {%- elif i == 4: -%}
                    {%-     set fRGB = t, p, v -%}
                    {%- elif i == 5: -%}
                    {%-     set fRGB = v, p, q -%}
                    {%- endif -%}
                    {%- set rgb = ((fRGB[0] * 255)|int, (fRGB[1] * 255)|int, (fRGB[2] * 255)|int) -%}
                    {%- set iR = rgb[0] -%}
                    {%- set iG = rgb[1] -%}
                    {%- set iB = rgb[2] -%}
                    {%- if iR + iG + iB == 0: -%}
                    {%-    set xy = 0.0, 0.0, 0 -%}
                    {%- endif -%}
                    {%- set R = iR / 255 -%}
                    {%- set B = iB / 255 -%}
                    {%- set G = iG / 255 -%}
                    {%- set R = ((R + 0.055) / (1.0 + 0.055) ** 2.4) if (R > 0.04045) else (R / 12.92) -%}
                    {%- set G = ((G + 0.055) / (1.0 + 0.055) ** 2.4) if (G > 0.04045) else (G / 12.92) -%}
                    {%- set B = ((B + 0.055) / (1.0 + 0.055) ** 2.4) if (B > 0.04045) else (B / 12.92) -%}
                    {%- set X = R * 0.664511 + G * 0.154324 + B * 0.162028 -%}
                    {%- set Y = R * 0.283881 + G * 0.668433 + B * 0.047685 -%}
                    {%- set Z = R * 0.000088 + G * 0.072310 + B * 0.986039 -%}
                    {%- set x = X / (X + Y + Z) -%}
                    {%- set y = Y / (X + Y + Z) -%}
                    {%- set Y = 1 if Y > 1 else Y -%}
                    {%- set brightness = (Y * 255)|round -%}
                    {%- set xy = (x|round(3) * 65535)|int, (y|round(3) * 65535)|int, brightness|int -%}
                      {{ xy[1] }}
                {%- endmacro -%}
                {{ hs_to_xy(h, s, 100) }}
              - 5
        set_temperature:
          service: zha.issue_zigbee_group_command
          data_template:
            group: 0x0001
            cluster_id: 768
            command: 10
            args:
              - "{{ color_temp }}"  # color temperature (153-500)
              - 5                   # transition time

Yep, jinja creates a string, period.

This sort of thing is a lot easier if done in a Python Script. I.e., instead of calling the zha.issue_zigbee_group_command service directly, rather call a python_script.xxx. Then in that script, do the calculations and then call the zha.issue_zigbee_group_command service from inside it.