Setting RGB light color based on sensor value

Hello,

I’m currently trying to create a custom color temperature slider for WWA LED strip (cold white, warm white and amber instead of RGB). For that I have basically created template sensors for the RGB channels based on the input of a color temperature slider.
I would like to change the color of my lights when I change the slider position. I have created an automation but this results in a config error:

  alias: CT WWA
  trigger:
  - entity_id: input_number.ct_slider
    platform: state
  condition: []
  action:
  - data:
      rgb_color: 
        - {{ states('sensor.red') | int }}
        - {{ states('sensor.green') | int }}
        - {{ states('sensor.blue') | int }}
    entity_id: light.wled2
    service: light.turn_on

Thank you !

you first need to use data_template if you’re going to use a template :wink:
Try this (untested):

  - data_template:
      rgb_color: "[{{ states('sensor.red') | int }},{{ states('sensor.green') | int }},{{ states('sensor.blue') | int }}]"
    entity_id: light.wled2
    service: light.turn_on

Thank you for your reply. That got rid of the error indeed ! However, my lights do not turn on … I will investigate further.

Do you need to provide a brightness level by chance?

I did not think of that, but neither brightness or brightness_pct did the trick. When setting rgb_color: [255 0 0], the LED strip reacts normally.

Make sure you are getting the sensor values correctly.

Go Developer Tools, Templates, and type:

rgb_color: [{{ states('sensor.red') | int }},{{ states('sensor.green') | int }},{{ states('sensor.blue') | int }}]

Does doing this give you a valid rgb_color line (e.g. “rgb_color: [x,y,z]”)?

Also, the suggestion above used “” around the rgb_color line. Don’t think quotes necessary for this.

Thank you for the reply. So in the templating tool, the template (without the quotes) yields something like rgb_color: [0,195,59], so this looks good. However, this results in the following error (which was my initial error I believe) :

Error loading /config/configuration.yaml: invalid key: "OrderedDict([("states('sensor.red' | int)", None)])"
  in "/config/automations.yaml", line 170, column 0

This is what i use:

      - service: light.turn_on
        data_template:
          entity_id: light.milight_gu10
          rgb_color:
            - '{{ states.sensor.random_rgb.state.split(",")[0] }}'
            - '{{ states.sensor.random_rgb.state.split(",")[1] }}'
            - '{{ states.sensor.random_rgb.state.split(",")[2] }}'

So i think your first code works when you use data_template

1 Like

The value for red (i.e., the first zero in the rgb color code) is zero. Is that the way is it supposed to be, or is the “0” there because no value is coming through.

Is line 170 in your automations.yaml the rgb_color line?

Also, the error message shows your code is messed up. Make sure that the code you copied and pasted into the template tool is EXACTLY the same as it is in your automation. Cut and paste. The code in the error message has quotation marks that shouldn’t be there, and the " | int" is in the wrong spot.

Your code did the trick, thanks a lot !!

@MRV thank you for your tips, it is indeed very easy to make mistakes …

Hello, i’m trying to do this via automation, i would like to create a rule taking advantage of an rgb led i have working.

If temperature is below 10c then light blue
If temperature is between 11c and 22c then light white
if temperature is above 23c, then light red.

Help

Create template sensor, that gives color names blue, white, red according the temperature change?
When this template sensors state changes, set new color with name?
Then the light is changed only when the color really changes.

Sounds easy, but i have no idea how to do that, do you have an example or something?. Thanks

Have a look at the template sensor doc:

Thanks I saw that but there is no instruction on how to establish a color in a led light and the. Establish values , like if temperature is between 20-30 then green , 31 above , red , 19 and below , blue

no you need to input your own data in it.
So value_template would look something like:

value_template: >
  {% if states('sensor.temperature') | int <= 10 %} "blue"
  {% elif states('sensor.temperature') | int >= 23 %} "red"
  {% else %} "white"
  {% endif %}

Of course replace the sensor with your own.

3 Likes

It’s easier also to debug, if values are in separate sensor.

Automation for changing light is then like this:

  - alias: Light
    trigger:
      - platform: state
        entity_id: sensor.Light_Name
    action:
      - service: light.turn_on
        data: 
          entity_id: light.your_light
        data_template:
          color_name: >
            {{ states('sensor.light_name')  }}
3 Likes

Hello, I want to change the colour of my lights based on an input number. Anyone can tell me how the automation would look like ? Or Node Red Flow ?

With automation:

   - alias: kitchen_lights_change
     trigger:
       - platform: state
         entity_id: input_number.light_brightness
       - platform: state
         entity_id: input_number.light_temp
     action:
       - service: light.turn_on
         data: 
           entity_id: light.kitchen_light
         data_template:
           brightness: >
               {{ states('input_number.light_brightness')|int  }}
           kelvin: >
             {{ states('input_number.light_temp')|int  }}
       - service: light.turn_on
         data: 
           entity_id: light.kitchen_light
         data_template:
           brightness: >
               {{ states('input_number.light_brightness')|int  }}
           kelvin: >
             {{ states('input_number.light_temp')|int  }}

Light turn on commend is twice, because if light was off, it does not have property light_temp. It has to be switched on first.

Is there a way to convert this into lovelace yaml to be used like this?

label: |
  [[[
    var bri = Math.round(states['light.zachs_lamp'].attributes.brightness/2.55);
    var col = states['light.zachs_lamp'].attributes.rgb_color;
    return `<ha-icon icon="mdi:circle" style="width: 20px; height: 20px; color: col ;"></ha-icon>
    <span>‎‏‏‎ ‎‏‏‎ ‎<span style="color: white">${(bri ? bri + '%' : 'Off')}</span></span>`
  ]]]

I want a dot that represents the color of the light using a variable to pull the rgb_color attribute but nothing I try is working.

Thanks!