Pull color state from light

Replace the square brackets with single quotes. I.e.:

rgb_color: '{{ state_attr("light.rgb_sensor", "rgb_color") }}'

I tried that first and got:

Invalid service data for light.turn_on: None for dictionary value @ data['rgb_color']. Got '(228, 223, 255)'

Which was why I added the square brackets. When that didn’t work, I took away the quotes and got the result posted earlier.

You tried it with single quotes or double quotes? If you tried either of these it wouldn’t work:

rgb_color: "{{ state_attr("light.rgb_sensor", "rgb_color") }}"
rgb_color: '{{ state_attr('light.rgb_sensor', 'rgb_color') }}'

Or you could skip the state_attr function and do this:

rgb_color: '{{ states.light.rgb_sensor.attributes.rgb_color }}'

Another quick copy-paste.

matchlights:
  alias: Match Lighting
  sequence:
    - service: light.turn_on
      data_template:
        entity_id: light.bedroom_up
        brightness: 128
        rgb_color: '{{ state_attr("light.rgb_sensor", "rgb_color") }}'

This is the one that got me

Invalid service data for light.turn_on: None for dictionary value @ data['rgb_color']. Got '(228, 223, 255)'

Your second option (without the function) game me the same issue.

Oh, interesting. So the template is working, it’s just the value isn’t appropriate for rgb_color in this context. Hmm. Let me do some head scratching… :wink:

Ok, maybe not the most elegant, but try:

rgb_color: '{{ state_attr("light.rgb_sensor", "rgb_color").replace("(","[").replace(")","]") }}'

Or maybe even:

rgb_color: '{{ state_attr("light.rgb_sensor", "rgb_color").replace("(","").replace(")","") }}'

Error rendering data template: UndefinedError: 'tuple object' has no attribute 'replace'

Got it for both versions.

Ok, it’s getting clearer… Try:

rgb_color: '{{ state_attr("light.rgb_sensor", "rgb_color")|list }}'

Unfortunately I don’t have an rgb light I can experiment on. :slight_smile:

Invalid service data for light.turn_on: None for dictionary value @ data['rgb_color']. Got '[228, 223, 255]'

OK, this is when it needs the patrick meme.

It’s like it’s asking for pasta an it gets upset because it receives “italian noodles”

Could have to do with the placement of the single quotes.

No, the outside (single) quotes are needed. For me, at least, it’s trying to figure out what rgb_color wants. Let me look at some code and try a few things. If I come up with something better I’ll let you know. Maybe someone who has been through this will jump in in the interim…

OK. Well thanks for your help, nevertheless!

I’ll look around, too. But I was on lunch break so I’ll be less responsive.

Ok, again, there may be a more elegant way to do this, but maybe try:

rgb_color: '{{ state_attr("light.rgb_sensor", "rgb_color")|string|replace("(","")|replace(")","") }}'

state_attr is returning a tuple, which, when (by default is) turned into a string, comes out “(1, 2, 3)”. rgb_color wants a “list” of three numbers. Although I’m not 100% sure, in YAML, I believe that can look like either of these:

rgb_color:
  - 1
  - 2
  - 3

or

rgb_color: 1, 2, 3

So, to convert the tuple returned by state_attr (or states.xyz.attributes.xxx) into a string of three numbers separated by commas (with no parentheses or brackets), my latest proposed solution uses the string filter (to explicitly convert the tuple to its string representation) followed by two replace filters (to get rid of the parentheses.)

Invalid service data for light.turn_on: None for dictionary value @ data['rgb_color']. Got '228, 223, 255'

What…even…

Added brackets to the outside and got this:

Invalid service data for light.turn_on: None for dictionary value @ data['rgb_color']. Got ['228, 223, 255']

At this point it looks identical to what it’s looking for.

Here’s what my old code looked like when I used 3 separate sensors.

rgb_color: ['{{ states.sensor.red_sensor.state | int }}', '{{ states.sensor.green_sensor.state | int}}', '{{ states.sensor.blue_sensor.state | int}}']

Whoa! I’ll admit I’m pretty new to this, so I’m sure I’m missing something. I was under the impression that when you use data_template, that the template needs to be quoted, either directly using quotes, or by using the “>” technique. Maybe that’s wrong.

For grins, what does this do?

rgb_color: {{ state_attr("light.rgb_sensor", "rgb_color")|list }}

Or even this?

rgb_color: {{ state_attr("light.rgb_sensor", "rgb_color") }}

I.e., without the quotes around the template.

Invalid service data for light.turn_on: None for dictionary value @ data['rgb_color']. Got ['(228,223,255)']

Invalid service data for light.turn_on: None for dictionary value @ data['rgb_color']. Got ['(228,223,255)']

Basiclly the same error both times.

I know this is probably more of a JSON thing than a YAML thing, but its picky and complex formatting is the main thing I have against YAML.

I wonder if rgb_color needs to be separated into red, green, and blue.

If you look at the old version of mine, it basically would have resulted in [‘255’, ‘255’, ‘255’]

I tried doing replacement for swapping , with ', ’
But it didn’t look like it actually change anything.

I’m not sure if it passes the data as a string, or tries to pass its constituent parts, but it might be looking for [int,int,int] and we are giving it [‘string’]

jinja does not return lists. Getting this method to work will not be easy. Your best option is to piece out each item like you had before:

rgb_color: [ '{{ state_attr("light.rgb_sensor", "rgb_color")[0] }}',  '{{ state_attr("light.rgb_sensor", "rgb_color")[1] }}',  '{{ state_attr("light.rgb_sensor", "rgb_color")[2] }}' ]

This of course assumes that the state_attr returns a tuple or list. You may need to convert to a tuple or a list but I doubt it.

5 Likes

What is the output of your sensor in the template editor?

Also, you can use the template editor to test your templates before moving them into your YAML config file. It saves a lot of restarts. Most of the time if they work in the template tool they just copy/paste into the config file, exception is changing ‘quotes’ to “quotes” or visa versa.

Sure enough. That seems to have worked. I’ll try it on the rest of my lights.

matchlights:
  alias: Match Lighting
  sequence:
    - service: light.turn_on
      data_template:
        entity_id: light.bedroom_up
        brightness: 128
        rgb_color: [ '{{ state_attr("light.rgb_sensor", "rgb_color")[0] }}',  '{{ state_attr("light.rgb_sensor", "rgb_color")[1] }}',  '{{ state_attr("light.rgb_sensor", "rgb_color")[2] }}' ]

Boy do I love C++ so much more.

I’ll post back when I verify on my other lights.

4 Likes

Jinja is limited in its functionality. It’s a language meant for displaying information so it always expects to output 1 value.

Yep, that seems to have done it!

Thanks guys!

Would still be killer if they used a scripting language like javascript or maybe some other markup language. I’m starting to make heavier use of the python script component as well. Madness.

2 Likes