Using template for xy_color

Hi, I’m trying to use templates to ‘copy’ light states. In this case I want to copy light.bank to light.televisie
I have it working for brightness and color_temp, but not for color
What I have so far is:

testscript:
  sequence:
    - service: light.turn_on
      entity_id: light.televisie
      data_template:
        brightness: '{{states.light.bank.attributes.brightness}}'
        color_temp: '{{states.light.bank.attributes.color_temp}}'
        xy_color: '{{states.light.bank.attributes.xy_color}}'

the xy_color template returns for example: [0.5017, 0.4152], but in the log i have the following error:

17-02-27 16:25:09 ERROR (MainThread) [homeassistant.core] Invalid service data for light.turn_on: None for dictionary value @ data['xy_color']. Got '[0.5017, 0.4152]'

Any suggestions?

Hey I think the problem here is that Jinja renders the whole thing as a string. So you only get the string “[0.5017, 0.4152]” but not the actual list. You could try to seperate x and y and put those there like that e.g.

xy_color: '[{{states.light.bank.attributes.x_color}}, {{states.light.bank.attributes.y_color}}]'

~Cheers

Hi,

Unfortunately there are no attributes like x_color and y_color.
However i tried the following because it is essentially the same i think

xy_color: '[{{states.light.bank.attributes.xy_color | first}},{{states.light.bank.attributes.xy_color | last }}]'

returns the same error:

17-02-27 21:09:30 ERROR (MainThread) [homeassistant.core] Invalid service data for light.turn_on: None for dictionary value @ data['xy_color']. Got '[0.5017, 0.4152]'

As you show, templates require quotes. And that might be part of why HASS is seeing a string. But I wonder if some alternate methods of expressing the data might work.
Like

xy_color: >-
    [{{states.light.bank.attributes.xy_color | first}},{{states.light.bank.attributes.xy_color | last }}]

or

xy_color: 
  - {{states.light.bank.attributes.xy_color | first}}
  - {{states.light.bank.attributes.xy_color | last }}

Hi

I tried @ih8gates suggestion, but didn’t work.

However this worked!!

testscript:
  sequence:
    - service: light.turn_on
      entity_id: light.televisie
      data_template:
        brightness: '{{states.light.bank.attributes.brightness}}'
        color_temp: '{{states.light.bank.attributes.color_temp}}'
        xy_color: ['{{states.light.bank.attributes.xy_color[0]|float}}','{{states.light.bank.attributes.xy_color[1]|float}}']

quotes should only be around the template values which are also converted to floats :slight_smile:
I guess the same should work for rgb_color but instead of using float, you use int.

3 Likes

For future reference you can use the template editor (http://(your ip):8123/dev-template) when working to see if your template works, hopefully you knew about that :slight_smile:. Then for syntax questions you can always google to find answers for jinja.

Not just directed at you btw, just for anyone reading.

Hi,

I knew that, but the problem wasn’t that the template did not work, it was in an incorrect format for the service using it. (string instead of a list of floats)

1 Like

Thanks for posting the solution. You can also mark it as such for future reference if someone else is struggeling with this. Makes it easiert to find it :slight_smile:

~Cheers

1 Like

This solution worked for me - thank you!