Presence detection (Bluetooth 4.0 Low energy Beacon)

From Domoticz
Jump to: navigation, search

Nut mini beacon.jpg Nut beacon.jpg


Check the presence of a list of beacons (BlueTooth Low Energy V4.0) and update uservariables in Domoticz accordingly.

Required in Domoticz, a uservariable of type String for each BLE Tag

Beacon uservariable.jpg

Script operates in 2 mode. Choose for each beacon which one you want :
- REPEAT MODE : For beacon in range, update the uservariable every 3 seconds with the RSSI (strength Signal). And "AWAY" otherwise.
- SWITCH_MODE : For beacon in range, update only 1 time the uservariable with "HOME". And "AWAY" otherwise.
The detection is very fast : around 4 seconds. And the absence is verified every 5 seconds by comparing the hour of the last presence with a time out for each beacon.

What is it For

You can use this to see if a person (who always carries his/her beacon) is at home or not, and trigger events based on this.
I plan to equip all the family keys with beacon and automatically arm/disarm my Domoticz alarm system by checking the presence of a beacon. Domoticz will trigger the alarm on when the last beacon leaves the home.

Hardware & Beacon

This Howto is made for RaspberryPi

BLE Adapter

You will need a Bluetooth 4.0 compatible USB adapter to use with the Pi. The following devices have been tested:
- I have this one :
- Plugable USB-BT4LE adapter: (uses BCM20702 chipset)
- CSR chipset adapter


This beacon works fine :
You may find them for around 5€
- lowest price:
Presence is detected in 4 to 8 seconds. (Thus on the contrary, the absence may be decided in less than 15 seconds when the tracker goes out of range...). The SODIAL BLE adapter detects it to a distance of 12m (in direct view) or upstairs across 2 floors.

- Xiaomi Mi Band (make it discoverable via the Mi app, then it can be found)
Doesn't Work yet
I haven't succeeded yet in using it. It stops emitting when it is disconnected from its Phone apps.
- .
- - TI SensorTag

Installation instructions

The script is hosted here :

Plug your BLE adapter.

Bluez - the linux bluetooth stack

