Odianosen25 / Monitor-App

Appdaemon App for Andrew's Monitor Presence Detection System
74 stars 9 forks source link

Reading retained messages at Monitor-App start #74

Closed afirix closed 3 years ago

afirix commented 3 years ago

My question is whether it is possible to get retained MQTT events when Appdaemon and Monitor-App start to properly initialize sensors that Monitor-App creates.

To give you some context: I have two Raspberry PIs at my place with monitor running on them. When I leave the house or get back to it, they react fairly quickly (messages reflecting new confidence are typically emitted within 2 minutes after I leave/return). However, if I stay home, there can be a significant gap between consequent monitor messages (I’ve seen at least 5 hours). If I don’t use retained messages in the MQTT topic and start Monitor-App, that means that I might not have accurate state of Monitor-App sensors for the first few hours after the start. Receiving retained messages and using them at the app start to do proper setup of the initial state sounds like a reasonable idea to me, but I cannot figure out how it can be configured or whether it is supported at all.

I’ve tried running appdaemon in debug mode and here are some relevant logs I’ve seen:

2021-02-07 18:08:19.847376 DEBUG MQTT: Message Received: Topic = monitor/living_room/iphone, Payload = b'{"id":"##REDACTED##","confidence":"100","name":"iPhone","manufacturer":"Apple Inc","type":"KNOWN_MAC","retained":"true","timestamp":"Sun Feb 07 2021 17:41:48 GMT-0800 (PST)","version":"0.2.200"}' 2021-02-07 18:08:19.847526 DEBUG AppDaemon: {'namespace': 'mqtt', 'domain': 'mqtt', 'service': 'publish'} 2021-02-07 18:08:19.847675 DEBUG AppDaemon: process_event_callbacks() mqtt {'event_type': 'service_registered', 'data': {'namespace': 'mqtt', 'domain': 'mqtt', 'service': 'publish'}} 2021-02-07 18:08:19.847889 DEBUG AppDaemon: Event type:MQTT_MESSAGE: 2021-02-07 18:08:19.866731 INFO MQTT: MQTT Plugin initialization complete 2021-02-07 18:08:19.870588 DEBUG AppDaemon: Reading /conf/apps/Monitor-App/home_presence_app.yaml 2021-02-07 18:08:19.881759 INFO AppDaemon: App 'home_presence_app' added 2021-02-07 18:08:22.080343 DEBUG home_presence_app: Listening on MQTT Topic monitor 2021-02-07 18:08:22.080684 DEBUG home_presence_app: Calling listen_event for home_presence_app

If I get it right, appdaemon does get the retained messages at startup correctly, but that happens during MQTT plugin initialization that is before the Monitor-App is started and gets its chance to receive those messages.

Is there anything that can be done to actually route retained messages to Monitor-App?

Odianosen25 commented 3 years ago

If I get it right, appdaemon does get the retained messages at startup correctly, but that happens during MQTT plugin initialization that is before the Monitor-App is started and gets its chance to receive those messages.

Yes

Is there anything that can be done to actually route retained messages to Monitor-App?

Normally when the app starts, it should request the present state from the nodes, so it should technically be having the right state as the nodes have it.

I am not sure what you meant here

if I stay home, there can be a significant gap between consequent monitor messages (I’ve seen at least 5 hours).

If I get it right, it should be working as required as it will mean that no one has passed via a door that has requested the scan to run. If this happens, try the following

Now if for any reason its the later, try configuring (if you haven't yet) remote_monitors so the app can attempt to restart the node if such an issue happens again.

Regards

afirix commented 3 years ago

Ok, I think I might have figured it out. My problem was that the monitor process on all nodes was running without '-a' argument, so it was rather untalkative and didn't emit MQTT messages on scan requests. You had that argument mentioned in the documentation, but I failed to realized its importance. Maybe you could consider putting a disclaimer in bold font somewhere, saying that argument is absolutely needed, maybe it'll save someone from my troubles 😂 .

Odianosen25 commented 3 years ago

Ok cool and glad that fixed it. Yeah I will try and do that during next upgrade.

regards