[Tutorial] Using Bluetooth multimedia buttons in Hassio Automations

Tags: #<Tag:0x00007f7c6e437870> #<Tag:0x00007f7c6e4377a8> #<Tag:0x00007f7c6e437618> #<Tag:0x00007f7c6e437500>

Hi! I’m new to Home Assistant and Hassio, so if you already knew how to do this or are already doing this then awesome! But I couldn’t find anything online with a clear instruction on how to setup Bluetooth multimedia buttons to work on my setup. So I’m sharing how I got my buttons to work with my automations in HA on Hassio in hopes that if someone had the same problem as me, this might be their answer.
Please note: This the way I got it to work and there may other ways to do it but this way worked for me.

I’m building on this 3 year old thread that sort of had the right direction.

In summary, it’s effectively 3 steps:

  1. Pair the Bluetooth device
  2. Use the keyboard-remote integration
  3. Write the automations.

But if you want the details, they are…

My Setup: RPi 3B running Hassio

Step 1. Pair the Bluetooth Multimedia Buttons to the Pi.
I couldn’t find a way to pair the buttons over SSH so I had to control the Pi server directly.

  • When prompted for the “hassio login:”, login with “root”.
  • Once logged on, you’ll need to access the host system by inputing “login”.
  • Go to the Bluetooth agent using the command “bluetoothctl”.
  • Start the Bluetooth scan by the command “scan on”.
  • Wake up the button by pressing the middle button, this will turn on the scan on the button as well.
  • You should see the device popup on the server with its MAC address and name
    e.g. “[NEW] Device XX:XX:XX:XX:XX:XX SmartRemote” (In my instance it was called SmartRemote)
  • Pair the device by entering the command pair along with the MAC address
    e.g. “pair XX:XX:XX:XX:XX:XX”
  • After pairing the agent should say the device is a Human Interface Device (HID)
  • Trust the device by entering the command trust along with the MAC address (not sure if this step is needed)
    e.g. “trust XX:XX:XX:XX:XX:XX”
  • Scanning can now be turned off by “scan off” and the Bluetooth Agent can now be exited with “exit”
  • It is now paired!

We now need to check that the device is actually an input device. We can do this by checking the /dev/input/ folder:

  • Type “ls /dev/input/”, you will see a bunch of things but we are interested in the “eventX”.
  • If you just paired the button, chances are the eventX that matches your button will be the eventX with the highest number.
    In my case, when I do “ls /dev/input/”,
    I see: “by-id by-path event0 event1 event2 event3 js0js1 mice mouse0”. So my bluetooth button will probably be “event3”.
  • We can check if you have the right input device by typing “cat /dev/input/eventX” where X is the number for your case.
  • If you now press any key on the Bluetooth button you will see some gibberish come up on the screen. This means you have the correct input device for the button and make a note of which it is.
  • If you don’t see anything come up on the screen when you press the button, you have the incorrect “eventX” and you can “cat /dev/input/eventX” the other input devices until you find the correct one.
  • Press “Ctrl + C” to get out of the “cat /dev/input/eventX”.
  • We don’t need control the Pi server anymore as It is now time to setup the configuration!

Step 2. Setup the configuration.yaml.
We use the Keyboard-Remote integration to obtain the button presses. Please refer to the integration for more details but in summary:

  • Setup the configuration.yaml in Home Assistant by adding the keyboard remote integration and using the input device found when pairing.
keyboard_remote:
    # type your own "/dev/input/eventX" found above
    device_descriptor: "/dev/input/event3"
    type: 'key_up'
  • Reload the configuration file on Home Assistant

Step 3. Setup the automations.
You can now setup the automations to trigger in the button presses! But before you can do this, you will need to know what the codes the button spits out when the buttons are pressed. If you don’t know you can find out by following galoz11’s comment on this thread:

To setup your automations the trigger will be a “keyboard_remote_command_received” event. Below is an example of one of my automations, I have just have them to toggle the lights on and off.

-   alias: "BT Button Toggle - Bedroom"
    trigger:
        platform: event
        event_type: keyboard_remote_command_received
        event_data:
            device_descriptor: "/dev/input/event3"
            key_code: 164
    action:
        service: light.toggle
        entity_id: light.bedroom

Reload the automation and that’s it! Your button should be working! :slight_smile:

I’m sure this can be done with any bluetooth device that is recognised as a HID so if you have gamepads or bluetooth phone shutters then this should work as well. My bluetooth multimedia buttons also goes to sleep after 30 seconds of inactivity and disconnects from Bluetooth to conserve battery but I’ve have no problems with the automation working even with it disconnecting and reconnecting (because it’s looking for a keyboard input). I’m sure you could even do an automation with it disconnecting and reconnecting but I have not explored this.

I hope you have found this useful.

Extremely good. I will order several of these immediately!

Thanks. I have similar setup and is working only till the Bluetooth button is active. Once it goes to sleep mode and reconnect, the HA is unable to find it. The RPI is able to find it since I see that the Bluetoothctl registers it dutifully. if I restart the HA, it reconnects fine

Have you face issues in reconnection after the sleep cycle?