Configuration of Broadlink IR Device and getting the right IR codes

I bought a Broadlink RM pro+ from AliExpress. It was very easy to integrate the device into HA, but getting the right codes for my TV and receiver was very difficult.
If someone else is in same situation, I just write down the way it worked for me and maybe it helps others.

First, get the Broadlink device to know your WIFI. Use ether Intelligent Home Center or SURE Universal Remote. Both Apps are available for Apple as well. Follow the instructions in the app.
Unfortunately, the SURE Universal app did not find my Broadlink RM pro+ after I connected it to my WIFI. That’s why I couldn’t use IR codes provided by the app.
I was looking for some databases and found some useful on the internet:
https://github.com/probonopd/irdb
http://www.irdb.tk/
IR codes are documented in a chart, so I got‘protocol, device, subdevice and function’ and from here I used the Software called IrScrutinizer to get the Pronto HEX Code.
Unfortunately, no online base64-converter I tried worked for me. However, I got a python script, which did the job. Don’t know where I found anymore. Here is the code:

import binascii
import struct
import base64

def pronto2lirc(pronto):
    codes = [long(binascii.hexlify(pronto[i:i+2]), 16) for i in xrange(0, len(pronto), 2)]

    if codes[0]:
        raise ValueError('Pronto code should start with 0000')
    if len(codes) != 4 + 2 * (codes[2] + codes[3]):
        raise ValueError('Number of pulse widths does not match the preamble')

    frequency = 1 / (codes[1] * 0.241246)
    return [int(round(code / frequency)) for code in codes[4:]]

def lirc2broadlink(pulses):
    array = bytearray()

    for pulse in pulses:
        pulse = pulse * 269 / 8192  # 32.84ms units

        if pulse < 256:
            array += bytearray(struct.pack('>B', pulse))  # big endian (1-byte)
        else:
            array += bytearray([0x00])  # indicate next number is 2-bytes
            array += bytearray(struct.pack('>H', pulse))  # big endian (2-bytes)

    packet = bytearray([0x26, 0x00])  # 0x26 = IR, 0x00 = no repeats
    packet += bytearray(struct.pack('<H', len(array)))  # little endian byte count
    packet += array
    packet += bytearray([0x0d, 0x05])  # IR terminator

    # Add 0s to make ultimate packet size a multiple of 16 for 128-bit AES encryption.
    remainder = (len(packet) + 4) % 16  # rm.send_data() adds 4-byte header (02 00 00 00)
    if remainder:
        packet += bytearray(16 - remainder)

    return packet

if __name__ == '__main__':
    import sys

    for code in sys.argv[1:]:
        pronto = bytearray.fromhex(code)
        pulses = pronto2lirc(pronto)
        packet = lirc2broadlink(pulses)

#        print
#        print binascii.hexlify(packet)

        print
        print base64.b64encode(packet)

The output of the script did work but there were not enough repeats and the TV didn’t respond on this virtual ‘button press’. I used this Code Generator to change the repeats to 5.

There might be other ways, but this worked for me at least. Hope it’ll help others.

4 Likes

Thank you very much for sharing this. It took me a lot of time to convert Denon IR codes to ProntoHEX and then to Broadlink, but after figuring out all the steps it still didn’t work.

You provided the final piece of the puzzle: changing the repeat to 1 and now its working. Very happy, thank you for posting!

I think there is a much easier way to get the codes. Documented here: https://www.home-assistant.io/components/switch.broadlink/#how-to-obtain-irrf-packets

"HOW TO OBTAIN IR/RF PACKETS?

Choose Call Service from the Developer Tools. Choose the service switch.broadlink_learn_command from the list of Available services: and hit CALL SERVICE. Press the button on your remote with in 20 seconds. The packet will be printed as a persistent notification in the States page of the web interface."

That worked fine for me for my LG air conditioning unit: Broadlink IR Climate Component

That works of course, but you are limited to the buttons that are present on your remote. Usually there are more IR commands available. For example: your remote might contain a power TOGGLE button (on or off), which you can capture with the Broadlink learning command.

But in databases online (that e.g. the SURE app is using) you can find discrete command for POWER OFF and POWER ON. Those are way more useful in automations, because you know for sure your TV will only turn on and not off, independent of it’s current state.

Just saying: there are good reasons why you might want to use a different approach for obtaining your IR codes :slight_smile:

2 Likes

