So…Bluetooth through docker is an unreliable nightmare! It works…until it doesn’t.
All is not lost - a silver lining of moving hassio to docker under ubuntu is that I had a “spare” pi3.
Roll forward a week and I have disabled the bluetooth device_tracker platform under hassio and instead implemented a single-node monitor instance (courtesy of @andrewjfreyer - what a fabulous solution this is! Truly!), which has been faultless since install.
That looooong thread is here: [monitor] Reliable, Multi-User, Distributed Bluetooth Occupancy/Presence Detection
For reference, my raspberry pi hostname is: home How original.
Again, a steep learning curve, but once the basics are in hand like:
- a naming convention
- starting monitor with the -x option (makes published topics persistent - retain flag set in mqtt terms)
- a single automation (now my only automation within hassio) to restart monitor when hassio restarts
How I achieved the above:
I went with names_devicetype e.g. rowlands_iphone, rowlands_ipad etc. 'cos I have a simple setup for now. I’m literally interested in family phones as they come and go.
Edit the systemd service config for monitor with:
sudo nano /etc/systemd/system/monitor.service
My monitor service now looks like this, the only change being the “-x”:
[Unit]
Description=Monitor Service
After=network.target
[Service]
User=root
ExecStart=/bin/bash /home/pi/monitor/monitor.sh -x &
WorkingDirectory=/home/pi/monitor
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target network.target
My hassio automation looks like this:
- id: '1571175825529'
alias: monitor restart
trigger:
- event: start
platform: homeassistant
condition: []
action:
- data:
topic: monitor/scan/restart
service: mqtt.publish
For reference, my mqtt_preferences file looks like this:
# ---------------------------
#
# MOSQUITTO PREFERENCES
#
# ---------------------------
# IP ADDRESS OR HOSTNAME OF MQTT BROKER
mqtt_address=yourhostname.yourdomainname
# MQTT BROKER USERNAME
mqtt_user=yournewuser
# MQTT BROKER PASSWORD
mqtt_password=yournewpassword
# MQTT PUBLISH TOPIC ROOT
mqtt_topicpath=monitor
# PUBLISHER IDENTITY
mqtt_publisher_identity=''
# MQTT PORT
mqtt_port='1883'
# MQTT CERTIFICATE FILE
mqtt_certificate_path=''
#MQTT VERSION (EXAMPLE: 'mqttv311')
mqtt_version=''
I have migrated from the mqtt addon within hassio through mqtt running on my new monitor node… now to it’s final resting place on another docker container to provide separation.
I followed this tutorial to setup the container and create my new username: installing mosquitto in docker
The only addition to behaviour_preferences is the enablement of the device_tracker reporting at the bottom which adds a device_tracker mqtt sub-topic for each device in your known_xxxx_addresses files (for example: monitor/home/rowlands_iphone/device_tracker) which has the value “home” or “not_home”. Very handy:
# ---------------------------
#
# BEHAVIOR PREFERENCES
#
# ---------------------------
#MAX RETRY ATTEMPTS FOR ARRIVAL
PREF_ARRIVAL_SCAN_ATTEMPTS=1
#MAX RETRY ATTEMPTS FOR DEPART
PREF_DEPART_SCAN_ATTEMPTS=2
#SECONDS UNTIL A BEACON IS CONSIDERED EXPIRED
PREF_BEACON_EXPIRATION=240
#MINIMUM TIME (IN SECONDS) BEWTEEN THE SAME TYPE OF SCAN (ARRIVE SCAN, DEPART SCAN)
PREF_MINIMUM_TIME_BETWEEN_SCANS=15
#ARRIVE TRIGGER FILTER(S)
PREF_PASS_FILTER_ADV_FLAGS_ARRIVE=".*"
PREF_PASS_FILTER_MANUFACTURER_ARRIVE=".*"
#ARRIVE TRIGGER NEGATIVE FILTER(S)
PREF_FAIL_FILTER_ADV_FLAGS_ARRIVE="NONE"
PREF_FAIL_FILTER_MANUFACTURER_ARRIVE="NONE"
# If true, this value will cause monitor to report a 'home' or 'not_home' message to
# /device_tracker conforming to device_tracker mqtt protocol
PREF_DEVICE_TRACKER_REPORT="true"
I wanted to keep things simple, so within hassio, I have simply configured my device trackers as follows:
# Device Tracker
device_tracker:
- platform: mqtt
devices:
rowlands_iphone_mqtt: 'monitor/home/rowlands_iphone/device_tracker'
rowlands_ipad_mqtt: 'monitor/home/rowlands_ipad/device_tracker'
rowlands_ipod_mqtt: 'monitor/home/rowlands_ipod/device_tracker'
sandras_iphone_mqtt: 'monitor/home/sandras_iphone/device_tracker'
sandras_ipad_mqtt: 'monitor/home/sandras_ipad/device_tracker'
megans_iphone_mqtt: 'monitor/home/megans_iphone/device_tracker'
kyles_ipad_mqtt: 'monitor/home/kyles_ipad/device_tracker'
This results in a simple device_tracker entity for each device that I have added to the relevant hassio “person”.
If you want to use the mqtt sensors and get at the confidence level etc, you can also do what I did initially, which is to add the relevant sensors to sensors.yaml. Most (if not all of this is covered off in the readme or within the original post/replies in the user community:
# mqqt sensors that enable the monitor platform running on a separate raspberry pi to
# communicate "presence confidence" values to homeassistant
- platform: mqtt
state_topic: 'monitor/home/status'
name: 'Home Monitor Instance Status'# overall health of the monitor service
force_update: true
- platform: mqtt
state_topic: 'monitor/home/rowlands_iphone'
value_template: '{{ value_json.confidence }}'
unit_of_measurement: '%'
name: 'Rowlands iPhone MQTT'
- platform: mqtt
state_topic: 'monitor/home/rowlands_ipad'
value_template: '{{ value_json.confidence }}'
unit_of_measurement: '%'
name: 'Rowlands iPad MQTT'
- platform: mqtt
state_topic: 'monitor/home/rowlands_ipod'
value_template: '{{ value_json.confidence }}'
unit_of_measurement: '%'
name: 'Rowlands iPod MQTT'
- platform: mqtt
state_topic: 'monitor/home/sandras_iphone'
value_template: '{{ value_json.confidence }}'
unit_of_measurement: '%'
name: 'Sandras iPhone MQTT'
- platform: mqtt
state_topic: 'monitor/home/sandras_ipad'
value_template: '{{ value_json.confidence }}'
unit_of_measurement: '%'
name: 'Sandras iPad MQTT'
- platform: mqtt
state_topic: 'monitor/home/megans_iphone'
value_template: '{{ value_json.confidence }}'
unit_of_measurement: '%'
name: 'Megans iPhone MQTT'
- platform: mqtt
state_topic: 'monitor/home/kyles_ipad'
value_template: '{{ value_json.confidence }}'
unit_of_measurement: '%'
name: 'Kyles iPad MQTT'
- platform: min_max
name: "Home Occupancy Confidence"
type: max
round_digits: 0
entity_ids:
- sensor.rowlands_iphone_mqtt
- sensor.rowlands_ipad_mqtt
- sensor.rowlands_ipod_mqtt
- sensor.sandras_iphone_mqtt
- sensor.sandras_ipad_mqtt
- sensor.megans_iphone_mqtt
- sensor.kyles_ipad_mqtt
This has been stable for four days. If anything changes, I’ll update my own thread
bikefright.