Xiaomi Human / Body / Motion Sensor - Timeout

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

Makes sense, so that’s why I noticed a shorter time out as you did. I’m trying out a Hue motion sensor right now to overcome this problem. But it’s big and expensive…

Hi all,

I have the xiaomi Window & Door sensor as well as the body movement sensor.

They work ok as long as you don’t want to specify a time range in which they should trigger any action.

First you can’t specify a time range, only starting time till midnight, which I don’t want (i.e. I may want to trigger an action overnight so I know if someone entered my home) but even worst, if I choose to set the ‘Effective time period’ this is using China time zone, so I can’t not trigger anything when I want to.

Even weirder is that log is ‘mapped’ to my mobile’s time zone, but results notifications are using China TZ (as well as sensor behaviour).

Do you know how to solve that?

Other devices (like bulbs, plugs or vacuum cleaner) do actually use my mobile’s time zone correctly.

Thanks!

Im just a bit shocked about last post. The gateway send information to :cn:? Well if u wanna stop this u just block the internet access for the gateway … I don’t want that Chinese people know when my robot cleans our home :house_with_garden:

I use my Xiaomi Motion Sensors for time based events. These are all configure in Home Assistant rather than the Xiaomi app though. For example:

- alias: bathroom_night_light_on
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_158d12315e7bg1
    to: 'on'
  condition:
    condition: and
    conditions:
      - condition: time
        after: '23:00:00'
        before: '06:00:00'
      - condition: state
        entity_id: sun.sun
        state: 'below_horizon'
  action:
    service: light.turn_on
    entity_id: light.bathroom_mirror_lights

I hope this helps.

3 Likes

Is there any way to fix the delay of the sensor?
I read trough all thread, but I simly couldnt understand/find a way to do it.
I’m using official component for xiaomi in HASSIO.

Unless anything has changed…

…although you can tweak it so that the maximum turn off time is 1m5s rather than 1m30s.

How can I achieve that? Is it possible to do it with official component?
Better to have ot on 1min than 2.

…I’m not sure however if this will have an affect on performance. Give it a try, see what you find.

I’v seen this, just not shure where to add that code?
Do I have to install appdeamon addon first?

No need to install anything, it’s one of the files that forms Home Assistant. You can find it with this command:

sudo find / -name xiaomi.py

There are a few files with this name, you want the one in the binary_sensor folder. Mine was located here:

/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/binary_sensor/xiaomi.py

I hope this helps.

I will try to figure this out today. I’m using HASS.IO on Pi3, can it be done on that setup?

I’m not sure about HASS.IO I’m afraid. If you can’t find it then perhaps someone more familiar with HASS.IO can help.

Meybe there is a smarter way of using motion sensor? The trouble I’m having is that lights turn off when I’m in the room, and won’t turn on on motion… Is there a way to have lights on as long as motion sensors is in ON position?

1 Like

That shouldn’t happen. Turning off the lights can be bounded to an automation with a timer setting of around 30s - 1 min. That way the lights will turn off approximately in 2 minutes when no motion is detected. The sensor should be available at that moment as the 1 min internal time has already passed and it should respond to motion immediately. Can you post the automations you use to turn the lights off and turning them on. Maybe something is missing.

If it helps, here is a simplified version of the automation I use to turn lights on and off with a Xiami Motion Sensor:

- alias: bathroom_night_light_on
  initial_state: true
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_258d46515e6fe1
    to: 'on'
  action:
    service: light.turn_on
    entity_id: light.bathroom_mirror_lights

- alias: bathroom_night_light_off
  initial_state: true
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_258d46515e6fe1
    to: 'off'
  action:
    service: light.turn_off
    entity_id: light.bathroom_mirror_lights

As you can see there’s not much to it and it works well for me.

1 Like