I’m a little confused as to the final format needed for my rm mini. I found a code from my receivers manufacturer and it looks like this, 0000 0068 0000 0022 0168 00b4 0016 0043 0016 0016 0016 0043 0016 0016 0016 0016 0016 0043 0016 0016 0016 0043 0016 0016 0016 0043 0016 0016 0016 0043 0016 0043 0016 0016 0016 0043 0016 0016 0016 0016 0016 0043 0016 0016 0016 0043 0016 0043 0016 0016 0016 0016 0016 0016 0016 0043 0016 0016 0016 0043 0016 0016 0016 0016 0016 0043 0016 0043 0016 0043 0016 06d9. Is that a pronto code? My python fu isn’t so great, but from the looks of it, that script should take the code as the argument, so something like python script.py '0000 0068 ...'? I get an error that Number of pulse widths does not match the preamble. Any ideas? I’ve been working on this for two days and it’s driving me nuts!

Hello!

I do not have many knowledge on Python, but found a working workflow to get the final code you need to use with Broadlink RMs from PRONTO HEX codes:

  1. Use this code to convert the PRONTO HEX, remember to remove all spaces, only number here: https://www.jdoodle.com/embed/v0/eBD. This step wil convert to HEX. I had to clic on Edit this in program JDoodle.com so I can se the CommandLine Arguments ... field, were the raw PRONTO HEX number should be pasted.

  2. Paste the resulting numbers here: http://tomeko.net/online_tools/hex_to_base64.php?lang=en. This step will convert HEX to base64 string, exactly what you need for Broadlink codes.

You can test this code by sending the package using services:

{ "packet" : [ "CODE==" ]}

You can also use this great code to change repeats: https://dimagoltsman.github.io/Random-Broadlink-RM-Code-Generator/#. This is a great tool to creat RF codes also…

You can find many codes on theses sites, like I did. I was able to find ON and OFF commands for my old Sony Receiver, very useful!

https://irdb.globalcache.com/Home/Database
http://irdb.tk/find/

Hope it helps!

6 Likes

For any future lookers, I finally got it to work! Here’s what I did…First, I’m not sure of the code format, but Pioneer has an excel spread sheet full of them. I found the code for my model and button press I wanted and it looked like this:

0000 0068 0000 0022 0168 00b4 0016 0043 0016 0016 0016 0043 0016 0016 0016 0016 0016 0043 0016 0016 0016 0043 0016 0016 0016 0043 0016 0016 0016 0043 0016 0043 0016 0016 0016 0043 0016 0016 0016 0016 0016 0043 0016 0016 0016 0043 0016 0043 0016 0016 0016 0016 0016 0016 0016 0043 0016 0016 0016 0043 0016 0016 0016 0016 0016 0043 0016 0043 0016 0043 0016 06d9

Next, I went to http://www.irdb.tk/convert/ and in the “Pronto Hex” box I pasted the code and clicked on the “Get protocol information” button. Next I clicked on the “Pronto Hex” tab and copied that code into the clipboard. This is the confusing part for me, apparently I converted a pronto hex code into a pronto hex code? Not sure how the two differ, but it works.

After that, I copied the python script above into a file named “broadlink.py”. In the terminal, I ran the command

python broadlink.py "0000 006C 0022 0002 015B 00AD 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0041 0016 0041 0016 0041 0016 0041 0016 0041 0016 0041 0016 0016 0016 0041 0016 0016 0016 0041 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0041 0016 0016 0016 0041 0016 0041 0016 0041 0016 0041 0016 0041 0016 0041 0016 0622 015B 0057 0016 0E6C"

You should obviously replace the code above, starting with 0000 and ending with 0E6C, with the code you got from the irdb.tk/convert site. The python script will output something like:

JgBIAAABKZQSOBISEjgSEhISEjgSEhI4EhISOBISEjgSOBISEjgSEhI4EjgSEhI4EjgSEhISEhISEhISEjgSEhISEjgSOBI4EgAFHQ0FAAAAAAAAAAAAAAAAAAA=

Go to https://dimagoltsman.github.io/Random-Broadlink-RM-Code-Generator/ and click the “Change repeats” tab, paste the output from the python script into the “Your code” box, set the repeats to 5 and click “Generate”. This will be your final code to put in your HA config file!

3 Likes

I’m trying the same thing here, but I get this when running your exact command;

File “c:/remote.py”, line 52
print base64.b64encode(packet)
^
SyntaxError: invalid syntax

You have to call the script in cmd (Windows) together with your Pronto HEX Code.
Just like:

python pronto2broadlink.py "0000 006C 0022 0002 015B 00AD 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0041 0016 0041 0016 0041 0016 0041 0016 0041 0016 0041 0016 0016 0016 0041 0016 0016 0016 0041 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0016 0041 0016 0016 0016 0041 0016 0041 0016 0041 0016 0041 0016 0041 0016 0041 0016 0622 015B 0057 0016 0E6C"

