Hi all, New to this, I"ll appreciate if anyone can tell me what should I enter instead of states.light.sofa.attributes.rgb_color? since it returned blank
- alias: Change sofa light color when on
trigger:
- platform: state
entity_id: sensor.sonoff_hold
to: 'HOLD'
condition:
condition: state
entity_id: light.sofa
state: 'on'
action:
service: light.turn_on
data_template:
entity_id: light.sofa
rgb_color: >
{% if states.light.sofa.attributes.rgb_color == (255,0,0) %} [0,255,0]
{% elif states.light.sofa.attributes.rgb_color == (0,255,0) %} [0,0,255]
{% else %} [255,0,0]
{% endif %}
states.light.sofa.attributes.rgb_color should work if its a color light. Try checking the state (in the dev tools) to see what it shows when you have it setup how you want it.
Also when you say blank is that using the template editor? the reason i ask is because there is some spacing issues in your code. everything under trigger needs one more space, everything under action needs one less space
Actually, no. Although for aesthetic reasons I would indent it like you say, the way it is is ok. It only matters that items at the same “level” and under the same “parent” item are indented the same amount.
Unfortunately you can’t specify rgb_color with a single template. A template can only return a single string. So although it might look like the template is resulting in a list of numbers, it’s actually returning a single string whose characters “look like” a list of numbers. To be clear, it’s returning, for example, '[0,255,0]', not [0,255,0].
The easiest way to do what you want is to use color_name instead of rgb_color. See documentation here.
Thanks! there is some progress, it does switch from red to blue, but doesn’t go any further.
Advise?
- alias: Change sofa light color when on
trigger:
- platform: state
entity_id: sensor.sonoff_hold
to: 'HOLD'
condition:
condition: state
entity_id: light.sofa
state: 'on'
action:
service: light.turn_on
data_template:
entity_id: light.sofa
color_name: >
{% if states.light.sofa.attributes.color_name == red %} blue
{% elif states.light.sofa.attributes.color_name == blue %} yellow
{% else %} red
{% endif %}
I’m pretty sure, even if you use color_name to set a light’s color when turning it on, it will not have an attribute named color_name. It will only have hs_color, xy_color and rgb_color. (That’s because, internally, the light component uses hs_color, so when you set the color using color_name, it is first converted to rgb_color, and then to hs_color.) But every color name has a corresponding rgb_color value – see here.
So, change your tests back to the rgb_color attribute, but use color names to set the color:
color_name: >
{% if states.light.sofa.attributes.rgb_color == (255,0,0) %} blue
{% elif states.light.sofa.attributes.rgb_color == (0,0,255) %} yellow
{% else %} red
{% endif %}
Using the condition in a template makes it easy to check in the dev-template tool.
The final template in the action could also be written like:
color_name: >
{% if is_state_attr('light.sofa','rgb_color',(255,0,0)) %} blue
{% elif is_state_attr('light.sofa','rgb_color',(0,0,255)) %} green
{% elif is_state_attr('light.sofa','rgb_color',(0,255,0)) %} yellow
{% else %} red
{% endif %}
or create a mapper, but that might be overkill here. Just letting you know there’s more than one way.
color_name: >
{% set mapper = { (255,0,0): 'blue',
(0,0,255): 'green',
(0,255,0): 'yellow'
} %}
{% set state = state_attr('light.sofa','rgb_color') %}
{% set colorname = mapper[state] if state in mapper else 'red' %}
{{colorname}}
btw, using states() has its advantages, because it won’t error out, when the state isn’t initialized yet, while states.entity.state will.
HA provides a link to a list of color names for RGB attributes at Light - Home Assistant but you’ll need to double-check the rgb color attribute for your light as I’ve found that the RGB decimals and the color name don’t quite match up with what HA says a color is.
You can create another sensor to find out the RGB state (am sure you could do this as a template, but this might be easier to decipher on your dashboard by integrating both color name and rgb decimal. Below is the code to create a template sensor for the rgb decimal: