How to get Lightcolor in an Automation and change a Value of it?

Hi, I have tried to automate my own colorloop, because these cheap wifi led strips had some trouble with transition settings or to combine led effects with brightness.

My Scenario is simple. The Lightstrip is in a rainbow colorloop effect on full brightness. When I start a movie, it should stay in rainbow effect mode but dimm the brightness to 50%.

In the colorloop effect, the brightness stays at 255. When I leave effect empty, the lightstrip dimms correctly, but stays in a color. So my thought is to create my own coloreffect.

First I will try to trigger every 5 seconds an event, that grabs the actual color of the strip and add +5 to red. But I don’t know how to write it down:

rgb_color:
  - '{{ (states.light.livingroom_ambient.attributes.rgb_color.split(",")[0]|int) + 1 }}'
  - '{{ (states.light.livingroom_ambient.attributes.rgb_color.split(",")[1]|int) }}'
  - '{{ (states.light.livingroom_ambient.attributes.rgb_color.split(",")[2]|int) }}'

In the home assistant logs comes an error, that says:

Error rendering data template: UndefinedError: ‘tuple object’ has no attribute ‘split’

How can I turn a tuple object in an array or how would you write this lines?

My next steps would be to check which colorvalue is the highest and turn the other down to zero, while turn the highest up to 255 and then turn down that one and turn up the next color. Hope it is understandable to you :slight_smile:

rgb_color:
  - '{{ states.light.livingroom_ambient.attributes.rgb_color[0] + 1 }}'
  - '{{ states.light.livingroom_ambient.attributes.rgb_color[1] }}'
  - '{{ states.light.livingroom_ambient.attributes.rgb_color[2] }}'

the lights don’t change… I don’t get it :frowning:

Can you share the whole automation? There might be a different issue.

- id: '1540597553796'
  alias: Test Turn Into Red
  trigger:
    platform: time
    seconds: /5
  action:
   - service: light.turn_on
     data_template:
        entity_id: light.livingroom_ambient
        rgb_color:
          - '{{ states.light.livingroom_ambient.attributes.rgb_color[0] + 1 }}'
          - '{{ states.light.livingroom_ambient.attributes.rgb_color[1] }}'
          - '{{ states.light.livingroom_ambient.attributes.rgb_color[2] }}'

Sometimes the color jumps back and forth, sometimes the log says, that I’m out of the range ( 0-255 )

The light component uses hs_color internally, which means rgb_color is converted to hs_color before changing the color of the light. Then the hs_color of the light is converted to rgb_color before setting the attributes. Since there are two conversions it’s possible incrementing just the red color by one is getting lost in the noise. You may need to increment by a larger number (such as 5 as you had originally said.)

Also, you have nothing to prevent the value from going above the max of 255.

Maybe try this:

- id: '1540597553796'
  alias: Test Turn Into Red
  trigger:
    platform: time
    seconds: /5
  action:
    service: light.turn_on
    data_template:
      entity_id: light.livingroom_ambient
      rgb_color:
        - "{% set r = states.light.livingroom_ambient.attributes.rgb_color[0]
                      + 5 %}
           {{ r if r <= 255 else 0 }}"
        - "{{ states.light.livingroom_ambient.attributes.rgb_color[1] }}"
        - "{{ states.light.livingroom_ambient.attributes.rgb_color[2] }}"

Thank you for your example, but it doesn’t change lights as expected. I added the lines for green and blue. Now the lights only rotates in three steps: red - green - blue. No colors between. Seems weired.

- id: '1540597553796'
  alias: Test Turn Into Red
  trigger:
    platform: time
    seconds: /3
  action:
    service: light.turn_on
    data_template:
      entity_id: light.livingroom_ambient
      brightness_pct: 100
      rgb_color:
        - "{% set r = states.light.livingroom_ambient.attributes.rgb_color[0]
                  + 3 %}
       {{ r if r <= 255 else 0 }}"
        - "{% set g = states.light.livingroom_ambient.attributes.rgb_color[1]
                  + 3 %}
       {{ g if g <= 255 else 0 }}"
        - "{% set b = states.light.livingroom_ambient.attributes.rgb_color[2]
                  + 3 %}
       {{ b if b <= 255 else 0 }}"

as you can see, I played with the values. It seems to be similar if it should change 5 steps or only 3.

and I don’t know how to write it, but that is my first complete try, but the log tells me an error:

