vdbg / hubibtpresence

Bluetooth scanner for Hubitat
MIT License
0 stars 0 forks source link
ble bluetooth hubitat python scanner

Hubitat Bluetooth presence detection

This app allows for reporting to Hubitat the proximity of Bluetooth devices (called from here on "BT device(s)") to a specific device (called from here on "the BT scanner"), via virtual presence devices in Hubitat.

This app has been successfully tested on Tile Pro and Tile Mate trackers.

Caveats

BT tracking is not an exact science. Here's a non-exhaustive list of issues that can impact the scanner's reliability:

Pre-requisites

Running the tool directly on the BT scanner

It is recommended to first run the tool directly on the BT scanner for troubleshooting and tuning:

Finding the MAC address(es)

The BT scanner needs to know what MAC address(es) to monitor and what virtual presence devices in Hubitat they map to. MAC adresses are of the form xx:xx:xx:xx:xx:xx with each x a letter between A and F or a digit.

Your task is to end with a list of pairs (MAC address, device name).

List all devices with stable MAC addresses

The BT scanner can only track devices with stable MAC addresses. To determine these, run the following with the BT device to monitor turned on and close to the BT scanner (same room):

The file stable.txt contains all BT MAC addresses that were seen in both scanning sessions, meaning devices that are more likely not to constantly randomize their MAC or to stop to advertise their presence.

The easiest way

The easiest option is to ask directly the BT device (for example in the case of a phone, by going in the phone's settings, about section) or the app that manages it.

If the BT device MAC address is not in the previous list, you may need to do one of these:

Harder ways

If you could not figure out the MAC address the easy way, here's another option:

Alternatively, if there are very few devices, sudo stdbuf -oL hcitool lescan may be sufficient (Ctrl-C to stop the scan).

Notes:

The hardest ways

Another approach is running sudo btmgmt find | grep rssi | cut -d ' ' -f 3 | sort once with the BT device on/near and once with the device off or away and comparing results. This can be hard when there are many BT devices with constantly changing MAC addresses in the vicinity.

Deciding to gate presence on RSSI

RSSI is a measure of the strength of the signal between the BT scanner and the BT device. RSSI values are negative. The closer the value is to 0, the stronger the signal is and the closer the BT device is from the BT scanner. Below is an estimate of the relationship between RSSI and distance.

RSSI value Probable distance
>=-55 Very close, few meters
-55 to -67 Same room
-67 to -80 Adjacent room
-80 to -90 Few rooms away
<= -90 Neighbors' house

Note: RSSI is not only affected by distance, but also by the environment (presence and type of walls, interferences, ...) and by the characteristics of the BT device (battery level, type of antenna, ...).

The app can optionally treat the device present or absent based on the RSSI value, when the minRSSI value is provided in config.yaml.

Running in a Docker container

It is recommended to test/tune the scanner before moving to containers. Once testing is completed:

sudo docker run -d \
  --name hubibtpresence \
  --restart always \
  --memory=100m \
  -v path_to_your/config.yaml:/app/config.yaml \
  --privileged --net=host \
  vdbg/hubibtpresence:latest

With:

Note: when the app within the container is performing its scans (every 3 minutes by default), the host won't be able to use BT anymore. For example sudo btmgmt find on the host will return Unable to start discovery. status 0x0a (Busy) during this time.