Xiaomi Human / Body / Motion Sensor - Timeout

Hmm, still not working :frowning:
Also, I noticed a light was turned off, triggered by the automation, but HA shows the sensor as being continuously on. So there was a movement directly after the sensor and consequently the light turned off. Detected by the sensor but no automation-triggered turning-on of the corresponding light in HA. There seems to be a dead-time in HA too?

I’ve got the same issues here, maybe once a week the motion sensor will not power on the light of my kitchen or of my entry.

It’s working maybe 100/1 but when the light doesn’t light up it’s really really annoying (specially when it’s my non-geek impatient girlfriend!)

Haven’t looked in details why it behave like this but I’ve noticed that when the light is switched-on and I restart homeassistant, the light will never switch off again.

Maybe that can help resolve the issue…?

I notice since 0.84.x the motion trigger got delay. like 10 second sometimes. For whatever reason, I am not sure. When I check the sensor it says 15 sec ago but the light come on late.

I managed to reduce the issue after noticing my Raspberry Pi 3B+ under volt(red led did not light up) by rebooting the pi.

Another possibility, xiaomi gateway broadcast signal got lost when your wifi unstable(maybe congested or someone using same channel with you. )

Solution for if you restart you HA and the light never off. I made an automation.
if the light on for x minute,
condition motion sensor is off,
turn off the light.
see example below. there is 2 trigger. Either sensor got off for 3 minute (means 5 mins) or light been on for 5 mins (add 10 second so both dont trigger at same time…just wild guess.)

- alias: 'AutoCMBathRoom Off'
  hide_entity: True
  trigger:
    - platform: state
      entity_id: binary_sensor.motion_cm_bathroom
      from: 'on'
      to: 'off'
      for:
        minutes: 3
    - platform: state
      entity_id: switch.wall_switch_cm_bathrm_light
      to: 'on'
      for:
        minutes: 5
        seconds: 10
  condition:
    - condition: state
      entity_id: binary_sensor.motion_cm_bathroom
      state: 'off'
  action:
    - service: switch.turn_off
      entity_id: switch.wall_switch_cm_bathrm_light
1 Like

If you’ve trouble/issues with the motion sensor please mention the version of the gateway you’re using. The motion sensor of the acpartner.v3 (LAN protocol v2) is handled differently as the lumi.gateway (LAN protocol v1).

I been reading the thru motion sensor. Seems my motion sensor still 120 second.

How to narrow down to 65 second. Should I use custom_component with scan_interval

from datetime import timedelta

SCAN_INTERVAL = timedelta(seconds=5)

Correct. Just copy the components/binary_sensor/xiaomi_aqara.py to custom_components/binary_sensor/xiaomi_aqara.py and add your code.

By accident, while trying to understand why my automations related to the xiaomi motion sensor sometimes do not function, I came across this, breaking change in 0.83.3:

I have updated my automations, let’s see what that does.

Hmm, now nothing works.

These bits make no sense to me:

    - service: automation.turn_on
      data:
        entity_id: automation.MOTION_OFF

and

    - service: automation.turn_off
      data:
        entity_id: automation.Motion_off

What is this automation.motion_off?

This is one of my automations, what shall I change to make it work according to the breaking change?

- id: 'xxxxxxxxxx'
  alias: Lamp aan na beweging op overloop
  trigger:
  - entity_id: binary_sensor.motion_sensor_xxxxxxxxxxxx33
    from: 'off'
    platform: state
    to: 'on'
  condition:
  - condition: template
    value_template: '{{ states.sensor.period_of_day.state != "day" }}'
  action:
  - data_template:
      entity_id: '{% if not( 1 < now().hour < 6) %} light.overloop {% else %} light.hal
        {% endif %}'
    service: light.turn_on
- id: 'xxxxxxxxxx'
  alias: Lamp uit na overloop sensor uit
  trigger:
  - entity_id: binary_sensor.motion_sensor_xxxxxxxxxxxx33
    from: 'on'
    platform: state
    to: 'off'
  action:
  - data_template:
      entity_id: '{% if states.light.hal.state == "on" %} light.hal {% else %} light.overloop {% endif %}'
    service: light.turn_off

Nothing. You don’t use “events” here.

Ok, thank you.

Then, something else is wrong. Recently, I see many of these errors:

Error doing job: Task exception was never retrieved

7:54 AM core.py (ERROR)

Fri Jan 04 2019 07:54:49 GMT+0100 (Central European Standard Time)

Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/automation/__init__.py", line 294, in async_trigger
    await self._async_action(self.entity_id, variables, context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/automation/__init__.py", line 378, in action
    await script_obj.async_run(variables, context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 130, in async_run
    await self._handle_action(action, variables, context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 172, in _handle_action
    action, variables, context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/script.py", line 261, in _async_call_service
    context=context
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/service.py", line 81, in async_call_from_config
    domain, service_name, service_data, blocking=blocking, context=context)
  File "/usr/local/lib/python3.6/site-packages/homeassistant/core.py", line 1101, in async_call
    processed_data = handler.schema(service_data)
  File "/usr/local/lib/python3.6/site-packages/voluptuous/schema_builder.py", line 267, in __call__
    return self._compiled([], data)
  File "/usr/local/lib/python3.6/site-packages/voluptuous/schema_builder.py", line 589, in validate_dict
    return base_validate(path, iteritems(data), out)
  File "/usr/local/lib/python3.6/site-packages/voluptuous/schema_builder.py", line 427, in validate_mapping
    raise er.MultipleInvalid(errors)
voluptuous.error.MultipleInvalid: Entity ID  is an invalid entity id for dictionary value @ data['entity_id']

From the timestamps I can see that they are related to the automations triggered by the Xiaomi motion sensors (I have both types with and without illumination sensor).
What I see is that the lights go on but not always, and they again go off, but not always.

These are my exact automations:

- id: '1541182668961'
  alias: Lampen aan bij beweging in hal
  trigger:
  - entity_id: binary_sensor.motion_sensor_xx59
    from: 'off'
    platform: state
    to: 'on'
  condition:
  - condition: numeric_state
    entity_id: sensor.illumination_xxa6
    below: 10
  action:
  - data_template:
      entity_id: '{% if not( 1 < now().hour < 6) %} light.hal {% endif %}'
    service: light.turn_on
  - condition: state
    entity_id: binary_sensor.motion_sensor_xxf6
    state: 'off'
  - data_template:
      entity_id: '{% if (1 < now().hour < 10) %} input_boolean.kamer_lampen_aan_door_hal_sensor
        {% endif %}'
    service: input_boolean.turn_on
  - data_template:
      entity_id: '{% if (1 < now().hour < 10) %} group.tradfri_lr_lights {% endif
        %}'
    service: homeassistant.turn_on
- id: '1541264176539'
  alias: Lamp uit na sensor hal uit
  trigger:
  - entity_id: binary_sensor.motion_sensor_xx59
    from: 'on'
    platform: state
    to: 'off'
  action:
  - data_template:
      entity_id: '{% if states.light.hal.state == "on" %} light.hal {% endif %}'
    service: light.turn_off
  - condition: state
    entity_id: input_boolean.kamer_lampen_aan_door_hal_sensor
    state: 'on'
  - data:
      entity_id: group.tradfri_lr_lights
    service: homeassistant.turn_off
  - data:
      entity_id: input_boolean.kamer_lampen_aan_door_hal_sensor
    service: input_boolean.turn_off
- id: '1542306632487'
  alias: Lamp aan na beweging op overloop
  trigger:
  - entity_id: binary_sensor.motion_sensor_xx33
    from: 'off'
    platform: state
    to: 'on'
  condition:
  - condition: template
    value_template: '{{ states.sensor.period_of_day.state != "day" }}'
  action:
  - data_template:
      entity_id: '{% if not( 1 < now().hour < 6) %} light.overloop {% else %} light.hal
        {% endif %}'
    service: light.turn_on
- id: '1541858493516'
  alias: Lamp uit na overloop sensor uit
  trigger:
  - entity_id: binary_sensor.motion_sensor_xx33
    from: 'on'
    platform: state
    to: 'off'
  action:
  - data_template:
      entity_id: '{% if states.binary_sensor.motion_sensor_xx07.state == "off" and states.light.hal.state == "on" %} light.hal {% elif states.binary_sensor.motion_sensor_xx07.state == "off" %} light.overloop {% endif %}'
    service: light.turn_off
- id: '1544867834009'
  alias: Lamp aan na beweging op zolder
  trigger:
  - entity_id: binary_sensor.motion_sensor_xx07
    from: 'off'
    platform: state
    to: 'on'
  condition:
  - condition: template
    value_template: '{{ states.sensor.period_of_day.state != "day" }}'
  action:
  - data_template:
      entity_id: '{% if not( 1 < now().hour < 4) %} light.overloop {% else %} light.hal
        {% endif %}'
    service: light.turn_on
- id: '1546163155123'
  alias: Lamp uit na zolder sensor uit
  trigger:
  - entity_id: binary_sensor.motion_sensor_xx07
    from: 'on'
    platform: state
    to: 'off'
  action:
  - data_template:
      entity_id: '{% if states.binary_sensor.motion_sensor_xx33.state == "off" and states.light.hal.state == "on" %} light.hal {% elif states.binary_sensor.motion_sensor_xx33.state == "off" %} light.overloop {% endif %}'
    service: light.turn_off

I have been replacing the “now().hour”-parts by “states.sensor.time.state”, also I have been writing the entity_id-parts in other formats, using the > symbol, but nothing seems to help.

Any advice?

I’m not a template guru but I think this will cause problems as it leaves the entity_id empty at cetain times.
{% if not( 1 < now().hour < 6) %} light.hal {% endif %}
You should put time related conditions in the contition part of the automation not in the action.

The same for this part;
{% if states.light.hal.state == "on" %} light.hal {% endif %}'
This will cause an empty entity_id if the light.hal is off which I assume will cause the automation to fail.

Thank you!
I’m not sure this will do anything but I will try this, just as a test to see if there is any effect.
I will report on the effect at a later time.

I will do something, for sure :wink:

If you leave the entity_id empty it will fail when starting HA. Because of the template it doesn’t fail at start but it fails later on because you dynamically remove the entity_id which causes it to crash in a not very elegant way… It’s also what is written in the log, there is an invalid entity_id in the action of the automation.

These are conditions, they must be in the condition part of the automation.

I’m still seeing the “Task exception was never retrieved” - errors after implementing the updates suggested by sjee.
I can pinpoint them to the automations that are triggered by the binary motion sensors that are changing the states from on to off.

The corresponding automations try to switch off a light that is not on. That should be OK, I think?

Please note that the errors also show up when the automations try to switch off a light that is on. And most of the time the actions have succeeded when the error shows up.

Ok, I modified two more entity_id templates, added a condition-part, so now there should always be a valid entity_id,

And… is it solved? If not post your current code.

Yes, I think it is solved, I don’t see the error any more after triggering the sensors multiple times.
Thanks again for your suggestion!
I learned something new about using templates with entity_id’s :slight_smile:

Any reason why this not push in to the ha ?

I just lazy to maintain another breaking change custom_component…

update: no different with Scan Interval
2019-01-05T00:36:20.601748+00:00 binary_sensor.motion_corridor on
2019-01-05T00:38:20.399676+00:00 binary_sensor.motion_corridor off

still 2 minute.

What do you mean? Did you read the complete thread? Did I miss an improvement?