{% set r = states.light.livingroom_ambient.attributes.rgb_color[0] %}
  {% set g = states.light.livingroom_ambient.attributes.rgb_color[1] %}
  {% set b = states.light.livingroom_ambient.attributes.rgb_color[2] %}
  {% if r == 255 && g < 255 %}
      {% set ng = g + 5 %}
      rgb_color:
        - 255
        - {{ ng if ng < 255 else 255 }}
        - 0
  {% elif r > 0 && g == 255 %}
      {% set nr = r - 5 %}
      rgb_color:
        - {{ ng if nr > 0 else 0 }}
        - 255
        - 0
  {% if g == 255 && b < 255 %}
      {% set nb = b + 5 %}
      rgb_color:
        - 0
        - 255
        - {{ ng if nb < 255 else 255 }}
  {% elif g > 0 && b == 255 %}
      {% set ng = g - 5 %}
      rgb_color:
        - 0
        - {{ ng if ng > 0 else 0 }}
        - 255
  {% if b == 255 && r < 255 %}
      {% set nr = r + 5 %}
      rgb_color:
        - {{ nr if nr < 255 else 255 }}
        - 0
        - 255
  {% elif b > 0 && r == 255 %}
      {% set nb = b - 5 %}
      rgb_color:
        - 255
        - 0
        - {{ nb if nb > 0 else 0 }}
  {% endif %}

and here is a second try, that won’t work:

- id: '1540597553796'
 alias: Test Turn Into Red
 trigger:
  platform: time
  seconds: /2
 action:
  service: light.turn_on
  data_template:
    entity_id: light.livingroom_ambient
    brightness_pct: 100
    rgb_color:
    - "{% set r = states.light.livingroom_ambient.attributes.rgb_color[0] %}
       {% set g = states.light.livingroom_ambient.attributes.rgb_color[1] %}
       {% set b = states.light.livingroom_ambient.attributes.rgb_color[2] %}
       {% if (r == 255 && g < 255) || (b > 0 && r == 255) %}
         255
       {% elif r > 0 && g == 255 %}
         {% set nr = r - 5 %}
         {{ nr if nr > 0 else 0 }}
       {% if (g == 255 && b < 255) || (g > 0 && b == 255) %}
         0
       {% if b == 255 && r < 255 %}
         {% set nr = r + 5 %}
         {{ nr if nr < 255 else 255 }}
       {% endif %}"
    - "{% set r = states.light.livingroom_ambient.attributes.rgb_color[0] %}
       {% set g = states.light.livingroom_ambient.attributes.rgb_color[1] %}
       {% set b = states.light.livingroom_ambient.attributes.rgb_color[2] %}
       {% if r == 255 && g < 255 %}
         {% set ng = g + 5 %}
         {{ ng if ng < 255 else 255 }}
       {% elif (r > 0 && g == 255) || (g == 255 && b < 255) %}
         255
       {% elif g > 0 && b == 255 %}
         {% set ng = g - 5 %}
         {{ ng if ng > 0 else 0 }}
       {% if (b == 255 && r < 255) || (b > 0 && r == 255) %}
         0
       {% endif %}"
    - "{% set r = states.light.livingroom_ambient.attributes.rgb_color[0] %}
       {% set g = states.light.livingroom_ambient.attributes.rgb_color[1] %}
       {% set b = states.light.livingroom_ambient.attributes.rgb_color[2] %}
       {% if (r == 255 && g < 255) || (r > 0 && g == 255) %}
         0
       {% if g == 255 && b < 255 %}
         {% set nb = b + 5 %}
         {{ nb if nb < 255 else 255 }}
       {% elif (g > 0 && b == 255) || (b == 255 && r < 255) %}
         255
       {% elif b > 0 && r == 255 %}
         {% set nb = b - 5 %}
         {{ nb if nb > 0 else 0 }}
       {% endif %}"

thats the error:

Invalid config for [automation]: invalid template (TemplateSyntaxError: unexpected char ‘&’ at 226) for dictionary value @ data[‘action’][0][‘data_template’][‘rgb_color’]. Got None.

Color cycles are much easier with hs_color, I posted an example here: Set a random color on a light

1 Like

That looks really simpler as my tryings :smiley: I will try your snippet next. Looks promising!

FYI, you can’t use Jinja template to conditionally include YAML options. That’s why the first one didn’t work. And this one doesn’t work, at least, because in Jinja and is and not &&, and or is or not ||.

Thank you for your reply. I don‘t know this language and its hard to try and error. Is there a way to simply code and learn?