Did get the same error on a windows machine.
It works perfect on ubuntu 16.04 LTS.

I used your way of converting IR codes and some of them worked while using them in a fire event way but when I copied them into switches they don’t work anymore… Super weird.

tv_lg:
friendly_name: “LG-TV Power”

command_on:
‘JgBgAAABJ5IUERMSEzYTEhMRFBETERQREzYUNRQREzYUNhM2EzYUNRQRExITERQ1FBEUERMRFBETNhQ1FDYTERQ2EzYTNhQ1FAAFIQABJ0gUAAxIAAEnSRQADEgAASdJEwANBQAAAAAAAAAA’
command_off: ‘JgVQAAABKJMSEhISEjcSEhISEhISEhISEjcSNxISEjcSNxI3EjcSNxI3EhISNxISEhISEhI3EjcSEhI3EhISNxI3EjcSEhISEgAFGgABKEoSAAxWDQUAAAAAAAA’
philips_power:
friendly_name: “Soundset Power”
command_on: ‘JgBMAFgcDx0PDg8OLCsPDhAODw4PDh0dDw4dDg8dDw4PDh0ODw4PAAq6Vx4NHg0QDRAqLg4PDw4PDg8OHR0PDh4NEBwQDQ8OHg4PDg8ADQUAAAAAAAAAAAAAAAA=’
#command_off: ‘JgAqAFcdDh0ODg4ODh0dDg4ODg4ODg4OHR0ODg4ODg4ODg4OHQ4OHQ4ODgAKwA0FAAAAAAAAAAAAAAAA’

Edit: I didn’t copy the “=” part of the code. Thanks for the help, guys!

Did you increase the repeats to 5?
Edit: also, can you format your code using the instuctions at the top of the page? My impression is your quotation marks might be off, but I can’t be sure.
Edit 2: for the LG-TV Power command off, I noticed your string length is incorrect. You have to be able to divide your string length by 4. Your string is 123 characters. You need to add an extra = at the end. That might help.

I think you didn’t copy all characters. A double-click on the code doesn’t highlight the equal sign at the end of a code.
Or like Emphyrio said, you may need to add an extra equal sign.

And please use "preformatted text" </> when posting yaml-code.

Thank your for the tips. I will take a look at it later.

And I tried to indent the yaml text but when I try to do that to the IR codes it just adds 4 breaks to the text without indenting it.

Edit: I didn’t copy the “=” part of the code. Thanks for the help, guys!

You guys may want to try https://github.com/elupus/irgen

It is a python program that can take the protocol, device, subdevice, function from irdb.tk and output base64 encoded data for home assistant.

pip install git+https://github.com/elupus/irgen.git#egg=irgen

2 Likes

Hi Schneider. I also have an old Sony Receiver and have been struggling to find a working Power On/Off code. I tried the databases with other equipment to see if conversions work and they work fine with a Samsung TV, but I have tried all Sony receivers I have seen and they do not work on mine. can you send me the codes you used to test it? Thanks

Ben

Hello!

These are the codes I am using now:

  home_theater_sala_power:
    sequence:
    - service: broadlink.send
      data:
        host: 192.168.1.34
        packet: JgBmAFATKBMVEikSFRIpEhUSFhEWEhUSFRIVEikSKRIWERYAAsFQEikSFhIpEhUSKRIVEhUSFhEWEhUSFRIpEikSFRIWAALBUBIpEhYRKRIWEikSFRIVEhUSFhEWEhUSKRIpEhUSFQANBQAA==
    alias: "Ligar ou desligar"
  
  home_theater_sala_power_on:
    sequence:
    - service: broadlink.send
      data:
        host: 192.168.1.34
        packet: JgwiAE4TExMnEycTJxMTEycTExMTExMTExMTEycTJxMTExMAAq8NBQAAAAA==
    alias: "Ligar"
  
  home_theater_sala_power_off:
    sequence:
    - service: broadlink.send
      data:
        host: 192.168.1.34
        packet: JgwiAE4TJxMnEycTJxMTEycTExMTExMTExMTEycTJxMTExMAAqANBQAAAAA==
    alias: "Desligar"

I suggest you check these IR remote databases, I’ve found mine there:

https://irdb.globalcache.com/Home/Database
http://irdb.tk/db/

Good luck!

Thanks!!

They actually didn’t work on my device and I guess they are some of the codes I tried from the databases you mentioned before (I think I tired all of them). Maybe my Receiver is just too old. The strange part is that I am able to control the power from the IHC application, but cannot see what code they are using internally.

1 Like

You might try the SURE app on Android. You need some luck, but sometimes it seems to offer codes not found elsewhere.

I could control the device with SURE app but how do I extract the codes??