[monitor] Reliable, Multi-User, Distributed Bluetooth Occupancy/Presence Detection

Please excuse what’s probably a very simple beginner question - but I just want to make sure this is what I need before splashing out on a couple of pi zeros. Is this suitable for an HA installation running in VirtualBox on a Mac (and which doesn’t therefore have direct access to Bluetooth)? Am I right in understanding that a separate raspberry pi would report back over wifi, and would be able to detect the presence/absence of iphones and items equipped with ibeacons?

I can’t find a clear answer in the docs, or in the forum - but perhaps that’s because to more experienced readers the answer may be obvious!

1 Like

Yes, that’s indeed the way it works. To give an example:

  • Pi4 with HA installation (supervised install), in my office at home, no monitor.sh
  • PiZero, next to the front door, with monitor.sh
  • PiZero, in the kitchen, with monitor.sh (this is the Pi for my Grocy barcode scanner)
  • PiZero, in the garage, with monitor.sh
  • PiZero, next to the outside car park, with monitor.sh

All these PiZeros report back to HA and are notifying each other to perform a scan under some circumstances.

My recommendation (but that’s really only me!) is, don’t use the BT on your server installation. This one is reserved for my BT devices like thermometers and such. The presence detection (aka monitor.sh) is running best at the “gates” to your appartment/house.

The communication between your monitor.sh instances and HA is btw not WiFi, it runs with MQTT (that is using WiFi, ok, point taken) :smiley:

The only thing I can’t say for sure, how monitor.sh is running with Apple devices. I’m only using Android, but in theory there shouldn’t be any problems. Maybe some things need to be adjusted on the iPhone, so it sends BT data, but that will be configurable. :slight_smile:

Hi Patrick, thank you so much, that’s exactly what I wanted to know. I now need to go and spend some money :smiley:

Have fun! :slight_smile: :+1:

Anyone have connected to MQTT Broker using ssl certificate? In the mqtt_preferences file, what certificate it’s asking? Shouldn’t it be at least 3 separate paths?


# MQTT CERTIFICATE FILE (LEAVE BLANK IF NONE)
mqtt_certificate_path=''

It’s asking for the MQTT server’s public certificate (or your certificate authority’s public certificate). Don’t forget to set:

mqtt_port='8883'

From this 3, I gave it ca.crt

ca.crt
server.crt
server.key

I am getting this error. What is this error saying? Correct certificate, but hostname mismatch/incorrect?

1624092533: OpenSSL Error[0]: error:1417C0C7:SSL routines:tls_process_client_certificate:peer did not return a certificate

Or it is looking for these 2 certificates as well?

client.crt
client.key

I think it would be looking for the cert file specified in the mqtt broker. Are you using the addon? In the configuration on the add-on page you should have a cert file specified. That file should be stored in the ssl folder of your HA install.

You could copy that to /etc/ssl/ on the remote pi, then for the path in mqtt preferences use /etc/ssl/fullchain.pem. I dont have this setup with a cert file and this is just my best guess.

yes, I have the mqtt addon and this is what I have in the config setup.
2021-06-20_5-37-01

On the client side, it usually ask for 3 files like this example taken from MQTT Explorer. The server ca cert is optional and possibly not need since the MQTT Broker is pointed to this same file for signing.
2021-06-20_5-38-10

So, I am confused as what single file mqtt_preference is asking.

I have tried combining all 3 files into a .pem, but I still get the same error message above.

mqtt-client.pem

-----BEGIN RSA PRIVATE KEY-----
<<client.key>>
-----END RSA PRIVATE KEY-----
-----BEGIN CERTIFICATE-----
<<client.crt>>
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
<<ca.crt>>
-----END CERTIFICATE-----

On mine (and this might not be technically correct), the .crt file used in monitor is the same file that my mosquitto server has specified in the line starting “cafile” in mosquitto.conf

I have double checked to make sure the same file pointing at cafile: is the same one I am using on my remote pi which it is. It is still failing to connect although. Wonder if it is a file/owner permission I have to set?
Currently is set to this:

ms1@ms1:/etc/mosquitto/ca_certificates $ ls -la
total 16
drwxr-xr-x 2 root root 4096 Jun 20 00:50 .
drwxr-xr-x 5 root root 4096 Jun 19 07:59 ..
-rw------- 1 ms1 ms1 1432 Jun 20 00:49 mqtt-ca.crt
-rw-r--r-- 1 root root 73 Aug 19 2020 README

By any chance do you have this line set as true in mqtt broker config?

require_certificate: true

Update: I figured it out. Working now. The below needs to be set to false or remove the whole line completely from mqtt broker config.

It makes sense now now that I actually stop and think about what that line really means.

require_certificate: false

MQTT Config:

logins: []
customize:
  active: true
  folder: mosquitto
certfile: mqtt-server.crt
keyfile: mqtt-server.key
require_certificate: false
cafile: mqtt-ca.crt

monitor mqtt_preference file

mqtt_port='8883'
mqtt_certificate_path='/path/to/what/your/mqtt/config/cafile/is/mqtt-ca.crt'
1 Like

Yes - it’s not intuitive. You would think the setting forces all clients to use certificates to verify the identity of the server, but it’s actually forcing the clients to identify themselves to the server using certificates, which is an unusual configuration for MQTT.

1 Like

I have 6 devices (all apple) I am monitoring. The monitor script is working as it should.

Given this scenario. All devices are home and all is showing 100% confidence in HA when I check via developer tool. However, when I open MQTT Explorer, the one that is highlight in red below, always shows confidence: 0 where it should read 100 like the others. What is causing this? I am assuming is qos, but where do I set this for this particular device?
2021-06-21_6-20-34

Monitor setting

monitor.sh -trd -b

Basic question: do you need to be running HA on a separate device for this to work? Or can you install everything on a single RPI0W?

Purchased a Pi Zero W, followed through all of the setup instructions right through to the final Setup Monitor section, when I get this:

git clone git://github.com/andrewjfreyer/monitor
Cloning into 'monitor'...
fatal: unable to connect to github.com:
github.com[0: 81.99.162.48]: errno=Connection timed out

Any ideas, please? I have very liitle experience of Linux command line so I don’t know what to check. The Pi doesn’t have any problems connecting to the internet generally, though. All of the preceding download and install steps worked fine. So nearly there …

Try again in a few minutes/hours. The error message simply tells you, that github.com is not reachable for whatever reasons. :slight_smile:

Another point could be, that git is not installed. You could check (and if not, install it) with
sudo apt-get install git.

Ah thank you, that’s good to know. Git is installed, so I’ll just wait and try again later.

UPDATE: It seems that something on my home network is permanently blocking cloning via http. I got it to work using https instead:

git clone https://github.com/andrewjfreyer/monitor
1 Like

I’m struggling with the -tdr flag. I want to trigger on depart, but scan recursively for arrivals. It is connecting and disconnecting all the time as if its not waiting for the departure trigger. If I change settings to -td it won’t scan for arrivals. I noticed on -h that the -r flag is for both arrival and departure so am I missing something to set it to only scan for arrivals and have a trigger for departures? Thanks.

hi did you resolve this? im having the same issue on a pi zero, when i run monitor.sh -v it works but as soon as I end that it stop working.

regards