Xiaomi Human / Body / Motion Sensor - Timeout

If my Xiaomi motion sensor detects motion the Home Assistant entity stays in a state of “on” for 2 minutes even though there is no motion during that time (e.g. I waive my hand in front of it then point it at a wall for 2 minutes).

Does anyone know if you can change this 2 minute window?

I’m not even sure if it is a Xiaomi thing or a Home Assistant thing?

EDIT: I originally noted a 3 minute windows but after timing it was found to be exactly 2 minutes.

I had the same question:

not mine … mine stays few seconds (need to check exactly)

Interresting… in the top post in the Xiaomi gateway thread there is actually something written about it by rave:

“UPDATE : Feb 6, 2017
Instant motion event trigger. Event will be triggered whenever motion is detected. It does not have a 60 second window. Event is instantly triggered when there’s a motion. Useful for sound notification.”

@anon35356645 can you check how long your motion sensor stays active? Are you running the official component and the latest firmware on your hub?
If I have a 1 minute timeout, Dullage 3 minutes and you a couple of seconds it seems to be random when the no motion information is published by the gateway. Strange because the door/window sensors publish open/close information almost immediately.

I have just timed mine and it’s actually 2 minutes not 3.< This is the time between the sensor showing as ON and OFF in Home Assistant if you momentarily wave your hand in front of it and then leave it alone

I can’t seem to see any where in the MiHome app that says whether or not motion is currently being detected. Only that motion has been detected at X time. Because of this I’m not sure where this 2 minutes comes from. It could be:

  1. The Sensor
  2. The Gateway
  3. Home Assistant

Another point I’m not sure one, during that 2 minutes I wonder what happens if there is motion detected again. Certainly Home Assistant gets to know about it as the sensor stays ON. Presumably the sensor isn’t constantly notifying the gateway that there is motion as that would consume a lot of battery? I might do some tests on this.

The mentioned codebase is not used in the official component. The event trigger for motion is every fast. The information for no_motion needs 60-120 seconds.

The gateway publishes a multicast message after 120 seconds with the “no_motion” information. If you need a more accurate time for no_motion you must poll the sensor. The official component doesn’t poll the motion sensor. Just multicast messages will processed.

Thanks syssi, this makes sense. If there’s nothing I can adjust in the official component then I will work around it other ways. Cheers for the info.

You could port this piece of code

to the official component if you like. :slight_smile: The job could be easy. Just compare:

https://github.com/home-assistant/home-assistant/blob/dev/homeassistant/components/binary_sensor/xiaomi.py#L157-L173

Hmmm… if I read the code again and again… it looks more like a bug. Both implementations supports polling, right? The official component uses the polling mechanism (and frequency) of home assistant. The implementation of @rave (lazcad) uses a async task delayed by 10 seconds. @Danielhiversen can you help here? I will provide a fix if you explain the code in some words.

1 Like

I read the code a second time and changed my opinion. The motion sensor supports polling and uses the scan interval of home assistant. We should try to reduce the scan interval.

Hi syssi, what is the scan interval of Home Assistant? Is it a hard coded value used for lots of sensors? Or something specific to this sensor?

There is a default scan interval which can be changed per plattform.

cp. https://home-assistant.io/docs/configuration/platform_options/

The sub devices / platforms (light, switch, binary, binary_sensor) of the xiaomi component are discovered automatically. That’s why (I guess) the “scan_interval” configuration cannot be applied at configuration.yaml. Just a few lines of code are needed to make the scan interval configurable. May be it’s already possible. I don’t know how. :wink:

Keeping in mind I’m not brilliant with Python

I had a very quick scan of these 2 and can’t see any reference to a hard coded polling interval:

  • homeassistant/components/xiaomi.py
  • homeassistant/components/binary_sensor/xiaomi.py

I wonder if Home Assistant has a default it falls back on, perhaps this is where the 2 minutes comes from?

I will check the code in detail this weekend and report.

1 Like

Some news: If there is motion detected polling of the sensor is enabled.

cp. https://github.com/home-assistant/home-assistant/blob/dev/homeassistant/components/binary_sensor/xiaomi.py#L158

Each following event disables the polling again and re-enables polling if there is motion:

https://github.com/home-assistant/home-assistant/blob/dev/homeassistant/components/binary_sensor/xiaomi.py#L147

It looks like this:

