Presence detection with low cost BLE devices and Raspberry Pi/Zero W

This is a few notes on BLE-based presence detection.

The obvious use case seemed to be to have your phone as the presence token. I started off looking into Eddystone and iBeacon devices but they seem to be too application oriented and I couldn’t get my phone working with them - interestingly this seems to due to the security differences between stock Android and LineageOS - but I couldn’t really get this to work usefully.
So I started fishing around for cheap things to play with and found a plentiful supply on eBay. The common devices at the moment are iTag and Nut, which are both application oriented but are really just simple BLE devices which send a MAC address and name. The iTags are square or triangular Bluetooth 4.0 BLE devices and cost a couple of quid each. I got a couple from a UK supplier (there are a few). The Nut is a square device an inch or so square, and i ordered a couple at £3.00 or so that turned out to be coming from China and still haven’t arrived, so I tried a couple at £6.00 or so which turned up in a few days.

As I now understood that what I was looking for was presence detection it was easier to find what I needed. The HappyBubbles devices look good but $30 still seems a bit much for fiddling. Then I came across Room Assistant and everything clicked. My Home Assistant device is a Raspberry Pi 3 B that doubles as a media player. As this has built in bluetooth it was easy enough to install the tools to detect the BLE devices and to install Room Assistant on it. You’ll also need an MQTT broker - mosquitto or the native broker in Home Assistant should be fine.

The tags can be detected using bluetoothctl. Starting it from the command line should find your bluetooth interface (usually hci0) and then running scan on should find broadcasting devices, which should look like this:
Device C1:BE:FF:73:AD:74 nut
That’s the tag’s MAC address and name. It might be possible to change the name but the MAC address should be all you need.
Install Room Assistant as per the instructions in the README on the repository. It’s easier to run it as root rather than as a user but both should work.

Setting up room assistant is fairly simple:

 "mqtt": {
    "enabled": true,
    "url": "mqtt://mqtt.host.name:1883",
    "username": "user",
    "password": "password",
    "reject_unauthorized": true,
    "topic": "Room1"
  },
  "console": {
    "enabled": true
  },
  "ble": {
    "enabled": true,
    "channel": "room_presence",
    "max_distance": 5,
    "whitelist": ["c1beff73ad74"],
    "use_mac": false,
    "system_noise": 0.01,
    "measurement_noise": 3,
    "update_frequency": 5
  }, 

The main variables are the host and port of your MQTT broker, the username and password and the topic. This usually represents the room that Room Assistant is in.

In the ble section, add the MAC address of your tag to the whitelist as shown, in lower case with the colons removed: the client and server seem to be happier with a plain string.

Test with npm start index.js. You should see some messages that the interfaces are being initailised and then the payload being sent to the broker, which looks like this:

[room_presence]                             {
[room_presence]                               "id": "c1beff73ad74",
[room_presence]                               "name": "nut",
[room_presence]                               "rssi": -59,
[room_presence]                               "distance": 0.9933927152745275
[room_presence]                             }

You can confirm that the broker is receiving the messages with mosquitto_sub using the topic room_presence/#

Enable the mqtt_room sensor in Home Assistant like this:

 - platform: mqtt_room
    device_id: "c1beff73ad74"
    name: "nut"
    state_topic: 'room_presence'
    timeout: 5

Restart and you should see a sensor state which should return your topic as the value, or away if your tag is sleeping.

However, this isn’t much use with one room. You will need multiple detectors for your house, and this is where the Raspberry Pi Zero W comes in. These are the newest variant of the Pi, with wifi and bluetooth. and 512MB of RAM They sell at around £9.00 in the UK. Pimoroni or Adafruit are good sources, and Pimoroni have their great little Coupe case and adaptors in a package for £19. Add in an SD card and you have the basis for another detector. Possibly a bit of overkill compared to a HappyBubble but it will be capable of doing other things as well.
Downside is that the nodesource distribution of node isn’t available for the Pi Zero’s ARM6 processor so you have to use the binary bundle (don’t use the node package in Raspbian), and there’s a bit more work to set it up but not a lot. The only difference in setup otherwise is a different topic for the mqtt configuration. Place the Zero in another room and make sure it can connect through wifi and your mqtt_room state should show either room depending where your tag is.

Next: how to make that do something useful…

14 Likes

Been playing with one of the Nuts today and paired it to my the app on my phone to see if that would keep it awake but it seems to have the effect of binding it to the phone and making it undiscoverable. Lucky I bought two then. It has a useful location function though, which would be good if it had an API, and a bidirectional find function.

1 Like

Has anyone got this working use CloudMQTT as the broker? I’ve never been able to get the bridge working so haven’t set up my own broker yet.

What sort of errors are you getting?

For that matter, you can just install mosquitto on your Pi for testing, it doesn’t add any load.

Hi,

Sorry, I’m a newbie, but very interested in home automation and person(al) room presence detection.

Do I understand correct that you’re setup would look like:
1 Raspberry Pi running Home Assistant
Multiple Raspberry Pi Zero W’s around the house running Room Assistant.

The Raspberry Pi Zero W’s pick up the Bluetooth signal, filter it in room assistant and send it via MQTT to Home Assistant.

It looks great, and probably a cheaper alternative then Happy Bubbles, as you can for e.g. run Kodi on the same raspberry pi zero w.
But how does it work out when multiple “Room Assistant” devices pick up the same tag?

Yes, Pi Zeros around the house. I admit they’re overkill somewhat but obviously you can use them for other things.
Room Assistant and mqtt_room work out proximity between them using signal strength. At the moment I haven’t had any issues with it as our house is pretty solid but I am going to try with upstairs rooms shortly so will update.

1 Like

Hi, can i ask if this setup work with Bluetooth LE Privacy devices? My watch constantly change MAC address.

Mac addresses wouldn’t work, obviously, so the device would need another identifier, and I’m not sure how that works with LE Privacy devices.

I bought a couple of Pi Zero W’ today.
They will arrive next week Tuesday.
then I’ll give Room Assistance a try.
I figured, if it doesn’t work out it was worth a try and I still can use the Pi Zero W’s for something else (Kodi,…)

I got this working with the pi zero w today, let me know if you get stuck buddy.

Thanks for this, finally got it working after seeing your post.

Just a quick question on how your implementing these with automations in HASS? I’ve created the sensor and it updates with this distance value. I was hoping or occupied/unoccupied kind of thing though. My sensor always shows the distance even once it looses contact it just leaves the last known distance on the sensor.

1 Like

Scratch that, just seen the away timeout.

Hi, unfortunately I did not get this working, nor on a pi zero w, nor on a Raspberry PI3.

it constantly gives me error:
"
noble warning: adapter state unauthorized, please run as root or with sudo
or see README for information on running without root/sudo:
"

Now, what I also find strange is that in the beginning we need to install libbluetooth-dev libUSB.
is this not for a USB Bluetooth device (device plugged into a usb port) instead of using the onboard Bluetooth adapter.

I really hope to get this working.

That’s an issue with noble, if you search for it there’s a fix, an ACL or permissions issue if I remember correctly. The bluetooth issue may be to do with how node works rather than Raspbian or the Pi Zero.

I’ve got CloudMQTT working as a bridge, I’ll write it up in another post.

What version of node do you have installed?

The latest version (version 8)

I didn’t think you could run that version on the Pi Zero? I got it working with version v6.11.5

ah, that’s a good input, I’ll try that, thanks!

Hi GetSmokes,

For some reason I still can’t get this to work.
would you feel up for it to make a kind of walkthrought please?
I think it would help out a lot of people.

Thanks in advance,
Regards,
Dieter