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

:joy: the MacDonalds example was pretty dope. But sometimes its not about the cash, but maintenance and in the long run sustainability. I manage just two and I am already :persevere: (though might be cos I am lazy :smirk:). Anyway what about if you have more users, will each person get their own Pi? Or you move to a bigger house that needs more than one, so each person gets two or more? It kind of gets out of hand at this rate. Then you have the issue of unecessary flooding your environment with Bluetooth scans and potentially affecting other appliances.

In terms of ā€œbluetooth perfectionā€ as you put it, I believe it can be achieved within the present setup. Arrival scan takes like less than 3 secs per user. And if anything faster is needed, it means both users are together and it will scan both. So there is no issue there. If it is depart scan, if working on a user, and the other user is also out, since its depart it doesnā€™t matter much really as in the above, if there are both together, then it will declare both at the same time.

Lastly there are instances where a user steps out, its scanning depart and around that time another comes in. To overcome this, my system scans for when the door is opened/closed. And if monitor is busy at that time, it will wait till its not busy and instruct a scan immediately again. So if I was to step out, and it starts its depart scan which takes time, and my wife pops in via the door, it will check if busy and wait for it to complete. Once complete, it will send an instruction to start arrive scan immediately. So at every point in time, no scan is wasted and its very quick.

This can of course begin to slow down as my family grows, but for now we just two, so its perfectly fine. One feature @andrewjfreyer could possible add, is the ability to instruct a particular monitor system to scan, instead of telling them all. So this way, the script running it all becomes more intelligent and optimises the system more.

I am not a fan of spending more if it can be avoided, even if its just for a MacDonalds (but really I rather eat the food :sweat_smile:). Hope this helps.

Regards

2 Likes

Well not sure really, as just running it in default is great since you donā€™t have any special needs.

The only difference between my setup and the default is that I increased my scan interval to 120 sec for both Arrive and Depart, and that was simply cos I have a script that manages it partially, so I had no need for too many scans.

Regards

1 Like

I used the -r flag during configuration to see if it works

lol glad you liked the Mcdonalds reference lol. It sounds like you arenā€™t having HASS unlock your doorā€¦Is that correct?

if monitor is busy trying to do a depart scan with my phone and the other phone comes in during that scan the presence of the incoming phone would not be detected. If there was a completely separate PIs doing the scans than the other device should be detected.

Hmmm good point about not using HASS to unlock my door. But that said, a question for you, how do you then instruct the system to carry out a scan? Does one step on a mat or proximity sensor or something?

Also @andrewjfreyer based on the challenge @benjimatt having, will it be possible to send a message to stop a scan while itā€™s ongoing? For example itā€™s carryinf out a depart scan, someone arrives and since arrival more important, it can stop the depart and carry out the arrival scan?

Regards

1 Like

@andrewjfreyer is there a reason this has to run as root (via sudo). Or does it just need sudo for the first run?

@nickrout hcitool requires root for lescan, so yes root is required.

I would say that it is a poor assumption that arrival is more important honestly. The scenario im talking about isnt one that happens a lot but for my situation it happens at least once a week. Since it happens to me and not her its not as bad :rofl::sweat_smile:

I think separate scanning from different PIā€™s would be a quick fix and not require much more effort if any. 2 Piā€™s Per device. Obviously this sounds like a waste but in theory it would be only 10 dollars per device (2 piā€™s)ā€¦ This would not be an ideal option for everyone.

Other option could be (this is me not knowing python at all) If we can trigger a depart scan and also clear the ā€œCurrently Homeā€ Devices. Or if @andrewjfreyer could just add another trigger that can clear ā€œCurrently Homeā€ devices. This way I could say door opens, clear currently home devices, wait 20 seconds, trigger arrival scan which would essentially scan for both devices.

third option would be a little harder having all devices talk to each other. one device used for depart scanning and the other device used for arrival scanning and the third device would be essentially managing what happens with both of these devices. (this would take a lot more work and would require a min of 3 PIā€™s)

