Pull color state from light

Basically, what I want to do is take the color state from an existing light and pretty much copy/paste it into my other lights. I currently have remote light sensors and I pass the detected color to an MQTT light.

Here’s what I threw together untested, to get you an idea of what I’m trying to do.

- service: light.turn_on
  data_template:
    entity_id: light.bedroom_up
    brightness: 128
    rgb_color: '{{states.light.rgb_sensor.state.color}}'

I must say that YAML is one of the least-intuitive (non-intentional) languages I’ve ever used. When it comes to moving data around, it feels like disassembling and rebuilding a car engine just to change the tire.

I can’t try as i’m not home, but your rgb_color would need to be in the format (0,0,0)
Not sure what data your sensor has, but if I wanted to get the data out of another light, the data would be like this:
{{states.light.bedroom_up.attributes.rgb_color}}

Cool. Gave that a shot.

Invalid service data for light.turn_on: None for dictionary value @ data['rgb_color']. Got ['states.light.rgb_sensor.state.rgb_color']

If you hate yaml you can try NodeRed

Can you share the exact YAML code? It should have worked if you had exactly what you posted first, but changed states.light.rgb_sensor.state.color to states.light.rgb_sensor.attributes.rgb_color, assuming, of course, that light has that attribute. Or, you could use something like:

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

You got this error because you left out the word attributes. If you look at @lolouk44’s post, he has the correct syntax:

So yours should be:

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

EDIT: You can also use @pnbruckner’s method, it gets you the same result.

1 Like

OK so I’m just about there. Here’s my code:

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") }}]

And here’s my result:

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

I tried with and without apostrophes around the curly braces, and with/without the square brackets. You guys are definitely very helpful. I’ll be sure to post the working version when it gets going.

From what I can tell, it’s basically “there”.

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.