Hi,
I recently posted an app for my motion sensor + lights (App #2: Smart Light)
I somehow come to the conclusion that listening for the “on” state via
self.listen_state(self.motion_detected_callback, motion_entity, new="on")
and resetting a timer (e.g. 2 min) in that callback works most of the time but not always. Especially for the Xiaomit Aqara motion sensor which sends occupancy values in one minute intervals. Here, I noticed that even if there is a lot of motion, the light might go off after some time.
I created two HA automations, one which is triggered by state to “on” and one is triggered by the MQTT message from zigbee2mqtt. Another one I set up in AppDaemon with listen_state() that also writes a log message.
The result was:
2019-12-13 07:05:16 Motion detected by MQTT message (HA automation)
2019-12-13 07:05:17 Motion detected by state change to ON (HA automation)
2019-12-13 07:05:17 Motion detected by state change to on in AppDaemon
2019-12-13 07:06:24 Motion detected by MQTT message (HA automation)
2019-12-13 07:06:24 Motion detected by MQTT message (HA automation)
2019-12-13 07:06:24 Motion detected by state change to on in AppDaemon
2019-12-13 07:07:33 Motion detected by MQTT message (HA automation)
2019-12-13 07:07:33 Motion detected by MQTT message (HA automation)
2019-12-13 07:08:36 Motion detected by MQTT message (HA automation)
2019-12-13 07:08:36 Motion detected by MQTT message (HA automation)
2019-12-13 07:08:36 Motion detected by state change to on in AppDaemon
2019-12-13 07:09:50 Motion detected by MQTT message (HA automation)
2019-12-13 07:09:50 Motion detected by MQTT message (HA automation)
2019-12-13 07:09:50 Motion detected by state change to on in AppDaemon
2019-12-13 07:09:50 Motion detected by state change to on in AppDaemon
2019-12-13 07:10:54 Motion detected by MQTT message (HA automation)
2019-12-13 07:10:54 Motion detected by MQTT message (HA automation)
2019-12-13 07:12:08 Motion detected by MQTT message (HA automation)
2019-12-13 07:12:08 Motion detected by MQTT message (HA automation)
2019-12-13 07:13:38 Motion detected by state change to on in AppDaemon
2019-12-13 07:41:07 Motion detected by MQTT message (HA automation)
2019-12-13 07:41:07 Motion detected by state change to ON (HA automation)
2019-12-13 07:41:07 Motion detected by state change to on in AppDaemon
On the first motion, all of them send a log message (fine!). Then you see there are MQTT messages that come from the sensor and although HA doesn’t trigger the automation AppDaemon does (2019-12-13 07:06:24). But then the MQTT message from 2019-12-13 07:07:33 doesn’t trigger the callback in AppDaemon and this is where the light will go off (because timer is not reset). In addition, it’s very strange that at 2019-12-13 07:13:38 there is a message from AppDaemon without a MQTT message exactly 30 seconds after the last MQTT message.
Do you have any explanation for the additional message? Or why does it call the callback sometimes if there is a state change and sometimes not?
How do you handle those cases? force_update seems to be not recommended. It seems that the only clean way to get updates from the motion sensor is to use MQTT events? (which IMHO is still a bit of a hassle because you need two apps and you have to exchange data between them).