2017-08-05 10:21:33 DEBUG (Thread-12) [homeassistant.components.xiaomi] PUSH >> <Entity Motion Sensor_158d000113e31f: off>: {'status': 'motion'}
2017-08-05 10:21:58 DEBUG (Thread-3) [homeassistant.components.binary_sensor.xiaomi] Updating xiaomi sensor 158d000113e31f by polling. Current state is "on".
2017-08-05 10:21:58 DEBUG (Thread-3) [homeassistant.components.xiaomi] PUSH >> <Entity Motion Sensor_158d000113e31f: on>: {'status': 'motion', 'voltage': 3075}
2017-08-05 10:22:29 DEBUG (Thread-6) [homeassistant.components.binary_sensor.xiaomi] Updating xiaomi sensor 158d000113e31f by polling. Current state is "on".
2017-08-05 10:22:29 DEBUG (Thread-6) [homeassistant.components.xiaomi] PUSH >> <Entity Motion Sensor_158d000113e31f: on>: {'status': 'motion', 'voltage': 3075}
2017-08-05 10:23:00 DEBUG (Thread-12) [homeassistant.components.binary_sensor.xiaomi] Updating xiaomi sensor 158d000113e31f by polling. Current state is "on".
2017-08-05 10:23:00 DEBUG (Thread-12) [homeassistant.components.xiaomi] PUSH >> <Entity Motion Sensor_158d000113e31f: on>: {'status': 'no_motion', 'voltage': 3075}
--> 1m 33s

2017-08-05 10:23:29 DEBUG (Thread-12) [homeassistant.components.xiaomi] PUSH >> <Entity Motion Sensor_158d000113e31f: off>: {'status': 'motion'}
2017-08-05 10:23:31 DEBUG (Thread-9) [homeassistant.components.binary_sensor.xiaomi] Updating xiaomi sensor 158d000113e31f by polling. Current state is "on".
2017-08-05 10:23:31 DEBUG (Thread-9) [homeassistant.components.xiaomi] PUSH >> <Entity Motion Sensor_158d000113e31f: on>: {'status': 'motion', 'voltage': 3075}
2017-08-05 10:24:02 DEBUG (Thread-5) [homeassistant.components.binary_sensor.xiaomi] Updating xiaomi sensor 158d000113e31f by polling. Current state is "on".
2017-08-05 10:24:02 DEBUG (Thread-5) [homeassistant.components.xiaomi] PUSH >> <Entity Motion Sensor_158d000113e31f: on>: {'status': 'motion', 'voltage': 3075}
2017-08-05 10:24:33 DEBUG (Thread-12) [homeassistant.components.binary_sensor.xiaomi] Updating xiaomi sensor 158d000113e31f by polling. Current state is "on".
2017-08-05 10:24:33 DEBUG (Thread-12) [homeassistant.components.xiaomi] PUSH >> <Entity Motion Sensor_158d000113e31f: on>: {'status': 'no_motion', 'voltage': 3075}
--> 1m 4s

The default scan interval is 30 seconds. I assume the resolution of the sensor itself is 60 seconds. I will try to reduce the scan interval to 10 seconds and report again.

The polling interval is inherited from a parent class of the home assistant core.

I have added two lines to the binary_sensor/xiaomi.py:

from datetime import timedelta

SCAN_INTERVAL = timedelta(seconds=5)

The scan interval is 5 seconds now. A quick test revealed the 60 seconds resolution of the sensor:

