Temperature to RGB light?

In the process of learning Home Assistant.
I would like to set a color of an RGB light based on a temperature sensor.
For now, just trying to control the amount of red.

In configuration.yaml I’ve added:

sensor:
  - platform: template
    sensors:
      rgb_color:
        friendly_name: "rgb_color"
        unit_of_measurement: "RGB"
        value_template: "[{{ states('sensor.lumi_lumi_weather_temperature') | float * 2}}, 20, 20]"

Then, in an automation I have:

alias: Test Set Color
description: ''
trigger:
  - platform: state
    entity_id: sensor.lumi_lumi_weather_temperature
condition: []
action:
  - service: light.turn_on
    target:
      device_id: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    data:
      rgb_color: sensor.rgb_color
mode: single

In automation debug, I get an error
“None for dictionary value @ data[‘rgb_color’]”

How can I properly format the RGB output so that it is a [R,G,B] array and not a string?

You would need to template the rgb_color to the entity, but I have a feeling it will fail.

    data:
      rgb_color: "{{ states('sensor.rgb_color') }}"

You could try putting the template from your sensor into the automation. The following topic has a couple examples.

Using the HSB (hue/saturation/brightness) colour space makes it easy to map a temperature range to a hue, keeping the saturation and brightness constant. e.g. starting at a hue of 180° on the wheel and heading up to 360° with an equation if you don’t mind cyan = cold, red = hot and blue/purple representing a middle temperature.

Or going the other way round the hue wheel, blue, cyan, green, yellow, orange, red (cold → hot), which I find better.

e.g. this maps 16°C → 24°C to blue, cyan, green, yellow, orange, red and clamps temperatures below and above that range to blue or red respectively. Purple = faulty sensor.

  action:
    service: light.turn_on
    target:
      entity_id: light.your_light
    data:
      brightness: 255
      hs_color: >
        {% set temp = states('sensor.your_temp_sensor')|float(default=1001) %}
        {% if temp < 1000 %}
          {% set h = -30 * temp + 720 %}
          {% set hlim = ([0,h,240]|sort)[1]|int %}
          [ {{ hlim }}, 100 ]
        {% else %}
          [ 300, 100 ]
        {% endif %}

3 Likes

Thank you for the suggestion. It worked very well.
I’ve tweaked the code a bit, so it is easier to adjust the temperature limits and the hue limits:

action:
  - service: light.turn_on
    target:
      device_id: b6201bbcb55a701236b11c9515823c99
    data:
      brightness: 255
      hs_color: >
        {% set t_min = 60 %} 

        {% set t_max = 90 %} 

        {% set h_min = 240 %} 

        {% set h_max = 0 %} {% set t_input = states('sensor.lumi_lumi_weather_temperature')|float %} 

        {% set h_out = (h_max - h_min) / (t_max - t_min) * (t_input - t_min) + h_min %} 

        [ {{ ([0,h_out,240]|sort)[1]|int }}, 100 ] #limit output to 0 <= h_out <= 240
1 Like

You should provide a default for your |float filter. Mine defaults to over 1000°C so I can detect that and display a “fault” colour (purple, H: 300).