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

Yep OK. So I’m only going to have the one node (for now anyway, we’ll see if another is needed I guess). I’ll have it trigger to scan for departs when the front door lock state changes, but should I just leave it at stock for arrivals (let it just do what it normally would do and not worry about setting it to only trigger on an MQTT message)?

For the automations, I’ll probably just do that all through Node-RED. I already have some stuff set up to send me actionable notifications to unlock the door when I get home (based on bayesian presence at the moment), so all I’ll add in HA is an input_boolean or a device_tracker item for visual reference.

It’s mostly just the arrival scanning I’m not too sure how to handle.

Also while I have you, I do have a Tile attached to my keys, and I’ve set that address in the known_beacons file, but honestly I’m a little unclear about what purpose that beacon serves in Monitor?

Yes, that’s right.

As for the Tile, make sure to use the -b flag. Then monitor can track the tile as another device. I’ll be back online to answer more questions in perhaps an hour.

Sure no problem, appreciate your help, I know you’ve probably been through this 1000 times (and i will try and read through this huge thread I promise).

As for tracking the tile, I suppose in my case it might not really help, as we only have the one car and the one set of keys, so it won’t really give me much valuable information about who’s home when the tile is seen, and though it might indicate that at least one of us is home, maybe we’ve both just gone for a walk and left the keys at home :slight_smile: I won’t worry about the Tile for now I guess.

1 Like

Hi @Hansel, i have only one monitor and it trigger to scan for arrives and departs with some rules (i see similar flow in a old post):

  1. If home is empty (no people) only scan for arrives.

  2. If home is not empty and my wife and me is in house, only scan for departs.

  3. If home is not empty and my wife or me is in house, scan for arrives and then scan for departs.

I’m testing the flow now, maybe it can inspirate to you.

2 Likes

Yup, that’s about right. We also have only one car to track and I also stopped tracking it… haha

A note for you that the Samsung phone may not advertise per the issues I note in the FAQ. Still trying to figure out how to make them work with advertisement-triggered scanning.

Gotcha. It seems to be working OK today at least, as my wife is home and has been walking out of range of the node (I have it in a weird position for testing) and it seems to have been disconnecting and connecting appropriately (though I can’t say for sure it’s correct until I’m home to observe I guess).

Nice, would you mind posting the json for that flow?

Also is that rate limit for 1 msg per min to account for someone opening and closing the door in rapid succession? I didn’t realise that node existed, that’s really handy (I’ve over engineered something to do the same thing but I can get rid of that now) :slight_smile:

This is my version:

And yes, the limit node is for what you said. The multiple delays are in case I stay within radius more than I usually do (taking out the trash before going to work, forgetting something inside). That way I get faster departure detection (I use monitor to turn my alarm system on or off, so the sooner I detect departures, the better).

JSON:

