Change Sonoff L1-Lite RGB colours based on temperature in Home Assistant
A scenario I had in my mind was to change the colour of the RGB light strip in my network cabinet based on the ambient temperature of my study. For example: if the temperature exceeds 21 degrees celcius, it should change to red. If it drops below 21 it should change to green, and below 17 it should shine blue.
Before I continue, here are the products I have used for this project:
- ESP-32S Development Board WiFi+bluetooth
- DHT11 Temperature and Humidity Sensor
- 5m Sonoff L1 Smart LED Light Strip
I have wired up the ESP-32 with the DHT11 Temp sensor, installed the LED strip in my network cabinet and linked the Sonoff strip with my Ewelink account. If you aren’t installing any other firmware and only using the original firmware for your Sonoff products (like I am doing currently), you can follow these instructions provided by AlexxIT to integrate your Sonoff products in Home Assistant without flashing firmware. I found this as the better integration for Sonoff products, as the official Ewelink integration gave me some headaches at first. The ESP-32 with DHT sensor’s sketch was created using the ESPhome integration in Home Assistant (install it if you haven’t done so), compiled the new firmware and uploaded it via USB to the ESP-32 using ESPhome-Flasher (there are numerous tutorials on how ESPhome works as well as flashing the ESP-32 with the new firmware). Here is an example of the ESPhome YAML file I use:
esphome: name: study-temp-sens platform: ESP32 board: esp32dev # Enable logging logger: # Enable Home Assistant API api: ota: password: "some password" wifi: ssid: "Your WiFi SSID" password: "Your WiFi Password" # Enable fallback hotspot (captive portal) in case wifi connection fails ap: ssid: "Generated by ESPhome" password: "Some Random Password generated" captive_portal: sensor: - platform: dht pin: GPIO4 temperature: name: "Study Temperature" humidity: name: "Study Humidity" update_interval: 60s model: DHT11
What I have found with my Home Assistant instance, is that when creating the automation to turn on the LED strip and set the attributes (colours, brightness, etc.), the automation would not execute (haven’t really found out why). I have created a workaround which works perfectly fine for me to change the colours based on temperature values. I will provide the YAML code and not the Home Assistant GUI screenshots for these automations.
Firstly, there is an automation which turns my network cabinet lights on every day at 08h00. After it has turned on (should the colour changed prior being turned off) there is another service running to change the light colour to Green (still roughly done as I will be working on automations to check the temp first and determine the colour it should use to turn on with):
alias: Turn ON Cabinet LED description: Turn on the network cabinet LED's in the morning. trigger: - platform: time at: '08:00:00' condition: [] action: - service: light.turn_on target: entity_id: light.sonoff_cabinetled data: {} - service: light.turn_on target: entity_id: light.sonoff_cabinetled data: transition: 0 color_name: green brightness_pct: 100 - service: notify.mobile_app_********** data: message: The network cabinet lights have turned on. Have a productive day. title: Network Cabinet Lights ON mode: single
So now the LED lights have turned on according to schedule. Now to change it to red when the temp exceeds 21 degrees celcius, create the following automation:
alias: 'TEMP: Change Cabinet LED - RED' description: Change network cabinet LED's when temp is high trigger: - platform: numeric_state entity_id: sensor.study_temperature attribute: unit_of_measurement above: '21' condition: [] action: - service: light.turn_on target: entity_id: light.sonoff_cabinetled data: transition: 1 color_name: red brightness_pct: 100 - service: notify.mobile_app_********** data: message: The network cabinet lights have turned red. title: Network Cabinet Lights RED mode: single
Now you can create more automations to change the colour when the temperature drops below a certain level. Here’s two automations:
alias: 'TEMP: Change Cabinet LED - Green' description: Change network cabinet LED's when temp is normal trigger: - platform: numeric_state entity_id: sensor.study_temperature below: '20' above: '16' condition: [] action: - service: light.turn_on target: entity_id: light.sonoff_cabinetled data: transition: 1 color_name: green brightness_pct: 90 - service: notify.mobile_app_********** data: message: The network cabinet lights have turned Green. title: Network Cabinet Lights Green mode: single
alias: 'TEMP: Change Cabinet LED - Blue' description: Change network cabinet LED's when temp is cold trigger: - platform: numeric_state entity_id: sensor.study_temperature below: '17' condition: [] action: - service: light.turn_on target: entity_id: light.sonoff_cabinetled data: transition: 1 color_name: blue brightness_pct: 90 - service: notify.mobile_app_********** data: message: The network cabinet lights have turned Blue. title: Network Cabinet Lights Blue mode: single
I will be updating this post, as this is just a quick way change the LED strip light if you have been struggling to get your automation to run. This is surely not perfect, but is a great place to start for beginners (and people still having a lot to learn about Home Assistant and YAML). If you have any other recommendation, please feel free to post it in the comments.
Leave a Reply