5 m

Stuffed dogs upon a closet underneath a blue colored ledstripSometimes, it’s nice to do something special with your color capable lights. It’s nice to create your own scenes with your own colors, but I wanted to go a step further, by letting my Hue light strip representing the outside temperature. Red is bloody hot, orange is relatively hot, yellow is nice and warm, green is nice and cold, blue is relatively cold and white is freezing cold! Wanna do the same? Here is how I did it.

One thing before you start building anything: The values used in this article are in degrees celsius. Since I made my own calculations to get a color from a temperature reading, the values for degrees Fahrenheit are different in many ways. So, don’t try to use the celsius calculations within a Fahrenheit environment, but convert them first or write your own calculations from scratch.

You need two things for this automation idea to work:

  • A light with color capabilities
  • A temperature reading from outside your home

Products I use for this automation


Temperature reading

For the outside temperature, you can use your own temperature sensor, a weather integration that supports your local region or the default weather integration within Home Assistant.

For the default integration, you can create a template temperature sensor for your own convenience. Your sensor will look like the following in yaml:

# Example template temperature sensor for default Home Assistant weather
sensor:
  - platform: template
    sensors:
      outside_temperature:
        friendly_name: "Outside temperature"
        unit_of_measurement: '°C'
        device_class: temperature
        value_template: "{{ state_attr('weather.home', 'temperature') | round(1) }}"

In the other two situations, you will already have a dedicated outside temperature sensor within Home Assistant, so this template sensor isn’t needed. However, for an exact reading, I advice you to use a hardware, weatherproof temperature sensor. I use a Hue Outdoor Motion Sensor on my balcony, which has a temperature and light sensor on board. I tried many outdoor sensors, but this one proves to be the most reliable.

Conversion from degrees celsius to color

Now, it’s time to do some math. If you don’t like the math part of it, scroll to the end of this section and copy and paste the two hue_temp sensors into your configuration file. Otherwise…

The challenge was to convert degrees celsius to a color with one mathematical formula, so I was able to run one script to match the color of the light with the outside temperature. After doing some research, a friend of mine advised me to use the hue/sat value to give the light the right color. Using hue/sat, I only needed two formulas to calculate the hue and the saturation values.

Hue value

First, the hue value formula. Normally, the hue values range is 0 to 360 or from red to red with all the colors of the rainbow in between. For this idea, I only need the colors red, orange, yellow, green and blue. So, I limited the hue value from 0 to 240.

Next, I had to make a decision about the temperature window. In my region, it is strange to have a temperature above 45 or below -5 degrees celsius and even 45 and -5 degrees aren’t within the regular temperature range. So I choose this as my temperature range to be on the safe side.

Now, let’s calculate. I wanted to have a full color every 10 degrees, starting from 45 with a red color and down to 5 degrees with a blue color. The range from 45 to 5 is 40, so I divided 240 by 40, which is 6 (the conversion value). To get from the current outside temperature to a value within the temperature range, I simply subtrack the temperature by 5. The last thing is to reverse the hue value since I wanted the high temperatures to be red and the cold temperatures to be blue. I did this to subtrack the outcome of the formula from 240 (the limit I set for the hue value). The formula looks as follows:

hue_value = max_hue_value - ((emperature_value - lower_limit_temperature_range) * conversion_value)

My formula looks as follows:

hue_value = 240 - ((sensor.balcony_temperature - 5) * 6)

Saturation

For the saturation value, I did a similar thing. Saturation is in percentage, so the formula is a little easier. The saturation needs to go down from 15 to -5 degrees celsius, so the range is 20. 100 divided by 20 is 5 (conversion value). The formula for the saturation value is as follows:

sat_value = ((temperature_value - lower_limit_temperature_value) * conversion_value)

My formula looks as follows:

sat_value = ((sensor.balcony_temperature +5) * 5)

Hue/Sat sensors

Lastly, I created two sensors, one for each value, with if-statements to prevent the hue and saturation value from going outside the ranges I set for these values. So the hue value may not be higher than 240 or lower than 0 and the saturation value may not be higher than 100 or lower than 0. The sensors are below. Feel free to copy and paste them into your configuration file, but don’t forget to change the entity ID for the temperature sensor.

# Example template sensors to get a hue/sat value from a temperature reading
sensor:
  - platform: template
    sensors:
      hue_temp_hue:
        value_template: >
          {% if states('sensor.balcony_temperature')| float > 45.00 %}0{% elif states('sensor.balcony_temperature')| float < 45.01 and states('sensor.balcony_temperature')| float > 4.99 %}{{ (240 - ((float(states.sensor.balcony_temperature.state) - 5) * 6)) | round(0) }}{% elif states('sensor.balcony_temperature')| float < 5.00 %}240{% endif %}
      hue_temp_sat:
        value_template: >
          {% if states('sensor.balcony_temperature')| float > 14.99 %}100{% elif states('sensor.balcony_temperature')| float < 15.00 and states('sensor.balcony_temperature')| float > -5.00 %}{{ ((float(states.sensor.balcony_temperature.state) + 5) * 5) | round(0) }}{% elif states('sensor.balcony_temperature')| float < -4.99 %}0{% endif %}

Coloring your lights

Now it is time to give the light the actual color. I did this with a script, triggered by an automation. This way, you can still use your own scenes and change the brightness, but three seconds after the light turns on, it will fade to the temperature color. This is the reason I left out any brightness options within the script below. The data is set as template, so it was possible to use the sensors 'sensor.hue_temp_hue' and 'sensor.hue_temp_sat' as values to set the color.

# Example script for coloring your lights
light_temperature_color:
  alias: Light - Temperature color
  sequence:
  - service: light.turn_on
    data_template:
      entity_id: light.rgb_strip
      transition: 3
      hs_color:
      - '{{ states(''sensor.hue_temp_hue'') }}'
      - '{{ states(''sensor.hue_temp_sat'') }}'
  mode: parallel
  max: 3

Lastly, I automated the script so I don't have to start it myself and it gets updated every ten minutes. I also added the light as a condition, so it won't turn on every ten minutes. 😉My example automation is below:

- alias: Light - Refresh outdoor color
  description: ''
  trigger:
  - platform: state
    entity_id: light.rgb_strip
    from: 'off'
    to: 'on'
    for: 00:00:02
  - platform: time_pattern
    minutes: '10'
    seconds: '00'
    hours: '00'
  condition:
  - condition: state
    entity_id: light.rgb_strip
    state: 'on'
  action:
  - service: script.light_temperature_color
    data: {}
  mode: single

And there you go! An RGB light that represents the outdoor temperature with a color. My light is between blue and white for the last few days and that doesn't say anything about my automation idea! 😉 Anyway, have fun with this automation ideas. I hope to hear from you if you have any improvements or you have your own ideas added to this idea!

Enjoy the holidays!