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

Easy to do… Download a ble app from your respective appstore and you can scan / retrieve from there.

1 Like

Awesome! Now ill wait until they arrive.

Could not get it working. See the device and Mac with Bluetoothctl but never got a new device.

For anyone who has this up and running, how is the performance? How much latency in detecting is there when entering a room? Does the detection ever “drop” while you’re still in the room? Do you have to fiddle with the locations of the detectors, or is anywhere fine? Is there trouble with “bleed” of detection between rooms?

Interested in the practicality and robustness of this approach before I go down this road.

The detection rate can be set to what you want, down to miliseconds. I probably wouldn’t use it to switch lights on when you enter a room for example but it’s generally reliable. The detection is real time so the beacons can power off - some do if they aren’t paired with anything for a while.
Detection is based on signal strength and Room Assistant does calculations to work out the closest but much will depend on wall and floor thickness in your house so thinking about the positions of detectors may help but your mileage may vary.

Thanks for this! I got my first node up and running and ordered a Pi zero to set up the next one!

I’m using Android as a Bluetooth beacon using a Beacon Simulator app from the play store and I noticed something weird. When using the BLE mode for roomassistant, the detected ID of the device was not the UUID I had set and it was not static in that it changed every few minutes. Switching to MAC didn’t help as it exhibits the same issue.

When I switched room assistant and the phone to iBeacon things worked fine and the uuid was the ID that room assistant detected. This could be an issue with the app I’m using but I’m indifferent to protocols as long as they work!

Unfortunately it looks like the refresh rate and max distance for iBeacon in room assistant are not coded in correctly. I hardcoded these in in my fork of room-assistant here:

With regard to the changing UUIDs and MACs, all I can think is that it’s a security feature for unpaired devices. If you dig below the surface of the Bluetooth it quickly becomes one of those standards that has been designed by a very large committee…

Hi! I have installed everything considering the readme of github proyect and @getsmokes ’ suggestions, but i have the same problem as @accelle .
When i run from console sudo npm start, i get the following:

[email protected] start /home/pi/room-assistant
node index.js
Starting Room Assistant…
Connected to MQTT server
Started console publisher
BLE scanner was initialized

But i dont see anything else. If i run bluetoothctl then i see different devices.
I am unable to guess where the problem is.
Any help please?
Thank you very much in advance.

The logs will tell you what is being detected or what errors are being thrown, look in the logs directory for the current date. You can also set up mosquitto_sub on your mqtt server to see what is being received.

https://github.com/mKeRix/room-assistant/issues/33

Might help u

1 Like

Thank you very much for your replys. It was the same issue!
I was using [""] instead “” on the white list.
Now i have it running perfectly.
Thanks!!!

So this is going to be long, but below I’ve detailed how I’ve gotten this working for my own personal use, using an ibeacon simulator app for my android phone.

I am by no means an expert on any of this, this is simply the steps I took to get this to work for me. A lot of this is repeated in this thread, in the room assistant repository and elsewhere, but my goal is by placing it in the order I have below, this will click for others. I’m posting it here in hopes of helping others and for any feedback to determine if there is a more efficient way to complete what I’ve gotten working.

1. Flash Raspbian Stretch Lite on SD card intended for the Pi Zero W. https://www.raspberrypi.org/downloads/raspbian/

2. After flash, I added my wifi network to the Pi. I then use PuTTY from my Windows machine to SSH into the Pi Zero W.

3. Checked for updates to Rasbian

sudo apt-get update
sudo apt-get upgrade -y

4. Run raspi-config and update settings as needed. I updated host name and my password.

5. I used this script to update NodeJs

my node version currently shows:

pi@RoomAssistant1:/ $ node -v
v9.5.0

6. I used Stretch Lite when I installed Rasbian, so I needed to install Git:

sudo apt-get install git

7. Install additional dependencies:

sudo apt-get install bluetooth bluez libbluetooth-dev libudev-dev libusb-1.0-0-dev

8. Install Room Assistant

git clone https://github.com/mKeRix/room-assistant.git
cd room-assistant
npm install

9. Setup Room-Assistant as a service:

