Help, Check current light color and change it

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 %}

you could also use scenes, which are tailored for setting lights and its attributes, like this

- name: Opstaan
  entities:
    light.kist:
      state: on
      transition: 4
      brightness: 47
      hs_color: [14,83]
    light.bedside_table:
      state: on
      transition: 4
      brightness: 30
      rgb_color: [255,94,42]

to turn_on the scene, you’d use another service:

action:
  service: scene.turn_on
  data_template:
    {% if state_attr('light.sofa','rgb_color') == (255,0,0) %} scene.blue
    {% elif state_attr('light.sofa','rgb_color') == (0,255,0) %} scene.yellow
    {% else %} scene.red
    {% endif %}

like this:



of course you’d need to create these 3 scenes:

scene:
  name: red
  entities:
    light.sofa:
      state: on
      transition: 4
      brightness: 47 #set to whatever you want or leave out
      rgb_color: [255,0,0]

you can use all other color variables available to the light, and if no colors are available use color_temp.

more info on scenes:

example of an automation or script in this case,calling scenes:

lighting_home_theater:
  alias: Lighting Hometheater
  sequence:
    - service: input_select.select_option
      data:
        entity_id: input_select.activity_lighting
        option: Home theater
    - service: scene.turn_on
      entity_id: scene.home_theater_off
    - condition: template
      value_template: >
        {{ is_state('binary_sensor.home_theater_lux_input', 'on')}}
    - service: scene.turn_on
      entity_id: scene.home_theater_on

Eventually its working, thanks guys! here is my script which change the color in a loop:

 - 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.rgb_color == (255,0,0) %} blue
            {% elif states.light.sofa.attributes.rgb_color == (0,0,255) %} green
            {% elif states.light.sofa.attributes.rgb_color == (0,255,0) %} yellow
            {% else %} red
            {% endif %}

great, glad it works for you.

you could write it like this too:

condition:
  condition: template
  value_template: >
    {{ is_state('light.sofa','on')}}
action:
  service: light.turn_on
  data_template:
    entity_id: light.sofa
    color_name:  >
      {% if state_attr('light.sofa','rgb_color') == (255,0,0) %} blue
      {% elif state_attr('light.sofa','rgb_color') == (0,0,255) %} green
      {% elif state_attr('light.sofa','rgb_color') == (0,255,0) %} 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.

cheers!

If you’re looking to add it as a template sensor, you can always do:

- platform: template
  sensors:
    sofa_light_rgb_name:
      friendly_name: Sofa Light Color
      value_template: >
        {% elif state_attr('light.sofa','rgb_color') == (242,255,255) %} azure 
        {% elif state_attr('light.sofa','rgb_color') == (255,25,71) %} crimson 
        {% else %} unknown
        {% endif %}

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:

- platform: template
  sensors:
    sofa_light_rgb_decimal:
      friendly_name: Soft Light RGB Decimal
      value_template: >-
        {{ state_attr('sofa_light', 'rgb_color') }}

Sorry to bump this, but wanted to vet an issue I encountered:

The color_name “green” is actually 0,0,128, so using the code above doesn’t correctly cycle for me. I either have to change the rgb value to 128, OR use the color_name “lime” instead, which appears to be 0,0,255. Once one of those changes is made, it seems to work.

Is there a good reason why the green channel set to 255 works with color_name green for OP? I’m trying to understand this better.