[Tutorial] Using Bluetooth multimedia buttons in Hassio Automations

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.

6 Likes

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?

Hi, thank you for the tutorial. However, I have a problem with the second part of first the step. I am running on RPI Pi 4, HassOS and I cannot use this code: ls /dev/input/ Is it possible to avoid this step? Or maybe to show the device path in the other way?

Thank you in advance.

Why can’t you run it in ssh?

It said: Folder not exist. I am using ssh terminal in HassOS.

Which version of the ssh add-on are you using?

SSH & Web Terminal 7.4.1

Same issue here: directory /dev/input does not exist and no other device has popped up in /dev.

HassOS 4.14
Supervisor 247

Thanks for sharing about the BLE device pairing.

But I encountered an issue with the “event number” of the newly paired device.

I can pair the BLE device in the SSH terminal and see the device name listed after “paired-devices” is executed. However, I can’t see any newly added event number in /dev/input/ folder. It always displays “by-id by-path event0 event1 event2 mice mouse0”.

Is there anything I missed?

Thank you for your help.

paired 2 devices, an AB Remote control (smartphone shutter button) and a Logitech keyboard. When trying to list input folder only “mice” come up. Any suggestions?

Long time passed, but if someone struggles with the same issue, this is what made it work for me.

  1. install acl sudo apt install acl
  2. setup udev rules as described here
  3. reload udev permission with sudo udevadm control --reload-rules && sudo udevadm trigger

now disconnecting and reconnecting should work. If it fails, test you permissions with getfacl /dev/input/event*

Thanks for the tutorial! :clap:

1 Like

Hi, it works with the device_name as well. I just wrote another article here: [Tutorial] How to use Bluetooth Media Button for Sonos

Sorry to necro an old thread, but I’m having the same issue mentioned earlier and seemingly unaddressed.

Running HASSOS in unraid VM, passed Broadcom BT dongle which seems to work fine - I can scan, pair and connnect to BT devices, including a Samsung Smart Control 2016 remote and XBOX One wireless controller.

Before and after either device is paired, there are no changes to /dev/input/ - no new events listed. It’s always event0 to 3 listed, plus a by-id and by-path directory with links to event1-3.

I have tested the same devices on a RPi4 with its built-in BT and there, new events are created. 0-3 and mouse0 just for the Samsung remote, with event2 being the one to monitor for button presses (verified with evtest)

Any info since the last time this thread was visited?

I’m pretty sure this requires evdev (evdev · PyPI) to be installed, so it won’t work on newer HASSOS installations. It works on a Raspberry Pi with Raspian or anything else with evdev already installed.

I haven’t figured out how to get evdev installed onto HASSOS yet, as I don’t know of any precompiled binaries for this platform and installing from source needs kernel headers and some tools which are also not installed.

1 Like

I am a noob, but any luck given the new releases and some BLE developments? I am specifically interested in hooking up Samsung Smart Control.