Either way im just happy to have something that works for me about 85% of the time. IMHO Monitor is probably the best presence detection that I have tried. I hope nobody thinks that I am complaining. I love the work that has gone into this.

Iā€™ll think on it. Probably not, since Iā€™m forking these processes, but Iā€™ll see if thatā€™s workable somehow.

I think the solution is to enqueue arrive scans while depart scans are processing, instead of rejecting them.

1 Like

I only said Arrival was more important because it takes way less time to complete its process, and since Depart is usually ran after a delay, stopping it to run an Arrival before running Depart again makes sense.

I really donā€™t get the idea of door opening, clearing devices at home and then scanning for arrival after 20 secs. But if that works for you, no worries bro. As Andrew said, itā€™s best to enqueue the scan as thatā€™s what I do. Just try that and see if it improves your experience.

Regards

1 Like

maybe im missing something but how can I enqueue an arrival scan? I didnt think it was possible to do thisā€¦ If this can already done please educate me :slightly_smiling_face:

3 Likes

I am struggling with how to trigger a scan via mqtt also

It would appear you post to the mqtt topic [topic_path]/scan/arrive (or depart). But I am not getting a result if I publish to this topic.

Not sure if it is expecting a payload.

I tried both an empty post and {ā€œidentityā€:ā€œmonitorā€} as payloads ā€¦ monitor is the name of my mqtt publisher identity ā€¦ and the default I think

1 Like

As I understand it you post the message depart or arrival to the [topic_path]/scan topic. eg

mosquitto_pub -t topic_path/scan -m depart
1 Like

Thanks @nickrout

did not work for me (at least did not seem to trigger a departure/arrival scan).

Was monitoring the log and mqtt topics when I published.

I am on 0.1.629 (the latest as at now)

Can someone confirm if triggering via mqtt is working for them (was not clear if you actually have it working for you @nickrout) .

I have not set -t option. Which I assumed means you can trigger arrive/depart scans via mqtt but it will also trigger when unknown packets are advertised (ie automagically)

1 Like

@phileep @nickrout

The -t flag causes the monitor script to only respond to trigger messages. No other triggers (random advertisement, periodic, and so on) will cause a scan.

The script, by default, will respond to mqtt messages posted to that topic, regardless the -t argument.

Also note that the ā€œ[topic path]ā€ string in the example and documentation is merely a placeholder reference for the mqtt_topicpath variable you have defined in your mqtt_preferences file. So, if you are using default settings and you want to trigger a scan at your notes, the topic path would be: monitor/scan/arrive or monitor/scan/depart

By default, all of your nodes will respond to either of those messages. If you do not want a node to respond to a particular message, include that nodeā€™s mqtt_publisher_identity in the message. So, with default settings, omitting the ā€œfirst floorā€ node from the scan, the topic path would be: monitor/scan/arrive or monitor/scan/depart and the message would be {"data" : "first floor"}. Sending this message will cause only the ā€œsecond floorā€ and the ā€œthird floorā€ to scan; the first floor will ignore the message.

1 Like

@Odianosen25 is referring to his AppDaemon script. I have to add this to monitor, and Iā€™m working on it.

1 Like

Thanks for the summary (I just saw it on the notify discussion after I posted)

However, it does not seem to be triggering a depart or arrive for me

I have not set a default mqtt_publisher_identity, so it is assuming monitor in my mqtt posts.
I have set my mqtt_topicpath to btmonitor

So, posting to btmonitor/scan/depart with an empty payload and getting no depart activity.

main_log is pretty chatty with generic beacons it is updating constantly ā€¦ although I am not interested in them at the moment and have not intentionally turned on any beacon tracking, nor have any in my beacon list.

My setup is 5 phones in the devices list. No beacons.

Any tips for debug would be appreciated

1 Like

Try this instead as your topic btmonitor/scan/Depart and btmonitor/scan/Arrive.

Regards

1 Like

It sounds like you are running the script and watching for results from Home Assistant. Stop the service, and launch the script from the command line to see debugging information that will let you know whatā€™s going on.