Presence tracking via bluetooth in home assistant
Currently, I track bluetooth devices via monitor which leverages mqtt to send a state to an mqtt broker.
This broker is then subscribed to by homeassistant so it can react to changing presence states which I can use inside home assistant for automations based on if I’m home or not.
I’m doing this via monitor because the built in bluetooth trackers of home assistant were pretty lacking:
Sometimes I would not get recognized as home at all, sometimes way too late, and sometimes I’d be presented as gone while still at home.
Installing mosquitto⌗
Monitor will require an mqtt broker, so according to the readme, we have to install a current version of mosquitto.
There might be some in the official raspbian repositories, but you can add mosquitto’s official repositories via:
# get repo key
wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key
# add repo
sudo apt-key add mosquitto-repo.gpg.key
# download appropriate lists file
cd /etc/apt/sources.list.d/
sudo wget http://repo.mosquitto.org/debian/mosquitto-buster.list
After that, install the required packages:
# update caches and install
sudo apt-get update
sudo apt-get install -f libmosquitto-dev mosquitto mosquitto-clients libmosquitto1
After this, you can play around with mosquitto. To start a listening server:
mosquitto_sub -h localhost -t test
Publishing something to the “test”-path will then arrive at the listening server:
mosquitto_pub -h localhost -t test -m "Hello world"
From there, we should make sure that any connections are being made securely, hence we add a user for mosquitto:
# Disable anonymous access
# /etc/mosquitto/conf.d/default.conf
allow_anonymous false
password_file /etc/mosquitto/passwd
# define a user
mosquitto_passwd /etc/mosquitto/passwd USERNAME
# enable and restart mosquitto service
systemctl enable mosquitto.service
systemctl restart mosquitto.service
Installing monitor⌗
Find a nice place for the service to live (for example /opt
) and clone the
repository:
git clone https://github.com/andrewjfreyer/monitor.git
Make monitor.sh executable and execute it. That will generate initial config
files.
Be sure to accept when it asks you to install service files as those will make
running it unattended a breeze.
cd monitor
chmod +x monitor.sh
./monitor.sh
Now edit the generated mqtt_preferences
according to the previously generated
user:
# mqtt_preferences
mqtt_address=localhost
mqtt_user=USERNAME
# please be aware: '@' in the password will lead to monitor being unable to connect
mqtt_password='PASSWORD'
# make the following describe where your tracker is or how you want to call it
mqtt_publisher_identity=TRACKERLOCATION
Add a device to the known_static_addresses
. This is your phone or whatever
bluetooth device you carry with you.
# known_static_addresses
00:11:22:33:44:55 Devicename
Adjust the behaviour_preferences
. This is also described in more detail in the readme.
# behaviour_preferences
# Consider devices as gone after 120 seconds
PREF_BEACON_EXPIRATION=120
# Publish an additional path with a simple home or not_home state
PREF_DEVICE_TRACKER_REPORT=true
# Maybe also filter incoming triggers to the manufacturer of your phone
PREF_PASS_FILTER_MANUFACTURER_ARRIVE="Apple"
Now you can run monitor by executing ./monitor.sh -b
. Alternatively enable and
start the service to make it persistent after restarts:
systemctl enable monitor.service
systemctl start monitor.service
Configuring Home Assistant⌗
In home assistant via the Integrations page, add MQTT as an integration.
Configure it to listen to the mosquitto broker started by monitor by entering the server address (localhost in our example), username and password.
If the integration asks you to automatically scan for entities, you can disable that.
After adding the integration, add the following to your configuration.yaml
:
# configuration.yaml
mqtt:
device_tracker:
- name: "Devicename"
state_topic: "monitor/Trackerlocation/Devicename/device_tracker"
The state topic here consists of the configuration options of monitor:
monitor/
comes from the mqtt_topicpath
option in mqtt_preferences
,
Trackerlocation/
comes from the mqtt_publisher_identity
option in mqtt_preferences
,
Devicename/
comes from the Devicename
in known_static_addresses
and
device_tracker
comes from the PREF_DEVICE_TRACKER_REPORT
option in behaviour_preferences
.
Restart home assistant and you should see the new entity in the settings.
Add the new entity as a device to your home assistant user and you’ve got pretty reliable tracking.
You can always tweak the settings in behaviour_preferences
some more. See the
readme for detailed explanations of all available options.
This configuration also makes it easy to add new tracking devices because they can all report to the same mqtt broker.