BroadLink RM4 Pro in Home Assistant Without the App



I've recently fallen into the rabbit hole of using home-automation. The obvious choice is Home Assistant and I've enjoyed the journey a lot so far. This blag post documents my process of automating all those crappy remote controlled gadget that accumulated in the house: that cheap light bulb, those cheap fairy lights, or that cheap LED strip … you might know what I mean. The remotes come in all forms and sizes and are often quite thin, powered by a button cell, and work occasionally. I have a literal box full of them. We'll be using off-the shelve hardware while still avoiding to install any crappy mobile apps. This will make the first part a lot harder. The second part will cover Home Assistant and training the hardware to emulate the signals currently sent by all those remotes. There's also some sustainability angle in there since you'll never need to replace those batteries anymore ;-). # Preamble The first step is to figure out if your remote even is an infrared (IR) remote. The other common option is that the remote is using radio (RF) around the 433mhz frequency spectrum. But we'll ignore those for now because it seems to be an entire rabbit hole of it's own touching topics like "software defined radio" (SDR) and Manchester Encoding. Anyway, to figure out if you remote is using IR or not, point it at your smartphone camera and start pressing buttons. The camera should see the IR LED flash. Speaking of IR LED: If you can't see an IR LED that's a good sign that your remote is not using IR since infrared radiation needs line-of-sight to work. In order to emulate your remote control, you'll need some sort of hardware to send the signal. I decided against building my own ESP-based IR blaster — again trying to avoid another rabbit hole — and instead just buy one that is reasonably compatible with Home Assistant: and the BroadLink RM4 Pro it is. # IR Blaster The first step is to connect the RM4 to your WiFi. For this, you need to connect it to some sort of power source (doesn't need to by your PC or the Home Assistant hardware) and start it up in what seems do be some sort of "provisioning mode". This should happen on power up if you got a brand new one. Otherwise, just stick a paperclip into some hole or hold a button for 10 seconds or something like this to trigger a factory reset. If the RM4 is in "provisioning mode", it'll create a wifi access point. Connect to it and figure out the IP address of the RM4 within that network. For me, that was as simple as executing ipconfig:
Wireless LAN adapter Wi-Fi:

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::2c84:e92d:549b:db7d%16
   IPv4 Address. . . . . . . . . . . : 192.168.10.100
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 192.168.10.1
In my case the RM4 actually was the default gateway 192.168.10.1 ymmv. The next step is to connect that RM4 to your wifi. For this, just install the broadlink Python package with
pip3 install broadlink
and execute the following Python script (after adjusting your wifi credentials):
import broadlink

broadlink.setup(
    ssid='YourWifiSsid',
    password='YourWifiPassword',
    security_mode=3,
    ip_address='192.168.10.1',
)
Now the RM4 should take a second and connect to your wifi instead of broadcasting the access point for provisioning. Finally, figure out how the IP address of the RM4 within your wifi is. For me that was as simple as waiting for the "there's a new device on the network"-notification from my plastic router. Otherwise, nmap might help. Again: all this is probably not necessary if you use the BroadLink app like a muggle. # Add Remote to Home Assistant As so often with Home Assistant, the first step is to add the Broadlink integration. The installation wizard will ask you for the IP address of your RM4. For me it suggested the name "智能遥控" which I ignorantly replaced with something else. Google translate says it means "smart remote control", so guess it would have been a good choice as well. Consider adding the room, you want to place the RM4 in, to the name. You might need more of them late, line-of-sight and all that. Let's now move on to teaching the remote, or rather Home Assistant, the IR signals to send. Because the RM4 doesn't only have IR LEDs to send IR signals but also at least one IR receiver to support this learning phase. I created the following "Script" in Home Assistant:
learn_rm4pro:
  sequence:
  - action: remote.learn_command
    data:
      device: your_device
      command: your_command
      command_type: ir
    target:
      device_id: 4e65766572476f6e6547697665596f75
What confused me a lot at first was what the device_id, device, and command fields are supposed to contain. The answer is that device_id is the ID of the RM4, while the other two fields are free text and you can put in there whatever you want. The device_id, you can get by searching your RM4 Pro in Home Assistant and noting it in the URL: /config/devices/device/4e65766572476f6e6547697665596f75. After completing the learning command, Home Assistant will have automatically created the device you put in there that supports the specified command. Before you can run the above script, you need to reload the Home Assistant configuration. In the old days that meant you needed to restart Home Assistant, nowadays there's also the option to simple reload the JSON only. When you run the script, Home Assistant will create a notification prompting you to press the button on the remote. Now point the remote at the RM — line-of-sight and all that — and press the button. The notification will go away. You could now repeat the process for every button on the remote, but I recommend holding back on that for a minute because there is a simple way to make the learning much more comfortable. If you want to be extra-sure that this step worked, you can check out the .storage directory in the Home Assistant file system and look for a file name broadlink_remote_baadf00d50c5_codes or something like that. The Home Assistant documentation is very good if you want to learn how to get access to the file system. # Sent Remote Commands from Home Assistant Finally, we need to create one script for each command we want to send to the device. This basically emulates the button press on the remote. Create the following script in Home Assistant to do just that:
rm4pro_your_device_your_command:
  sequence:
  - action: remote.send_command
    data:
      device: your_device
      command: your_command
    target:
      device_id: 4e65766572476f6e6547697665596f75
You can now bind this script to a button on a dashboard or use it in automations. # Batch Learning As promised, here is a way to simplify the above process of "Create Script", "Reload YAML", "Execute Script", "Press Button", "Create Script" (again), and this starts the tedious loop from the start. And it is as simple as passing a list to the command key (it seems that the remote.learn_command action supports that):
learn_rm4pro:
  sequence:
  - action: remote.learn_command
    data:
      device: your_device
      command:
        - first_button
        - second_button 
        - last_button
      command_type: ir
    target:
      device_id: 4e65766572476f6e6547697665596f75
When you execute that script, Home Assistant will iterate through all entries of the command list and just show you command names as notification one after the other. # Final Thoughts and Ideas Many remotes only have one button to, say, power the device on as well as off. My understanding is that Real Engineers™ call that an "open loop system": Home Assistant does not know the state of the device before powering it on or off. The feedback _loop_ is not closed (i.e. open). One could try to track that state within Home Assistant of course. But a problem with that approach is that the actual state could changed or not change due to external factors: * power loss * usage of actual remote * RM4Pro signal did not arrive A general solution here is to "close the loop" by, for example, attaching a smart plug to measure power draw to estimate if the device is actually on or off. One word of warning for any travelers of the tubes in the future: based on the fact that almost all tutorials I found for Home Assistant were slightly outdated, this here probably is as well by the time you find it.

Leave a Reply

Your email address will not be published. Required fields are marked *