andrewjfreyer / monitor

Distributed advertisement-based BTLE presence detection reported via mqtt
1.62k stars 193 forks source link

Arrive or depart ? #390

Open pgruetter opened 3 years ago

pgruetter commented 3 years ago

Hi all

Got a question: The batteries on my key tags are empty after about 1 month. Monitor is scanning every 5 minutes, so I'm trying to greatly improve battery times by using Openhab to only scan on request. I have door sensors anyway so I would like to only trigger a scan for the keys let's say 1 minute after the door is opened.

I updated the service file by executing sudo bash monitor.sh -tad -a -b -u

Now I understand a scan will only be triggered when I publish a message on MQTT, perfect. What I don't understand: Do I need to publish on monitor/scan/arrive and monitor/scan/depart at the same time? If I have two keys at home, and one of them is currently present, I don't know if the key at home is leaving, or if the second key is coming home.

Reading the doc, I assume a arrival scan will search for all known beacons not currently present. And a departure scan will only scan for addresses currently present. With that, I need to trigger both, is that correct?

Thanks a lot

pgruetter commented 3 years ago

I think I'm still misunderstanding something. With the configuration above, I now switched off one of the key tags without triggering a MQTT message to arrive or depart. And still, monitor detect that the key tag is missing and reports it. I expected to not get a message until I trigger a scan manually.

RoboMagus commented 3 years ago

Regarding the triggering of the arrival / departure scans, I have a similar setup implemented in my home automation system. I've got an automation setup that performs an arrival scan when one of my doors open and a departure scan that is performed some time after the door closes again.
My idea behind this is that arrivals should be known a.s.a.p., but I only want a departure scan after I've had enough time to get out of the scanning range of my monitor. A departure scan lowers the presence confidence of the device by some amount every scan it does not show up until it reaches 0. Arrival scans pop the confidence to 100% once the device is found present.

pgruetter commented 3 years ago

I've got an automation setup that performs an arrival scan when one of my doors open and a departure scan that is performed some time after the door closes again.

Thanks a lot for the explanation, that makes sense. I'll also implement it like this.

Can you say something about the second question? With the configuration, it seems that scans still happen automatically so my batteries are still empty quite quickly. What do I need to configure so monitor does not scan automatically but ONLY when I trigger it through Openhab ?

RoboMagus commented 3 years ago

I'm not sure about that as I'm not using those options, but based on the arguments you use I would assume that arrival and departure scans are only triggered once the MQTT command is issued. Perhaps you could take a look at the logs to figure out if those commands are issued more often than expected and if so what causes the scans to be issued.

pgruetter commented 3 years ago

Mhh, stupid question: where do I find the logs? Or do I need to start monitor.sh manually to observe the log output?

michaelblight commented 2 years ago

Show the logs with the following command:

sudo journalctl -e -u monitor
pgruetter commented 2 years ago

Show the logs with the following command:

Thanks a lot, now I can check the logs. Not sure how to interpret the result though. When I publish something on MQTT to scan/arrive or scan/depart, nothing happens in the log.

Am I doing this correctly? MQTT is configured with mqtt_topicpath=monitor and mqtt_publisher_identity='pizero', so I publish messages to monitor/pizero/scan/depart (and monitor puts messages on monitor/pizero/<MAC/Name>): image

When I switch on the key tag, I see one entry in the logs: Okt 17 20:33:18 pizero bash[356]: [+] 0.2.200 17-10-2021 08:33:18 [CMD-MQTT] monitor/pizero/my_key { ... confidence : 100 ... }

Now, 30 minutes later, I don't see any other messages regarding my_key.

Should I see another message when I trigger a depart or arrival scan?

michaelblight commented 2 years ago

Your problem looks the same as the question I asked yesterday (which is how I found yours). If I just use "-b", I see beacon messages, however if I also use "-tad", the beacon messages stop. The script is huge, so I struggle to follow it to see whther or not this is expected behaviour - hence the question I posted.

pgruetter commented 2 years ago

Ah, thanks for the hint. I'll watch your issue too then. I'm not sure how actively this project is maintained looking at the last few issues though.. Let's see if someone can help.

RoboMagus commented 2 years ago

When I switch on the key tag, I see one entry in the logs: Okt 17 20:33:18 pizero bash[356]: [+] 0.2.200 17-10-2021 08:33:18 [CMD-MQTT] monitor/pizero/my_key { ... confidence : 100 ... }

Now, 30 minutes later, I don't see any other messages regarding my_key.

Should I see another message when I trigger a depart or arrival scan?

The way that the Monitor script works is that it keeps track of what devices would currently be present, and once it has not detected. Once your device has been found it wil no longer show up in an arrival scan, because Monitor will no longer be searching for it upon an arrival scan. It should show up when performing a departure scan though. But in my opinion it only makes sense scanning for arrivals / departures when there is cause for the device to have left. I agree that it's a shame that we can't just search for all known devices regardless of their presence, just so we can update their state accordingly.

That is btw the reason that when porting the logic of Monitor to the ESP32, I decided to add a monitor/scan/any option which would simply scan for all known devices and update their state accordingly.

Btw out of curiosity, what key tags are you using? I just got my hands on one of them as well and so far I'm having no luck using it with Monitor.

pgruetter commented 2 years ago

The way that the Monitor script works...

Thanks for that explanation, makes sense!

But in my opinion it only makes sense scanning for arrivals / departures when there is cause for the device to have left.

Exactly, that's what I'm trying to do. As I have door sensors, I know when someone comes home or leaves the house. I'm actually fine without a search option for all devices. If I only scan when a door opens, I was hoping to optimize battery usage drastically.

Btw out of curiosity, what key tags are you using? I just got my hands on one of them as well and so far I'm having no luck using it with Monitor.

The original link doesn't work anymore, but I got two of these: https://www.aliexpress.com/item/4000121755351.html?spm=a2g0o.productlist.0.0.4bf0311cFGJqMS

RoboMagus commented 2 years ago

Thanks for that link. Spec wise they look quite similar to what I'm trying to work with.

I've not yet gotten those tag things to work for me since the ones I have don't respond to the name-request's performed by Monitor during the active arrival / departure scans.

They do seem to work quite well with ESPresense though. But I don't really want to have to deal with 2 quite different BT based presence detections in my HA environment, so I kind of put that whole little project on the back burner for now.