2017-08-05 10:53:20 DEBUG (Thread-5) [homeassistant.components.xiaomi] PUSH >> <Entity Motion Sensor_158d000113e31f: off>: {'status': 'motion'}
2017-08-05 10:53:21 DEBUG (Thread-7) [homeassistant.components.binary_sensor.xiaomi] Updating xiaomi sensor 158d000113e31f by polling. Current state is True
2017-08-05 10:53:21 DEBUG (Thread-7) [homeassistant.components.xiaomi] PUSH >> <Entity Motion Sensor_158d000113e31f: on>: {'status': 'motion', 'voltage': 3075}
2017-08-05 10:53:28 DEBUG (Thread-8) [homeassistant.components.binary_sensor.xiaomi] Updating xiaomi sensor 158d000113e31f by polling. Current state is True
2017-08-05 10:53:28 DEBUG (Thread-8) [homeassistant.components.xiaomi] PUSH >> <Entity Motion Sensor_158d000113e31f: on>: {'status': 'motion', 'voltage': 3075}
2017-08-05 10:53:33 DEBUG (Thread-8) [homeassistant.components.binary_sensor.xiaomi] Updating xiaomi sensor 158d000113e31f by polling. Current state is True
2017-08-05 10:53:33 DEBUG (Thread-8) [homeassistant.components.xiaomi] PUSH >> <Entity Motion Sensor_158d000113e31f: on>: {'status': 'motion', 'voltage': 3075}
2017-08-05 10:53:39 DEBUG (Thread-3) [homeassistant.components.binary_sensor.xiaomi] Updating xiaomi sensor 158d000113e31f by polling. Current state is True
2017-08-05 10:53:39 DEBUG (Thread-3) [homeassistant.components.xiaomi] PUSH >> <Entity Motion Sensor_158d000113e31f: on>: {'status': 'motion', 'voltage': 3075}
2017-08-05 10:53:46 DEBUG (Thread-3) [homeassistant.components.binary_sensor.xiaomi] Updating xiaomi sensor 158d000113e31f by polling. Current state is True
2017-08-05 10:53:46 DEBUG (Thread-3) [homeassistant.components.xiaomi] PUSH >> <Entity Motion Sensor_158d000113e31f: on>: {'status': 'motion', 'voltage': 3075}
2017-08-05 10:53:51 DEBUG (Thread-9) [homeassistant.components.binary_sensor.xiaomi] Updating xiaomi sensor 158d000113e31f by polling. Current state is True
2017-08-05 10:53:51 DEBUG (Thread-9) [homeassistant.components.xiaomi] PUSH >> <Entity Motion Sensor_158d000113e31f: on>: {'status': 'motion', 'voltage': 3075}
2017-08-05 10:53:57 DEBUG (Thread-9) [homeassistant.components.binary_sensor.xiaomi] Updating xiaomi sensor 158d000113e31f by polling. Current state is True
2017-08-05 10:53:57 DEBUG (Thread-9) [homeassistant.components.xiaomi] PUSH >> <Entity Motion Sensor_158d000113e31f: on>: {'status': 'motion', 'voltage': 3075}
2017-08-05 10:54:03 DEBUG (Thread-7) [homeassistant.components.binary_sensor.xiaomi] Updating xiaomi sensor 158d000113e31f by polling. Current state is True
2017-08-05 10:54:03 DEBUG (Thread-7) [homeassistant.components.xiaomi] PUSH >> <Entity Motion Sensor_158d000113e31f: on>: {'status': 'motion', 'voltage': 3075}
2017-08-05 10:54:10 DEBUG (Thread-6) [homeassistant.components.binary_sensor.xiaomi] Updating xiaomi sensor 158d000113e31f by polling. Current state is True
2017-08-05 10:54:10 DEBUG (Thread-6) [homeassistant.components.xiaomi] PUSH >> <Entity Motion Sensor_158d000113e31f: on>: {'status': 'motion', 'voltage': 3075}
2017-08-05 10:54:15 DEBUG (Thread-6) [homeassistant.components.binary_sensor.xiaomi] Updating xiaomi sensor 158d000113e31f by polling. Current state is True
2017-08-05 10:54:15 DEBUG (Thread-6) [homeassistant.components.xiaomi] PUSH >> <Entity Motion Sensor_158d000113e31f: on>: {'status': 'motion', 'voltage': 3075}
2017-08-05 10:54:20 DEBUG (Thread-11) [homeassistant.components.binary_sensor.xiaomi] Updating xiaomi sensor 158d000113e31f by polling. Current state is True
2017-08-05 10:54:20 DEBUG (Thread-11) [homeassistant.components.xiaomi] PUSH >> <Entity Motion Sensor_158d000113e31f: on>: {'status': 'motion', 'voltage': 3075}
2017-08-05 10:54:26 DEBUG (Thread-2) [homeassistant.components.binary_sensor.xiaomi] Updating xiaomi sensor 158d000113e31f by polling. Current state is True
2017-08-05 10:54:26 DEBUG (Thread-2) [homeassistant.components.xiaomi] PUSH >> <Entity Motion Sensor_158d000113e31f: on>: {'status': 'no_motion', 'voltage': 3075}
--> 1m 6s