(for synology, check here :

Bluez requires a 3.5+ kernel

First install Dependencies that the bluez library uses.

   sudo apt-get update
   sudo apt-get install -y libusb-dev libdbus-1-dev libglib2.0-dev libudev-dev libical-dev libreadline-dev

And BLE need a >5.3 bluez stack version.

Check the version available with apt-get
Beacon bluez install.jpg

When the version will be higher , you could do a simple : sudo apt-get install bluez
But now, the available version is under 5.3, then you must compile from the source.

Check the last version here : (and change the filename below accordingly. Mine was 5.9 ). The compilation take around 20 minutes and at the end, you will need to reboot.

sudo mkdir bluez ; cd bluez ; sudo wget
sudo tar -xJf bluez-5.9.tar.xz ; cd bluez-5.9 ; sudo ./configure --disable-systemd ; sudo make ; sudo make install ; sudo apt-get install python-bluez python-requests
sudo cp attrib/gatttool /usr/local/bin/
sudo shutdown -r now

TODO check the right of gatttool. (set execution right chmod +x ?)

First Test

To ensure the USB Bluetooth device is being seen, run


With the Pluggable adapter shown above, the result will look like this:

Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp.
Bus 001 Device 004: ID 04d8:f55e Microchip Technology, Inc.
Bus 001 Device 005: ID 0403:6001 Future Technology Devices International, Ltd FT232 USB-Serial (UART) IC
Bus 001 Device 006: ID 0a12:0001 Cambridge Silicon Radio, Ltd Bluetooth Dongle (HCI mode)

For me, the adapter is the one on Bus 001 Device 006, close by my audio card, RFXCOM, USB relay card ... To show more information about it, do:

sudo lsusb -v -d 0a12:

If this is working, run the hciconfig tool:

pi@raspberrypi ~ $ hciconfig
hci0:	Type: BR/EDR  Bus: USB
	BD Address: 00:02:72:14:27:0E  ACL MTU: 1021:8  SCO MTU: 64:1
	RX bytes:2715 acl:1 sco:0 events:146 errors:0
	TX bytes:2500 acl:0 sco:0 commands:133 errors:0
!!!  The python script will work only if your adapter is on hci0:  If not, you will have to adapt the script !!!

Get this test script :

sudo python

It shall activate the hci0 port, and detect all beacons around. It's important to note the MAC Address and the frequency with which the beacon emits, in order to understand what happened next

Install Script

sudo wget
sudo mv /usr/local/bin/
sudo chmod 755 /usr/local/bin/

Edit the script

(Read the comment in the script)

- For manual launch and testing, display Log to console with debug verbose


- For production : log in file, only critical


Beacon Data
Configure your Beacons in the TAG_DATA table with : [Name,MacAddress,Timeout,0,idx,mode]
- Name : the strictly exact name of the uservariable used in Domoticz. Case sensitive !
- macAddress : of the beacon. Case insensitive
- Timeout is in seconds the elapsed time without a detection for switching the beacon AWAY. Ie :if your beacon emits every 3 to 8 seconds usually, a timeout of 15 seconds seems good.
- 0 : used by the script (will keep the time of the last broadcast)
- idx of the uservariable in Domoticz for this beacon
mode : SWITCH_MODE = One update per status change / REPEAT_MODE = continuous updating the RSSI every 3 seconds

TAG_DATA = [  

Domoticz :
Change your IP and Port here :
For the most common case, you use your localhost

 URL_DOMOTICZ = 'http://localhost:8080/json.htm?type=command&param=updateuservariable&idx=PARAM_IDX&vname=PARAM_NAME&vtype=2&vvalue=PARAM_CMD'

Or for those who have strengthened their installation with SSL, certificate, password ...


Second Test : Run the script manually

sudo python /usr/local/bin/

The output will be displayed on screen , or in log file according to the parameter in the script:
Display the log file with :

sudo more /var/log/check_beacon_presence.log

If uservariables are updating correctly, go head with the installation and edit the script to log to file.

Autorun as a service

And now the installation of the service (and auto run at start of the raspberryPi)

It Depends of your system. Do you use wheezy or jessie ? Check with :

echo $(lsb_release -is) $(lsb_release -cs) $(lsb_release -rs)

Method 1 for Debian wheezy system

sudo wget
sudo mv check_beacon_presence /etc/init.d/
sudo chmod 755 /etc/init.d/check_beacon_presence

Check with starting the service manually :

sudo /etc/init.d/check_beacon_presence start

Check with :

sudo /etc/init.d/check_beacon_presence status

other commands : start, stop, restart, status

And Finally, register the service to autorun the script when the Raspberry Start :

sudo update-rc.d check_beacon_presence defaults

Memo removing the autorun :

sudo update-rc.d -f check_beacon_presence remove

Method 2 for Debian jessie system (thanks to justin81)

The autostart has to be modified to use systemctl instead of the update-rc.d method to run at the end of all initializations.

sudo nano /lib/systemd/system/check_beacon_presence.service


Description=check_beacon_presence Service




sudo chmod 644 /lib/systemd/system/check_beacon_presence.service
sudo systemctl daemon-reload
sudo systemctl enable check_beacon_presence.service
sudo reboot

Check if it is running:

sudo systemctl status check_beacon_presence.service -l

Final test

(with autorun enabled)

sudo reboot
ps -ef | grep python

You should get running

Use in Domoticz


Write script based on the uservariable.
For using user variables in Lua scripts, there are three tables available: uservariables, uservariables_lastupdate and uservariablechanged. The uservariablechanged table (with only the variable that triggered the event) is only available in your event scripts (script_variable_name.lua), the other two are always available. Reference your variable by name, e.g. if uservariables["MyVar"] = ... Have a look here :

For example a variable script :

--Variable scripts receive 7 tables from Domoticz: 
-- otherdevices, otherdevices_lastupdate, otherdevices_svalues, otherdevices_idx, uservariables, uservariables_lastupdate and uservariablechanged

commandArray = {}

for variableName, variableValue in pairs(uservariables) do
    if string.sub(variableName,1,3)=="Tag" and variableValue ~= "AWAY" then
if otherdevices['Alarm'] == 'On' and beaconHome>=1 then   -- switch Off Alarm because 1 beacon come back Home
    table.insert (commandArray, { ['Alarm'] ='Off' } )
elseif otherdevices['Alarm'] == 'Off' and beaconHome==0 then  -- switch On Alarm because all beacon are away
    table.insert (commandArray, { ['Alarm'] ='On' } )

return commandArray

Other example on the forum topic :



The forum topic about this script is here:
Review page for ble beacons : => pybluez\examples\advanced\

Other command

Reset the bluetooth adapter (hci0 in the above example) with:

sudo hciconfig hci0 down
sudo hciconfig hci0 up

Find the MacAdress of your beacon with hcitool (from bluez installation)

pi@raspberrypi ~ $ whereis hcitool
hcitool: /usr/local/bin/hcitool
pi@raspberrypi ~ $ sudo hcitool lescan
LE Scan ...
FB:14:78:38:18:5E nut
FB:14:78:38:18:5E (unknown)