Mastering Infrared Control: Integrating ESPHome with Home Assistant
- #Home_Assistant
- #ESPHome
- #Infrared
- #IR_Control
- #Smart_Home
- #DIY
In the quest for a fully integrated smart home, we often encounter devices that stubbornly cling to older technologies – primarily, infrared (IR) control. While newer devices boast Wi-Fi, Bluetooth, or Zigbee, your trusty TV, air conditioner, fan, or audio receiver might still rely solely on a handheld IR remote. Abandoning these perfectly functional devices isn't the smart home way. Fortunately, with the power of Home Assistant and ESPHome, you can bring these infrared-controlled gadgets into your modern smart ecosystem.
Why ESPHome for IR Control?
While other methods exist (like commercial IR blasters), using ESPHome offers significant advantages:
- Local Control: Commands stay within your local network, offering speed and reliability independent of internet connectivity.
- Flexibility: Highly customizable hardware and software allow you to build exactly what you need.
- Cost-Effective: ESP boards and IR components are inexpensive.
- Deep Integration: ESPHome devices appear natively in Home Assistant, making automation and dashboard control straightforward.
- Open Source: Part of the Home Assistant ecosystem with active development and community support.
Prerequisites
- An existing Home Assistant installation.
- The ESPHome add-on installed and configured in Home Assistant (or a separate ESPHome installation).
- Basic understanding of electronics and soldering (optional but helpful for building the circuit).
- A suitable ESP board (e.g., ESP8266 like NodeMCU, or ESP32).
- An IR receiver module (e.g., VS1838B, TSOP series).
- An IR transmitter setup (e.g., a standard IR LED, a transistor like 2N2222 or BC547, and appropriate resistors).
Hardware Setup
You'll need to connect the IR receiver and transmitter to your ESP board. A common setup involves:
IR Receiver (e.g., VS1838B):
- VCC to 3.3V or 5V on ESP (check receiver datasheet).
- GND to GND on ESP.
- OUT/DATA to a digital GPIO pin on ESP (choose one not used for SPI Flash, e.g., D1/D2/D5/D6/D7 on NodeMCU, or various pins on ESP32).
IR Transmitter (Simple Circuit):
- Connect the Anode (longer leg) of the IR LED to VCC (3.3V or 5V) via a current-limiting resistor (e.g., 100-220 ohms).
- Connect the Cathode (shorter leg) of the IR LED to the collector of an NPN transistor (e.g., 2N2222).
- Connect the emitter of the transistor to GND on ESP.
- Connect the base of the transistor to a digital GPIO pin on ESP (different from the receiver pin) via a base resistor (e.g., 1k-10k ohms).
Alternatively, you can use pre-built IR transmitter modules that simplify wiring. Ensure your circuit provides enough current to the IR LED for reliable transmission – a simple series resistor might not be sufficient for high-power LEDs.
ESPHome Configuration (YAML)
Access your ESPHome dashboard in Home Assistant and create a new node or edit an existing one. You'll need to add configurations for both receiving and transmitting IR signals.
1. Add Receiver and Transmitter Components
!$0$!
Explanation:
!$1$!
: Configures the input pin for detecting IR signals. The!$2$!
and!$3$!
sections define actions when a signal is received. Logging!$4$!
is essential for learning new codes.!$5$!
: Configures the output pin to send IR signals.- Replace
!$6$!
and!$7$!
with the actual GPIO numbers your components are connected to.
2. Capture IR Codes
Before you can send commands, you need to 'teach' your ESP board the codes your remotes use. This is done by receiving the signals.
- Upload the configuration to your ESP board.
- Open the 'Logs' for the ESPHome device in Home Assistant.
- Point your original remote at the IR receiver on your ESP board (get relatively close, a few inches usually works).
- Press the button on the remote you want to capture (e.g., Volume Up, Power On, Input Select).
- Look at the ESPHome logs. You should see lines like:
!$8$!
where!$9$!
is a long string of comma-separated numbers. This is your RAW code. - Copy the raw code string. Repeat for every button you need to control (Power On, Power Off, Volume Up, Volume Down, Channel Up, Channel Down, Input 1, Input 2, etc.).
3. Add Buttons or Entities to Send Codes
Now that you have the RAW codes, you can define entities in your ESPHome configuration that will send these codes when triggered from Home Assistant.
The simplest way is using the !$10$!
component:
!$11$!
Explanation:
!$12$!
: Defines a button entity that will appear in Home Assistant.!$13$!
: Allows defining custom actions on press.!$14$!
: A unique ID for internal use in ESPHome.!$15$!
: The action to perform when the button is pressed in Home Assistant.!$16$!
: The service call to send a raw IR signal.!$17$!
: Paste the raw code you captured earlier as a list of numbers.!$18$!
: Specify the carrier frequency, typically 38kHz for most devices. You can sometimes find this in the raw code data or device specifications, or experiment.
Upload the updated configuration to your ESP board. Once online, Home Assistant should automatically discover the new button entities.
Integrating into Home Assistant
The button entities (and any other components you define in ESPHome) will appear as devices in Home Assistant under the ESPHome integration. You can now:
Add to Dashboards (Lovelace)
Use a 'Button' card in your Lovelace dashboard and select the ESPHome IR button entities you created. Now you have on-screen controls for your IR devices.
Create Automations
This is where the real power lies. You can trigger sending IR commands based on any Home Assistant automation trigger:
- Turn on the TV and set input when the 'Watch Movie' scene is activated.
- Turn off the AC if a window sensor reports open for more than 5 minutes.
- Lower the stereo volume when a phone call is detected (if you have a phone integration).
- Turn on a fan when a temperature sensor reaches a certain threshold.
Use the 'Call Service' action in your automation. The service will be `button.press` and the entity will be your ESPHome IR button (e.g., !$19$!
).
If you captured codes using !$20$!
or !$21$!
with actions other than just logging (e.g., setting a binary sensor), you could also trigger automations in Home Assistant from the IR remote itself.
Tips and Best Practices
- Reliable Code Capture: Capture codes multiple times for the same button to ensure consistency. Sometimes small variations occur; find the most common or reliable pattern. Position the remote and receiver correctly.
- Store Codes Securely/Organized: For long, complex raw codes, consider storing them in ESPHome secrets or using
!$22$!
directives to keep your main YAML clean. - Line of Sight: IR requires a clear line of sight between the transmitter LED and the device's IR receiver. Position your ESP board strategically. You might need multiple IR LEDs or a more powerful transistor circuit for wider coverage.
- Carrier Frequency: While 38kHz is common, some devices use different frequencies. If transmitting doesn't work, try capturing the code again and see if the logs provide a carrier frequency, or consult online databases for your specific device model.
- State Tracking: IR communication is typically one-way. Home Assistant won't inherently know the state of your IR device (is the TV on or off? What input is selected?). You'll need to implement state tracking in Home Assistant using helper entities (like a boolean toggle) or by inferring state from other sensors (e.g., a power monitor on the TV).
- Power Supply: Ensure your ESP board has a stable power supply, especially if driving multiple or powerful IR LEDs.
- Debugging: Always check the ESPHome logs first if commands aren't working. Verify your wiring. Double-check the raw codes you captured and pasted.
Conclusion
Integrating infrared-controlled devices into Home Assistant with ESPHome is a powerful and flexible way to extend your smart home's reach. By building a simple IR blaster, you gain the ability to control countless legacy devices locally and integrate them into complex automation routines. This DIY approach not only saves you money but also provides a deeper understanding and complete control over your smart home environment, truly mastering those infrared signals.
NGC 224
Author bio: