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

I’m only running one pi zero w. But as I wrote it worked perfect first days and I only live in a small apartment (42 m2) so I think it should be working just fine. Maybe I’m wrong. 4-5m is the furthest my phone gets from the pi. But at night just 3-4 m and almost clear line of sight to the pi.

Thanks so much for your answer :slight_smile:

Have you modified any of the default settings?

yes it is turned on, and actually on the Bluetooth screen. what options exactly should I use, where is that used. I cannot seems to find any config file. Only the service file created automatically when installed.

[Unit]
Description=Monitor Service
After=network.target

[Service]
User=root
ExecStart=/bin/bash /home/osmc/monitor/monitor.sh &
WorkingDirectory=/home/osmc/monitor
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target network.target

1 Like

That may well be your problem. Exit the bluetooth discovery screen. Some phones do not respond to name requests when they themselves are trying to discover other devices.

Additionally, as noted first by @dwalt, if you have never paired your iPhone with any bluetooth device, iOS may not respond to requests at all! If this is the case, try to pair your phone with any other bluetooth device, and it’ll begin working well with monitor.

1 Like

i have actually more than a dozen paired devices with my iPhone (none is currently connected). I just tried with the phone locked, and opened on on the home screen only. no change.

I also tried using monitor.sh -g -b which resulted in some devices discovered including my Apple TV, but not my phone.

1 Like

Interesting. What happens if you run this command via ssh to you pi?

hcitool name [MAC ADDRESS OF IPHONE]

Actually, looking at your log, it doesn’t seem like monitor is properly reading your known_static_addresses file at all. You should see something like:

Starting monitor.sh (v. 0.1.672)...
> preference: delay between scans = 3
> preference: periodic arrive/depart check interval = 15
> preference: periodic arrive interval = 45
> preference: periodic depart interval = 90 
> preference: database refresh interval = 35
> preference: max arrival scan attempts = 3
> preference: max depart scan attempts = 3
> preference: random advertisement expiration = 45
> preference: beacon rssi change required for reporting = 5 
> preference: bluetooth environmental report frequency = 300
> preference: forced departure check interval = 360
> preference: interval until beacon is considered expired = 145
> preference: trigger a departure scan at other nodes below [x] confidence = 25
> preference: preferred HCI device = hci0
> preference: minimum time between the same type of scan = ERROR!
> preference: mqtt scan start/end reporting = false
> mqtt trigger: location/scan/ARRIVE
> mqtt trigger: location/scan/DEPART
> warning: preferences error, using defaults. consider reset to defaults with '-d' flag
> known device: REDACTED:F7 will publish to: location/first floor/REDACTED:F7
> known device: REDACTED:74 will publish to: location/first floor/REDACTED:74

Notice the two last lines. I’d check to make sure that your known_static_addresses file is correctly saved.

it returned nothing.

I agree, it is confusing and can be difficult to understand exactly which options are in place at any one time. I have added in the options manually to the behavior_preferences file.

How it works is this (from examination of the code):

  • Options are set on the command line. They are not additive, so if you use -g first time then -b only one is in operation each time. To have both you need use -b -g.
  • To set options permanently, use the -u option (so -g -b -u). This causes the script to write a new systemctl service file with those options as the exec command (you can check the options in use by doing a sudo systemctl status monitor.service).
  • Remember to stop the service before running Monitor from the command line (although it should kill the current processes).

HTH

I have only one line there, MAC address and a name I selected. should I have the exact device name from the iPhone?

1 Like

I figured out the issue. I have my MAC address missing a : once i fixed the mac address all worked fine and result are reported to the mqtt broker.

Thanks for your help and patience

Are you sure that you have the correct bluetooth address and not another address like the Wi-Fi MAC address? If the command returns nothing, then either your iPhone’s bluetooth is not working/turned off or you have the incorrect address.

The name in the config file is arbitrary. A nickname.

I have not touched anything within behavior_preferences. It worked really great the first days. Seems to be running fine and stable now, but will be interesting to see how it handles this night.

