My previous post demonstrated how to make IR-controlled devices smart using a microcontroller and the ESPHome project. Besides the basic setup described there, there is no limit to what you can do with the system once you can control your devices with software. Here are three cool tricks I’m using in my smart home setup that might be useful for others as well.
The problem with infrared communication is that it is one-way only. Unlike two-way communication protocols (like Zigbee or Z-Wave), there is no way to know the current state of the controlled device. When you reach for the remote to turn on the TV, you see with your own eyes that it is off, so you press the power button on the remote. But you can’t create an automation to turn it on by pressing the power button. It would work in some cases, but what happens if the device is already on? Pressing the power button without knowing the current state would turn off the TV.
While it is not a perfect solution, we can work around this by keeping track of the device state in our smart home system. We assume the real device state has indeed changed by our actions. Let’s create a binary sensor in ESPHome to track the device power and update its state every time we fire the power button action:
This is a templated sensor that we will manually update later. In order to do that, we have to define a unique ID. This sensor shows up in Home Assistant like this:
Now that we have a sensor, let’s create the automation that updates its state every time we trigger the IR command over MQTT. Thanks to ESPHome’s powerful automation rules, everything happens on the device without extra delays and errors caused by any network communication:
Based on this setup, we can finally create an automation to turn on the device:
- Trigger: some event
- Condition: if device state is OFF
- Action: press power
This trick is not perfect, though, because there are two scenarios when our fake state can get out of sync with reality:
- Someone uses the physical remote to control the device: the real state changes, but our fake state is not updated
- A transmission error occurs: ESPHome sends the signal and updates the fake state, but the signal doesn’t reach the receiver. This occasionally happens when someone blocks the signal by standing in front of the remote.
In the previous post, I briefly mentioned that I prefer an abstraction on IR remote actions:
As you can see in the config above, my MQTT topics define logical commands (
volume_up) instead of protocol-specific data (
address 0, command 16). I think it’s a good practice to have this abstraction on the device itself and expose only logical commands to the outside world.
For debugging purposes, however, it’s useful to have an interface for sending protocol-specific codes. The following automation rule grabs a JSON object from an MQTT topic, extracts the
command fields, then calls the transmit action with those values:
My TV has an energy-saving feature to turn off the display (while keeping everything else running). This is ideal for music listening because the Spotify player on the TV can be controlled with any other device.
The only problem: it takes four button presses on the remote to activate this power-saving mode. So I created a sequence of actions that sends
OK to activate this mode from the menu (with a delay between each action):