[{"id":"4520fd63.2199b4","type":"tab","label":"Presence detection","disabled":false,"info":""},{"id":"fde19650.17b628","type":"server-state-changed","z":"4520fd63.2199b4","name":"Front door opened","server":"56742937.6b70a8","entityidfilter":"binary_sensor.front_door","entityidfiltertype":"substring","outputinitially":false,"state_type":"str","haltifstate":"on","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"x":130,"y":140,"wires":[["a70b03ff.e37c8"],[]]},{"id":"1f9e9e76.a51662","type":"mqtt out","z":"4520fd63.2199b4","name":"","topic":"monitor/scan/depart","qos":"","retain":"","broker":"d279ce4c.26e5d","x":1160,"y":160,"wires":[]},{"id":"887c4700.f6ff08","type":"api-current-state","z":"4520fd63.2199b4","name":"Home status","server":"56742937.6b70a8","outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":true,"entity_id":"binary_sensor.home_occupied","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":150,"y":240,"wires":[["4ec0af49.0a861"]]},{"id":"d0b22b69.7238e8","type":"mqtt out","z":"4520fd63.2199b4","name":"","topic":"monitor/scan/arrive","qos":"","retain":"","broker":"d279ce4c.26e5d","x":450,"y":460,"wires":[]},{"id":"a70b03ff.e37c8","type":"delay","z":"4520fd63.2199b4","name":"","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"minute","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":true,"x":330,"y":140,"wires":[["887c4700.f6ff08"]]},{"id":"4ec0af49.0a861","type":"switch","z":"4520fd63.2199b4","name":"Check occupancy","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"on","vt":"str"},{"t":"eq","v":"off","vt":"str"}],"checkall":"false","repair":false,"outputs":2,"x":210,"y":340,"wires":[["21fe11d4.be3b0e"],["d0b22b69.7238e8","a243ea8b.697f28","e1433ed3.3db0e","5844d26f.caf27c"]],"outputLabels":["Occupied","Clear"],"icon":"node-red-contrib-home-assistant-websocket/home.png"},{"id":"4bf52c20.771da4","type":"switch","z":"4520fd63.2199b4","name":"switch","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"True","vt":"str"},{"t":"eq","v":"False","vt":"str"}],"checkall":"false","repair":false,"outputs":2,"x":670,"y":280,"wires":[["b57cb185.6742c","ef33428b.b33d","e42cd57c.e1e9f8","ec6f2bce.f503e8"],["530ffe20.27cd","6831ba98.3621a4","97ebb83b.69c7e8","19a50c1b.871204","fc84843e.e059c8","1e93b2cf.4347cd"]],"outputLabels":["Yes","No"]},{"id":"21fe11d4.be3b0e","type":"api-render-template","z":"4520fd63.2199b4","name":"Both@home?","server":"56742937.6b70a8","template":"{{is_state('binary_sensor.areks_presence', 'on') and is_state('binary_sensor.hildr_presence', 'on')}}","x":440,"y":280,"wires":[["4bf52c20.771da4"]]},{"id":"fd96cc53.e014","type":"mqtt out","z":"4520fd63.2199b4","name":"","topic":"monitor/scan/arrive","qos":"","retain":"","broker":"d279ce4c.26e5d","x":1150,"y":380,"wires":[]},{"id":"275ca477.2a226c","type":"mqtt out","z":"4520fd63.2199b4","name":"","topic":"monitor/scan/depart","qos":"","retain":"","broker":"d279ce4c.26e5d","x":1160,"y":480,"wires":[]},{"id":"23940a94.e7e3e6","type":"comment","z":"4520fd63.2199b4","name":"House is clear. Looking for arrivals.","info":"","x":500,"y":420,"wires":[]},{"id":"3d4701de.89b79e","type":"comment","z":"4520fd63.2199b4","name":"House is occupied...","info":"","x":450,"y":240,"wires":[]},{"id":"df1ae3a.ddc732","type":"comment","z":"4520fd63.2199b4","name":"Both at home, looking for departures.","info":"","x":980,"y":60,"wires":[]},{"id":"9c689b40.29aeb8","type":"comment","z":"4520fd63.2199b4","name":"Just one at home. Scanning both arrival and departure.","info":"","x":1040,"y":300,"wires":[]},{"id":"3148efcf.db792","type":"comment","z":"4520fd63.2199b4","name":"...house occupancy is checked","info":"","x":170,"y":200,"wires":[]},{"id":"530ffe20.27cd","type":"delay","z":"4520fd63.2199b4","name":"","pauseType":"delay","timeout":"300","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":910,"y":540,"wires":[["275ca477.2a226c"]]},{"id":"6831ba98.3621a4","type":"delay","z":"4520fd63.2199b4","name":"","pauseType":"delay","timeout":"180","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":910,"y":500,"wires":[["275ca477.2a226c"]]},{"id":"97ebb83b.69c7e8","type":"delay","z":"4520fd63.2199b4","name":"","pauseType":"delay","timeout":"120","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":910,"y":460,"wires":[["275ca477.2a226c"]]},{"id":"19a50c1b.871204","type":"delay","z":"4520fd63.2199b4","name":"","pauseType":"delay","timeout":"90","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":900,"y":420,"wires":[["275ca477.2a226c"]]},{"id":"fc84843e.e059c8","type":"delay","z":"4520fd63.2199b4","name":"","pauseType":"delay","timeout":"15","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":900,"y":380,"wires":[["fd96cc53.e014"]]},{"id":"1e93b2cf.4347cd","type":"mqtt out","z":"4520fd63.2199b4","name":"","topic":"monitor/scan/arrive","qos":"","retain":"","broker":"d279ce4c.26e5d","x":930,"y":340,"wires":[]},{"id":"b57cb185.6742c","type":"delay","z":"4520fd63.2199b4","name":"","pauseType":"delay","timeout":"90","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":900,"y":100,"wires":[["1f9e9e76.a51662"]]},{"id":"ef33428b.b33d","type":"delay","z":"4520fd63.2199b4","name":"","pauseType":"delay","timeout":"120","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":910,"y":140,"wires":[["1f9e9e76.a51662"]]},{"id":"e42cd57c.e1e9f8","type":"delay","z":"4520fd63.2199b4","name":"","pauseType":"delay","timeout":"180","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":910,"y":180,"wires":[["1f9e9e76.a51662"]]},{"id":"ec6f2bce.f503e8","type":"delay","z":"4520fd63.2199b4","name":"","pauseType":"delay","timeout":"300","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":910,"y":220,"wires":[["1f9e9e76.a51662"]]},{"id":"d9f37897.c8ee88","type":"mqtt out","z":"4520fd63.2199b4","name":"","topic":"monitor/scan/arrive","qos":"","retain":"","broker":"d279ce4c.26e5d","x":610,"y":540,"wires":[]},{"id":"a243ea8b.697f28","type":"delay","z":"4520fd63.2199b4","name":"","pauseType":"delay","timeout":"60","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":420,"y":580,"wires":[["d9f37897.c8ee88"]]},{"id":"e1433ed3.3db0e","type":"delay","z":"4520fd63.2199b4","name":"","pauseType":"delay","timeout":"30","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":420,"y":540,"wires":[["d9f37897.c8ee88"]]},{"id":"5844d26f.caf27c","type":"delay","z":"4520fd63.2199b4","name":"","pauseType":"delay","timeout":"10","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":420,"y":500,"wires":[["d9f37897.c8ee88"]]},{"id":"56742937.6b70a8","type":"server","z":"","name":"Home Assistant","legacy":false,"hassio":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true},{"id":"d279ce4c.26e5d","type":"mqtt-broker","z":"","name":"Hassio Broker","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

2 Likes

Nice thanks, presumably you run monitor with just -td ?

1 Like

-tad, actually. I’d love to use -td but sadly none of our phones do random advertisements : (

RIght OK. Am I right in thinking that even if you left it on -td, those manual arrive scans in your flow would still fire and work OK?

I’m just not 100% sure right now if our phones (an iPhone XS and a Galaxy S8) work properly with random advertisements. I think they do, as things seem to work, but I figure I can leave it on -td and worst case those manual arrival scans in the flow will pick it up anyway?

Monitor always performs arrival or departure scans when you use the manual triggers. Using -td or -tad makes it that it ONLY via triggers.

So yes, if you use -td i’ll scan arrivals both random advertisements and via MQTT triggers, but will only scan for departure via MQTT triggers.

1 Like

Yep cool, so no harm leaving it on -td for now, other than potentially increasing interference I guess.

Though if -td is working for me is there any benefit in having those manual arrival scans in the flow I wonder (maybe it’d speed up detection?)

1 Like

If you know for sure both of your devices work, then you can totally skip scanning for arrivals.

1 Like

Great, thanks for your help. Only thing i need to work on now is speed of detection of arrivals. Ideally I’d love it to pick up my phone when I’m walking down the driveway so that I can send myself a message to unlock the door (or do it automatically), but I assume the best way to do that would be moving the Pi closer to the front door? If I did that might need another node out the back I suppose.

Hi @andrewjfreyer, I’m sending monitor/scan/echo through to see if my node is still up, but I’m getting no response and it results in the following in journalctl:

./support/mqtt: line 67: -q: command not found
Use 'mosquitto_pub --help' to see usage.
Error: Unknown option ' '.

That’s the quality of service flag right?

EDIT: Fixed it, sent you a pull request. It was just a rogue space in the wrong spot :smiley:

1 Like

I’m having troubles with Nut mini tag. I’ve paired it with my phone through Nut app (not sure if I had to) and added it to known_beacon_addresses (tried both lowercase and uppercase) with running -b flag and monitor does not recognize it. Also, I’ve tried it with empty known_beacon_addresses and still no success. How do you set up Nut mini with monitor? I’m running 0.2.187.

Do you see it advertise when you review the monitor logs?

How to run logs? Should I just run this command in cd monitor journalctl -u monitor -r?

no need to change the working directory, but yes thats the command. far easier in my view to stop the service and run manually with -b