sudo nano /etc/systemd/system/room-assistant.service

10. Copy and paste the following.

[Unit]
Description=Room Assistant service

[Service]
ExecStart=/usr/bin/npm start
WorkingDirectory=/home/pi/room-assistant
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target

11. Ctrl+O, Enter, then Ctrl+X to save and close the service you’ve created.

12. Install Docker

curl -sSL https://get.docker.com | sh

13. Run with docker:

docker run --network=host -d --name room-assistant -v ~/room-assistant/config:/room-assistant/config mkerix/room-assistant:latest-rpi

14. Copy default.json config to local.json and open it. Edit the file to your own personal mqtt server & ibeacon settings.

sudo cp config/default.json config/local.json
sudo nano config/local.json

At this point, the only items I’ve changed from the default local.json:

  • For the MQTT section, I used the MQTT server IP & Port, username, and password from my Home Assistant configuration.

  • Under ibeacon, i changed enabled to True

Example:
/home/pi/room-assistant/config/local.json:

{
“app”: {
“unsafe”: false
},
“mqtt”: {
“enabled”: true,
“url”: “mqtt://enter_your_mqtt_ip:and_port”,
“username”: “username_if used”,
“password”: “password_if used”,
“reject_unauthorized”: true,
“topic”: “livingroom”
},
“console”: {
“enabled”: true
},
“ble”: {
“enabled”: false,
“channel”: “room_presence”,
“max_distance”: 0,
“whitelist”: ,
“use_mac”: false,
“system_noise”: 0.01,
“measurement_noise”: 3,
“update_frequency”: 5000
},
“ibeacon”: {
“enabled”: true,
“channel”: “room_presence”,
“max_distance”: 0,
“whitelist”: “”,
“system_noise”: 0.01,
“measurement_noise”: 3,
“major_mask”: “0xFFFF”,
“minor_mask”: “0xFFFF”
},
“temper”: {
“enabled”: false,
“channel”: “temper”,
“interval”: 60000,
“scale”: 1,
“offset”: 0
},
“gpio”: {
“enabled”: false,
“only_send_updates”: true,
“ports”: ,
“qos”: 0,
“retain”: false
},
“shell”: {
“enabled”: false,
“commands”: ,
“qos”: 0,
“retain”: false
}
}

Note: I also downloaded beacon simulator for android which is free.
I decided to go with ibeacon component over the BLE as the UUID did not change if I shut the beacon off and on, whereas when I used the BLE component the ID was simply the MAC address of the current BLE and changed if I turned the app on or off.

15. Ctrl+O Enter, then Ctrl+X to save and close the local.json you’ve just modified.

16. Stop and restart your room-assistant service.

sudo systemctl stop room-assistant.service
sudo systemctl start room-assistant.service

17. From the room assistant directory run:

sudo npm start index.js

Below is a sample of the output:
Ex:

pi@RoomAssistant1:~/room-assistant $ cd room-assistant
pi@RoomAssistant1:~/room-assistant $ sudo npm start index.js

> [email protected] start /home/pi/room-assistant
> node index.js “index.js”

                                       Starting Room Assistant...
                                       Connected to MQTT server
                                       Started console publisher
                                       iBeacon scanner was initialized

[room_presence] {
[room_presence] “id”: “3453b11223434bb295731cadd7b2e6e3-0-0”,
[room_presence] “uuid”: “3453b11223434bb295731cadd7b2e6e3”,
[room_presence] “major”: 0,
[room_presence] “minor”: 0,
[room_presence] “rssi”: -72,
[room_presence] “distance”: 1.5537514174765523,
[room_presence] “accuracy”: 1.4939266621809253,
[room_presence] “measuredpower”: -65,
[room_presence] “proximity”: “near”
[room_presence] }

18. Ctrl Z. Now that you have your iBeacon id, you can plug that into the local.json under whitelist. The id not the UUID.

sudo nano config/local.json

Update your whitelisting as shown below. Ctrl+O, Ctrl+X to save and close.

Example:

