Making my IR based RGB strip smart - EspHome on Home Assistant.
I had a basic RGB strip that came with an IR blaster. Good one, working flawlessly for over a year now. I can control it via the remote and occasionally I would even forget it can spit out colors other than violet.
A week after I started playing with Home Assistant, I decided I want to hook it up to this little brain. Imagine getting up late at night in a pitch dark room, and you start hitting your toenails; Or you just start walking, and "the house" turns on a guiding light for you to follow. With smart bulbs and strips it is straight forward to just link into HA with community maintained integrations.
But what I had was a pair of dirt-cheap analog strips, already attached to my bedroom windowpane since I moved into the new house. So I started to look for ways to use the existing setup in my room with minimal changes.
Hunting ways to control the strip.
The first step was to get to know about the different methods I can use to control this RGB strip. After a few google searches and DIY videos, I was left with these three options -
1. Generic H801 RGBW controller
The H801 controller is one of the easily available LED controllers with separate RGB channels. Another benefit of using this one will be ease of integration into ESPHome. They even have a cookbook tutorial for it.
- H801 RGBW LED controller
https://esphome.io/cookbook/h801.html
2. PWM - dedicate an esp module.
PWM is an acronym for Pulse Width Modulation. If you know how analog strip works - the three channels can be supplied with different voltage and thereby controlling the intensity of Red, Green, and Blue which in turn produces different colors. There are existing tutorials on how to wire things up in node MCU. One can combine this up with RGBW light component from Esphome to easily have a fully integrated working solution. This would also allow you to utilize a whole bunch of effects without doing any extra work.
- https://www.instructables.com/id/IOT-ESP-8266-Nodemcu-Controlling-RGB-LED-Strip-Ove
- https://esphome.io/components/light/rgbw.html
3. Mimic the IR remote via ESPHome.
Like I said, I had everything already set up and working via the remote. What if I can mimic to hit the red button when I set it red from Home assistant? Theoretically, that should just work!
Folks have already made few tutorials on how to set up an IR led acting as a remote with node MCU.
- https://www.instructables.com/id/ESP8266-RGB-LED-STRIP-WIFI-Control-NODEMCU-As-a-IR/
- https://www.instructables.com/id/WiFi-LED-Strip-Controller-Based-on-ESP-8266/
I just needed to figure out how to get this working in Home Assistant.
Putting things together.
Hardware hints from the first tutorial above - One IR LED with a simple resistor? Yes, I can do that :)
The next step was to use this IR blaster to send some test signals. ESPHome has a very handy component for this very specific task - Remote Transmitter. But before we dive in there, it would be good to know how the basic protocols work. The Wikipedia page on Consumer IR Protocols do not have much info, but I found a little article briefing just about the same.
https://techdocs.altium.com/display/FPGA/NEC+Infrared+Transmission+Protocol
This is just NEC. If the above link is broken, please search for basic IR NEC codes. Most RGB IR transmitters available in the market follow similar NEC protocols. There are other protocols and few vendor-specific ones too, but we do not need to worry about that for this project.
Good that the folks over ESPHome have already made this simple. Thanks to the articles above, I already knew the command I needed to send; and from the docs I knew - how to send. Combining both, I had a working example to switch on and switch off the strip.
esphome:
name: node_one
platform: ESP8266
board: nodemcuv2
wifi:
ssid: "***"
password: "********"
# Enable logging
logger:
captive_portal:
remote_transmitter:
pin: GPIO2
carrier_duty_percent: 50%
switch:
- platform: template
name: strip_on
turn_on_action:
- remote_transmitter.transmit_nec:
address: 0xF7
command: 0xC03F
- platform: template
name: strip_off
turn_on_action:
- remote_transmitter.transmit_nec:
address: 0xF7
command: 0x40BF
Once this is sorted, I moved on to the custom light component to see if I can seamlessly create a light entity. The idea was to create a component that would act as a light interface to Home Assistant. Any commands like ON, OFF, Switching colors etc would be translated and mapped to the proper buttons on the remote.
Mapping colors to a button.
There are tons of available color palettes in the HA dashboard, while my remote and driver can recognize only 12 out of those! With some help, I deduced a way to map a given color value to the nearest shade available on the remote.
Mapping brightness.
The only buttons available were to increase and decrease brightness. So even if Home Assistant was providing us with brightness info, it was of no use on its own. Instead, if there is a change in brightness, we can use this delta to determine if we need to send Brightness UP or DOWN command.
Proceeding on with this assumption, I created a custom component to set desirable traits and make a handle available to Home Assistant. On receiving commands, it translated them to their remote counterparts and the driver behaves just like if someone had pressed the remote.
RGBStripLight
homeassistant-config/esphome/rgbstriplight.h
Copy and paste the file in Esphome config root. Once there, update your test node to import and create light from this custom component.
remote_transmitter:
id: esp_transmitter
pin:
number: D8
carrier_duty_percent: 50%
light:
- platform: custom
lambda: |-
auto led_strip = new RGBStripLight(id(esp_transmitter));
App.register_component(led_strip);
return {led_strip};
lights:
- name: "Window Strip Light"
default_transition_length: 0s
Upload and head over to your Home Assistant entities dashboard to configure this newly added light. Try some actions and see if it responds.
I also have an integration with Google Assistant. Hence I can easily switch the strips by reaching out to my assistant on the phone or smart speakers.
Cons. Reverse feedback?
I can still use the remote to control the same strip independent of Home Assistant. If I use it to say change the color or turn it off, my HA dashboard would not reflect the state change. This is a current limitation of this approach, but I am okay to live with it 🐷.
If this is something that bothers you, you may choose to go with the PWM solution I posted above. On top of that, if you would like to retain your IR remote support, ESPHome has a Remote receiver component as well.
Wrap up
It's been only a few months now, and I have already written about my onboarding experience with Home Assistant. Being a hobbyist myself, I like to customize a lot of things to cater to my needs. And I can definitely back up the level of flexibility Home Assistant and integrations like EspHome is providing here. Looking forward to more of such tweaks in the future. Follow me on @twitter to get latest updates.
Thanks for reading. Have a nice day 🤘
References
- https://esphome.io/cookbook/h801.html
- https://www.instructables.com/id/IOT-ESP-8266-Nodemcu-Controlling-RGB-LED-Strip-Ove
- https://esphome.io/components/light/rgbw.html
- https://www.instructables.com/id/ESP8266-RGB-LED-STRIP-WIFI-Control-NODEMCU-As-a-IR/
- https://www.instructables.com/id/WiFi-LED-Strip-Controller-Based-on-ESP-8266/
- https://esphome.io/components/remote_transmitter.html
- https://esphome.io/components/light/custom.html