RGB condition

Could anyone help how to set RBG conditions?

I would like to run an automation that takes the color of a bulb as condition, and trigger a new scene.

Something like…

condition: state
entity_id: light.hue_go_1
state: On

  • data:
    rgb_color: [255, 207, 120]

Use a template condition looking at the attributes of the entity.

Hi marc

Would you mind writing how this would look?

Ummm, yeah :laughing:

condition: template
value_template: "{{ is_state_attr('light.hue_go_1' , 'rgb_color' , '[255, 207, 120]' }}"

You might have to play with it a bit in the template editor, not sure how it will translate the array as a comparable attribute.

1 Like

I think that’s close, but the rgb_color attribute is a tuple, not a string or even a list, so:

condition: template
value_template: "{{ is_state_attr('light.hue_go_1' , 'rgb_color' , (255, 207, 120)) }}"

And the attribute won’t exist when the light is off, so as you probably surmised, you don’t need to check for “on” explicitly.

EDIT: Fixed typo.

1 Like

Phil, your point of the state “on” is obvious but was overlooked, thanks :wink: .

Having said so, my HA wont accept the string:
condition: template
value_template: “{{ is_state_attr(‘light.hue_go_1’ , ‘rgb_color’ , (255, 207, 120) }}”

It say: Message malformed: not a valid value for dictionary value @ data[‘condition’][0][‘condition’]

That’s due to a typo. The is_state_attr function is missing a closing bracket.

Add another ) to the existing one, as shown here:

condition: template
value_template: "{{ is_state_attr('light.hue_go_1' , 'rgb_color' , (255, 207, 120)) }}"
1 Like

Oops, my bad. I’ll fix my comment for future reference. Thx!

amazing ! Thanks… got it working now.

Now, i need to see if I can get some if, else in to this… Phhyy. I might call in a favor again :wink:

I think I understand the main difference between a tuple vs a list (mutability; nicely summarized here) but help me understand the syntax. When defining it in YAML it’s with square-brackets:

[215, 125, 125]

whereas in the Jinja2 template it’s with brackets:

(125, 125, 125)

Is that simply due to the convention used by the specific interpreter? In other words, YAML represents tuples and lists the same way, with square-brackets, but Jinja2 is pythonic; lists are represented differently from tuples (square-brackets vs regular brackets).

YAML, AFAIK, doesn’t have such a thing as a tuple.

When using a template, to YAML, it’s just a string. It has no idea that it’s Jinja, JSON, or whatever. E.g., notice: value_template: "{{ ... }}". See the quotes? It’s a string!

Now that string gets interpreted as a Jinja expression. And in Jinja (at least in this Python based implementation) there are both Jinja and Python constructs. (E.g., is_state_attr() is really a Python function. See this piece of code.) And objects/variables are Python objects. E.g., the rgb_color attribute of a light entity is a Python tuple. So, when you’re using the is_state_attr() function with this attribute you need to compare it against a tuple – i.e., (255, 207, 120), not [255, 207, 120], because that’s a list, and in Python (255, 207, 120) != [255, 207, 120].

1 Like

OK, so it’s because is_state_attr is a python function and rgb_color is a tuple so the supplied argument must also be a tuple.

I did a search and, like you said, YAML has no means of defining a tuple and so it must be defined as a list. That explains why rgb_color expects a tuple yet this is the way to define it (like a list):

  rgb_color: [125, 125, 125]

Well, that’s a different story. That’s a service call parameter, which is controlled by a “schema”. And in this case it’s this piece of code. So, yes, since YAML can only represent a list, you have to enter it that way. But the schema converts it to a tuple (behind the scenes.)

Ok, 3 hours and I have to give up… hopefully one of you sharp minds can assist finalizing this with me?

Basically this is the mission. Get an automation running where i get to toggle through different hue_scenes depending on what state of rgb color the entity has.

{% if (check the rgb color as above on a specific light entity) %}
  true = run NEXT hue.scene call service with a specific group_name and scene_name
{% else %}
  false = run START hue.scene call service with a specific group_name and scene_name
{% endif %}


Ok, so you’re not really looking for an automation condition after all. It seems you want to use data_template in a service call to figure out which scene to apply. At least that’s what it seems like you’re looking for.

I don’t use hue, so I’m not sure what to tell you. E.g., how do you know what scene has been previously applied? I’m guessing it’s more than just knowing if you’ve applied the last scene (and therefore should start at the “first” scene again), but the template would also have to be able to figure out which scene is “active” so it can figure out what the “next” scene is in the other cases.

Maybe what you should do is use a counter. Have the automation first increment the counter if it has not reached it’s maximum value, or reset it if it has, then use that in the automation to select a scene (group and/or scene name) from a list or lists. Something like:

- service_template: >
    {% if states('counter.scene_index')|int == N - 1 %}
    {% else %}
    {% endif %}
  entity_id: counter.scene_index
- service: hue.hue_activate_scene
    group_name: WHATEVER
    scene_name: >
      {{ ('scene1', 'scene2', ... 'sceneN')
         [states('counter.scene_index')|int] }}

If the group name also changes then do the same thing for it (but, of course, with a different list.)