“ibeacon”: {
“enabled”: true,
“channel”: “room_presence”,
“max_distance”: 0,
“whitelist”: “3453b11223434bb295731cadd7b2e6e3-0-0”,
“system_noise”: 0.01,
“measurement_noise”: 3,
“major_mask”: “0xFFFF”,
“minor_mask”: “0xFFFF”
},

19. Open a new SSH instance for your home assistant, not room assistant. We’re checking to see if the MQTT server is broadcasting your ibeacon.

pi@Hassbian:~ $ mosquitto_sub -h host_ip_here -p port_here -u **username_if_neede**d -P password_if_needed -v -t room_presence/livingroom

Output:

room_presence/livingroom {“id”:“3453b11223434bb295731cadd7b2e6e3-0-0”,“uuid”:“3453b11223434bb295731cadd7b2e6e3”,“major”:0,“minor”:0,“rssi”:-70,“distance”:1.6380266201803306,“accuracy”:1.3320511843278362,“measuredpower”:-65,“proximity”:“near”}

20. Add the sensor in home assistant.

In my sensors.yaml

- platform: mqtt_room
device_id: "3453b11223434bb295731cadd7b2e6e3-0-0"
name: "Beacon Name Here"
state_topic: 'room_presence'
timeout: 5
away_timeout: 30

I also created a group in my groups.yaml for the time being:
ble_trackers:
name: BLE Trackers
entities:
- sensor.s7

Note: The sensor will show in Home Assistant as sensor.name. Mine currently shows sensor.S7

  1. Restart your Home Assistance Instance.

  2. Profit.

https://imgur.com/a/CCPGa
https://imgur.com/a/7qMTa

14 Likes

@touliloup Your setup sounds like the one i want.
I have already seen some videos on YouTube with the guy with the swiss accent.
Some questions about Your hardware:
What kind of esp32 modules do You use, the small or Big ones?
How do You supply them with power?

Raspberry pi zero w has wifi and ble bluetooth built-in, so I don’t think you need any esp modules or nrf addons. I think @HAVEN8 got it working on Rpi zero w which has not been done before. (Rpi 3 was done before).

I too saw the video from “the guy with the Swiss accent” and modified his code

ESP32 presence detection

to send ‘Home’ and ‘Away’ MQTT presence messages to Homeassistant.

To make the build as small as possible I used an ESP-WROOM-32 which can be picked up for under £4 on AliExpress. I housed mine in the case from an old faulty USB Bluetooth stick so it can be plugged into any USB 5v charger.

ESP32 presence detector wiring

ESP32 USB stick

I used a HT7333 to drop the 5v to 3.3v and put a capacitor across the output as I was getting brownout reboots. I’m currently running the detector to make sure long term stability is good, so far everything is working well.

2 Likes

Is there a resistor going from 3.3v to the reset pin? (hard to tell from the photo)

I used a 10k SMD resistor between 3.3v and the “EN” pin (between 2nd and 3rd pins from the top on the right)

1 Like

I have room assistant working on a raspberry pi zero w together with a nut 3. The publishing interval of the nut 3 is every 2 seconds it seems which is fine. Room assistant is set up to send every 5000 milliseconds an mqtt message. It now sends an mqtt message at an highly irregular interval, sometimes every 10 seconds and sometimes not for minutes at a time. While testing with an ibeacon app on my phone it gives updates with regular intervals so I don’t think it is due to room assistant. Anyone else experiencing this problem? Maybe a good tip for a cheap configurable alternative ibeacon/ble beacon?

You probably want to change the timeout value in the MQTT_presence setting. It should be longer than the update period in room assistant or it will show n/a. The Nut and similar devices do go to sleep if they are unpaired but the Nut is pretty sensitive so it’s easy to wake it up if it’s in your pocket.
There seems to be a market for a cheap configurable portable beacon but it would have to be a mass market device to get it to the right price point (about £/$/€10) and it may be that it’s less useful than Zanzito and Find or similar, which I have to admit I now use.

I use multiple rpi zero w ble for home presence to have coverage with same major and minor id. Could I instead use room assistant and still use for home presence and room presence ?

Eg. Particular room presence using room assistant tied to scripts with lights etc and any room presence as “being home” for other scripts for home presence irrespective of which room is activated. Any thoughts or suggestions ?