2017-08-05 10:54:34 DEBUG (Thread-4) [homeassistant.components.xiaomi] PUSH >> <Entity Motion Sensor_158d000113e31f: off>: {'status': 'motion'}
2017-08-05 10:54:38 DEBUG (Thread-4) [homeassistant.components.binary_sensor.xiaomi] Updating xiaomi sensor 158d000113e31f by polling. Current state is True
2017-08-05 10:54:38 DEBUG (Thread-4) [homeassistant.components.xiaomi] PUSH >> <Entity Motion Sensor_158d000113e31f: on>: {'status': 'motion', 'voltage': 3075}
2017-08-05 10:54:44 DEBUG (Thread-4) [homeassistant.components.binary_sensor.xiaomi] Updating xiaomi sensor 158d000113e31f by polling. Current state is True
2017-08-05 10:54:44 DEBUG (Thread-4) [homeassistant.components.xiaomi] PUSH >> <Entity Motion Sensor_158d000113e31f: on>: {'status': 'motion', 'voltage': 3075}
2017-08-05 10:54:49 DEBUG (Thread-12) [homeassistant.components.binary_sensor.xiaomi] Updating xiaomi sensor 158d000113e31f by polling. Current state is True
2017-08-05 10:54:49 DEBUG (Thread-12) [homeassistant.components.xiaomi] PUSH >> <Entity Motion Sensor_158d000113e31f: on>: {'status': 'motion', 'voltage': 3075}
2017-08-05 10:54:56 DEBUG (Thread-12) [homeassistant.components.binary_sensor.xiaomi] Updating xiaomi sensor 158d000113e31f by polling. Current state is True
2017-08-05 10:54:56 DEBUG (Thread-12) [homeassistant.components.xiaomi] PUSH >> <Entity Motion Sensor_158d000113e31f: on>: {'status': 'motion', 'voltage': 3075}
2017-08-05 10:55:01 DEBUG (Thread-11) [homeassistant.components.binary_sensor.xiaomi] Updating xiaomi sensor 158d000113e31f by polling. Current state is True
2017-08-05 10:55:01 DEBUG (Thread-11) [homeassistant.components.xiaomi] PUSH >> <Entity Motion Sensor_158d000113e31f: on>: {'status': 'motion', 'voltage': 3075}
2017-08-05 10:55:07 DEBUG (Thread-8) [homeassistant.components.binary_sensor.xiaomi] Updating xiaomi sensor 158d000113e31f by polling. Current state is True
2017-08-05 10:55:07 DEBUG (Thread-8) [homeassistant.components.xiaomi] PUSH >> <Entity Motion Sensor_158d000113e31f: on>: {'status': 'motion', 'voltage': 3075}
2017-08-05 10:55:13 DEBUG (Thread-12) [homeassistant.components.binary_sensor.xiaomi] Updating xiaomi sensor 158d000113e31f by polling. Current state is True
2017-08-05 10:55:13 DEBUG (Thread-12) [homeassistant.components.xiaomi] PUSH >> <Entity Motion Sensor_158d000113e31f: on>: {'status': 'motion', 'voltage': 3075}
2017-08-05 10:55:19 DEBUG (Thread-5) [homeassistant.components.binary_sensor.xiaomi] Updating xiaomi sensor 158d000113e31f by polling. Current state is True
2017-08-05 10:55:19 DEBUG (Thread-5) [homeassistant.components.xiaomi] PUSH >> <Entity Motion Sensor_158d000113e31f: on>: {'status': 'motion', 'voltage': 3075}
2017-08-05 10:55:25 DEBUG (Thread-11) [homeassistant.components.binary_sensor.xiaomi] Updating xiaomi sensor 158d000113e31f by polling. Current state is True
2017-08-05 10:55:25 DEBUG (Thread-11) [homeassistant.components.xiaomi] PUSH >> <Entity Motion Sensor_158d000113e31f: on>: {'status': 'motion', 'voltage': 3075}
2017-08-05 10:55:31 DEBUG (Thread-11) [homeassistant.components.binary_sensor.xiaomi] Updating xiaomi sensor 158d000113e31f by polling. Current state is True
2017-08-05 10:55:31 DEBUG (Thread-11) [homeassistant.components.xiaomi] PUSH >> <Entity Motion Sensor_158d000113e31f: on>: {'status': 'motion', 'voltage': 3075}
2017-08-05 10:55:37 DEBUG (Thread-6) [homeassistant.components.binary_sensor.xiaomi] Updating xiaomi sensor 158d000113e31f by polling. Current state is True
2017-08-05 10:55:37 DEBUG (Thread-6) [homeassistant.components.xiaomi] PUSH >> <Entity Motion Sensor_158d000113e31f: on>: {'status': 'no_motion', 'voltage': 3075}
2017-08-05 10:56:33 DEBUG (Thread-6) [homeassistant.components.xiaomi] PUSH >> <Entity Motion Sensor_158d000113e31f: off>: {'no_motion': '120'}
--> ~2m 

The second shot is strange. I triggered the sensor by waving at the floor for a second both times. It should have result in a one minute period of “motion” too.

Conclusion: The scan interval could be reduced but there is not much room for improvement. Any suggestions?

1 Like

Good effort on the investigation syssi.

So, if my understanding is correct…

  1. The sensor itself updates the gateway every 60 seconds.
  2. Home Assistant by default will check the gateway for changes every 30 seconds.

…these 2 timers are likely not in sync and so Home Assistant will see the state change anywhere between 0 and 30 seconds after the sensor changed to no_motion (which itself may be 0-60 seconds from when there is actually no motion in front of the sensor).

So if you happen to leave the field of view of the sensor just before the sensor updates the gateway…and…if Home Assistant happens to poll the gateway just after the sensor updated it…then…you would see the state of the sensor change straight away. Other than that it could be up to 1m 30s after.

As you say, even if you reduce the SCAN_INTERVAL to 5 seconds it will still take up to 1m 5s for the state to change in Home Assistant.

So we’ve probably got pretty much the best we can get :face_with_raised_eyebrow:

1 Like