1 Like

A depart trigger would be really helpful here too. Do you have sensors on your door that can indicate when it is opened/closed/locked?

Ah, thats really(!) clever. Since a departure cant happened without the door open. Unfortunately I don’t own any door sensors yet.

Are there any thing else I can do to make it more stable? The solution I came up with was buying a bluetoot beacon I attach to my keyring. Since my pi running monitor is located where I always put my keys when I get home I thought that would make the confidence more stable. Do you guys have any great beacon you would recommend meeting the keyring requirement?
For example would something like this work? https://www.gearbest.com/smart-finder/pp_009587838156.html?wid=1433363

I’d increase expiration delays, number of times to scan, and inter-scan delays first:

#DELAY BETWEEN SCANS OF DEVICES
PREF_INTERSCAN_DELAY=5

...

#MAX RETRY ATTEMPTS FOR DEPART
PREF_DEPART_SCAN_ATTEMPTS=5

...

#DETERMINE NOW OFTEN TO REFRESH DATABASES TO REMOVE EXPIRED DEVICES
PREF_DATABASE_REFRESH_INTERVAL=85

#PERIOD AFTER WHICH A RANDOM BTLE ADVERTISEMENT IS CONSIDERED EXPIRED
PREF_RANDOM_DEVICE_EXPIRATION_INTERVAL=90

If you live in an apt with lots of folks coming and going, unfortunately monitor is going to recognize all of these devices as potential triggers for arrival scans and expiration of all of these devices as depart scans.

A door sensor would eliminate your noise to near zero. Increasing these numbers will decrease the frequency that a depart scan will be triggered.

1 Like

So now I have the setup reporting correctly the presence of the mobile phone in a specific location and the mqtt is receiving the update. I have a big family in the house, and my objective is report accurately (as much as possible) where are the phones around the house to trigger automation in different part of the house like A/C or light.
How can I setup the mqtt topic and sensor in HA to report the actual location for the device? from what I read in this topic, there is a different sensor for each device in each room, which means I will end up with at least 15 sensors if I have 3 PI’s and 5 occupants in the house. which complicate any automation I am trying to make.

I feel I want room-assistant which I tried but did not report the devices correctly, hence I am trying this monitor script, which is much easier to install and worked mostly from the first try.

I started using HA couple of months back and still learning lots of things and I appreciate all the help I am getting from this great community and it would be great if I can figure out if what I want to achieve can be done with this script.

Has anyone successfully detected a Nut mini BLE device with monitor.sh ?

I’ve tried adding the mac address to both known_* files without much luck. I see it in the console output when starting monitor.sh with -b -g, never a confidence above 0 although the device is next to the Pi Zero W.

0.1.672 08:28:50 pm [CMD-SCAN]  (No. 1) E5:B9:B2:08:2B:01 arrival?
0.1.672 08:28:55 pm location/bryggers/E5:B9:B2:08:2B:01
{
        retain: true
        version : 0.1.672
        address : E5:B9:B2:08:2B:01
        confidence : 0
        name : mathias_nut3
        timestamp : Tue Oct 09 2018 20:28:55 GMT+0200 (CEST)
        manufacturer : Unknown
        type : KNOWN_MAC
}

0.1.672 08:32:24 pm [CMD-SCAN]  (No. 2) E5:B9:B2:08:2B:01 arrival?
0.1.672 08:32:30 pm [CMD-NAME]  E5:B9:B2:08:2B:01 Undiscoverable Device Name  Unknown
0.1.672 08:34:35 pm [CMD-SCAN]  (No. 1) E5:B9:B2:08:2B:01 arrival?

0.1.672 08:34:57 pm [CMD-RSSI]  E5:B9:B2:08:2B:01 Undiscoverable Device Name PUBL RSSI: -56 dBm (Slow/No Motion, changed 14)

Any help greatly appreciated :slight_smile:

Since the Nut is a generic beacon, you should use the known_beacons file to record its mac address only. Make sure to also use the -g flag.

1 Like