Help to understand how the Xiaomi motion sensors work

Hello,
I have a bunch of xiaomi/aqara motion sensors all around my house. They are wireless, very cheap and small, they’re fantastic

device

I have them connected through the zigbee2Mqtt integration.
If I recall correctly, this is how they work:

  1. When they detect motion, they report it and they went to sleep for a minute
  2. After a minute they become active again, and the cycle starts

Knowing that,this is how I setup my node-red logic to have lights that turn on automatically and turn off automatically:

  1. Detected motion light turns on and starts a 61 seconds timer
  2. If motion is detected again, the input is sent to the timer and it is extended by additional 61 seconds
  3. After the timer ends, the light turns off

The problem is that in practice this does not works reliably. Some times it works, and I am able to turn the light on as soon as it turns off, and other times it needs a long cooldown period before I can turn it on again.

Previously what I had was as simple as reading the motion state: if it is detected, turn light on, if it reports not detected, turn light off. That somewhat worked, but does not gives you the ability to “extend” the on period, and I don’t like having the feeling that I’m not in control…

I would not be able to help with the Node-Red portion, but the timer in HA could be used. I would only use a timer if the countdown needed to seen via UI. A motion off status for 61 seconds could achieve the same result.

That was my first instinct and my first try: to put a timeout of 61 seconds to allow the sensor to “extend” the time. However, while it worked sometimes, some other times had the opposite effect: the light will turn off and you will have to wait an entire minute to being able to turn it on.
What I thought could be happening was: the timer ends at the same time motion is detected so the timer is not extended and the light is turn off maybe half a second after the message is received.

But now that I’m explaining it out loud (and after some debugging) I think the actual problem may be hassio not reporting all the state transitions? like, if the new value to report (detected motion) is the same as before, it is just ignored?

I’ll try unchecking that!

EDIT: that was from another node that was not working either, the one I was testing had everything unchecked, so my guess does not make sense…

I’m logging out some values using a node-function, and homeassistant is really delaying the state changes, not sure why.
This is how I simply setup the logging mechanism (events are stored on the contex of the function node)

Here are the logs (note that the timestamp is one hour less because is on my local timezone, while the HA timestamps are UTC):

[
    {
        "timestamp": "05:07:45",
        "old": {
            "entity_id": "binary_sensor.motion_2_occupancy",
            "state": "off",
            "last_changed": "2021-01-31T15:51:57.903318+00:00",
            "last_updated": "2021-01-31T15:51:57.903318+00:00",
            "original_state": "off"
        },
        "new": {
            "entity_id": "binary_sensor.motion_2_occupancy",
            "state": "off",
            "last_changed": "2021-01-31T15:51:57.903318+00:00",
            "last_updated": "2021-01-31T16:07:44.983673+00:00",
            "original_state": "off"
        }
    },
    {
        "timestamp": "05:07:45",
        "old": {
            "entity_id": "binary_sensor.motion_2_occupancy",
            "state": "off",
            "last_changed": "2021-01-31T15:51:57.903318+00:00",
            "last_updated": "2021-01-31T16:07:44.983673+00:00",
            "original_state": "off"
        },
        "new": {
            "entity_id": "binary_sensor.motion_2_occupancy",
            "state": "on",
            "last_changed": "2021-01-31T16:07:44.984114+00:00",
            "last_updated": "2021-01-31T16:07:44.984114+00:00",
            "original_state": "on"
        }
    },
    {
        "timestamp": "05:10:17",
        "old": {
            "entity_id": "binary_sensor.motion_2_occupancy",
            "state": "on",
            "last_changed": "2021-01-31T16:07:44.984114+00:00",
            "last_updated": "2021-01-31T16:07:44.984114+00:00",
            "original_state": "on"
        },
        "new": {
            "entity_id": "binary_sensor.motion_2_occupancy",
            "state": "on",
            "last_changed": "2021-01-31T16:07:44.984114+00:00",
            "last_updated": "2021-01-31T16:10:17.491604+00:00",
            "original_state": "on"
        }
    },
    {
        "timestamp": "05:10:17",
        "old": {
            "entity_id": "binary_sensor.motion_2_occupancy",
            "state": "on",
            "last_changed": "2021-01-31T16:07:44.984114+00:00",
            "last_updated": "2021-01-31T16:10:17.491604+00:00",
            "original_state": "on"
        },
        "new": {
            "entity_id": "binary_sensor.motion_2_occupancy",
            "state": "off",
            "last_changed": "2021-01-31T16:10:17.492355+00:00",
            "last_updated": "2021-01-31T16:10:17.492355+00:00",
            "original_state": "off"
        }
    }
]

Looking at the logs, there was a gap of 3 minutes between the first two messages and the last two, where it went from on to off. During that time, there were motion on the WC, but it was just ignored.
Maybe the sensor goes to sleep if it does not detects motion exactly one minute after waking up again? Or maybe the opposite, it does not report anything until something “changes” ? In which case my entire flow is stupid, and my only option is to wait for the sensor to tell me “there is no motion”.

I think I have an Idea of what could be the actual behavior, which I read somewhere else:
I think it is like this:

  1. The first time the sensor detects motion reports it and goes to sleep for one minute.
  2. After that minute of sleep it wakes up, and if it does not detects motion during a minute it reports the “absence of motion”
  3. If during that middle minute it detects motion then it does nothing (no reporting) and goes to sleep again to start from point 2

That will explain why in home assistant 3 minutes had passed: after the first minute since the report, it detected motion and waited another minute before doing anything else (2 minutes) and then, after that last minute waiting for motion (3 minutes) it reports that no motion is detected.

So I will try to listen to activation events and, instead of start a countdown, listen to deactivation events with a little cancelable timeout of 10 seconds to let the sensor “change it’s mind” after reporting no motion