Why I didn't have the state updated in HA from my sensors?

Hi

I hope I’m in the right section.

My problem is a little bit weird but very painful with my use case.

I have some xiaomi motion sensors at home.

When I move, the occupancy is changed from “OFF” to “ON”.

But if I move again during the time before it goes “OFF”, it stays in the “ON” state WITHOUT received the message of “ON” state in HA but I can see it in Z2M as MQTT message.

This is a example of what I have at 19:44:00 =>

Zigbee2MQTT:info  2021-09-17 19:44:08: MQTT publish: topic '/motion_desktop', payload '{"battery":100,"illuminance":2,"illuminance_lux":2,"linkquality":63,"occupancy":true,"temperature":27,"voltage":3175}'

Since 19:22 I have the “ON” state as you can see here:

Zigbee2MQTT:info  2021-09-17 19:22:39: MQTT publish: topic 'motion_desktop', payload '{"battery":100,"illuminance":10,"illuminance_lux":10,"linkquality":69,"occupancy":true,"temperature":27,"voltage":3175}'

But in HA I have only the first time where the sensor go from “OFF” to “ON”

At home it is 8:02 PM and I triggered 10 times since 7:22 PM the sensors without any update.

If I wait that the sensor goes in “OFF” state, it will appears in Home Assistant but during the same state, I don’t see any update.

Why ?
Do you have any idea :slight_smile: ?

Thank you a lot,

Regards

The behavior you have observed has been in effect for a long time; that’s the way it (MQTT integration) works.

Oh, I can’t do anything to force to receive the message in HA?

If I saw the mqtt message in Zigbee2mqtt, why I can’t see it in HA?

I’m surprised that we can’t do nothing about it :frowning:

Because the MQTT integration ignores duplicated payload values.

OK I see the problem.

Thank you :slight_smile:

I tried to use homeassistant.entity_update but it’s seems to doesn’t work.

Maybe you have an idea as workaround ? :slight_smile:

A way to force the state of the device at off (fake state). Like that, on the next trigger of the mqtt message the state (real state) will be different and triggered. It’s uggly, I know, but it could works isn’t it?

I have no idea why you are trying to do that but here’s a python_script that allows you to set an entity’s state value:

Just don’t forget that if you use it to change the binary_sensor’s from on to off, no matter how briefly it remains off before it changes back to on, the off state will be recorded in the database and the two state-changes (from on to off and then back to on) will trigger any automations you may have that are listening for state-changes.

I have no idea why you are trying to do that but here’s a python_script that allows you to set an entity’s state value:

my current automation said: each time that I have the “ON” state, I set a timestamp with the timestamp using now() function. And I turn off lights after 30 minutes ( data for the example). The 30 minutes are calculated using the timestamp thanks to the triggered state ( of the motion sensor).
After X(30) minutes without activities / motions, I will turn off lights.

Currently my issue is if I stay 35 minutes in front of my motion sensor. In HA I got 35 minutes the same state “ON” with ONLY 1 message.
In Zigbee2MQTT, I have in reality around of 25 messages send.
So even if I still here, my lights turn OFF after 30 minutes because HA doesn’t trigger again my timestamp…
Is it more understandable ? It’s a tricky issue :slight_smile:

If after to explain my issue you think the python script ( that I saw but I thought it was deprecated since 2019 ), is still available for my use case, I will try to look into it :slight_smile:

Thank you for your help

You can only work with what you have and that means you only have the first off to on state-change and none of the subsequent on messages.

You need to redesign how you determine when to turn the lights off because your idea to force the binary_sensor to off just so it can return to on will cause other issues (explained above).

Yeah I tried to think in the opposite way using the “off” state but I didn’t figure out with a viable solution.
I will try again

Otherwise, as I use the motion sensor only to switch ON / OFF lights, it should not give me weird behaviors on others automations even if it will trigger “on” / “off” fake state.

Anyway, thank you for your help.

I will see if I can redesign my automations for more simple automations :slight_smile:

Edit: Finally it was easy… I don’t know why I was blocked on my ideas :smiley: but I just added a trigger every minute who check the state of the sensor. If it’s “ON”, I set my timestamp. It works as expected.

Thank you @123 :wink:

Why not set an automation to turn the lights off if the motion sensor has been off for 30 mins?

Indeed, it could be an other solution.

It’s “on” when the sensor go from “off” to “on” during motion.
After 1 min (no motion) it will be in “off” state.
After X minutes when the “off” state happens, I turn off lights.
If I move before X minutes, the “for” loop is not triggered so nothing will happens until the next “off” state

I will think about it, if this covers all my cases :slight_smile:

Thanks

You can create an automation that triggers on the mqtt message.

  trigger:
  - platform: mqtt
    topic: zigbee2mqtt/motion_desktop
  condition:
    - condition: template
      value_template: '{{ trigger.payload_json.occupancy == true }}'
  action:

Oh so many solutions! I fully forgot this one…
In more, I used it for my centralite with my alarm the last year.

I need to see what is the best but maybe it will be this one :slight_smile:

Thank you @francisp

The dwell time on the Xiaomi sensors is a pain. They take too long to send a ‘no motion’ signal so any automation you want to create for say ‘lights off after x time’ needs to have a value of ‘x’ greater than the dwell time, which I think is about 2 or 3 minutes (I’d have to check). This is the reason I didn’t buy any more of them after the first one I got.

There is a firmware hack that can be done to shorten the time but it’s fairly involved.

Hi

@sparkydave, yes I know about the lack of time to send messages only each 60s.
I saw a possibility to cut something on the motherboard to have a motion sensors which send events every 5 seconds.

But in my thread here. It’s not really my problem :grin:. It’s just by default, HA doesn’t trigger again his own sensor because my sensor (xiaomi) send the same state of occupancy :wink:. So as long as the state will be the same, HA will not fire new events.

However, thank you for your answer :slight_smile:

For the particular use I put them to, it hasn’t been a problem. I turn lights on in the hallway when I stagger to the bathroom at night. 2 minutes is an OK time. If they have gone out while I am in the bathroom, they go on again as I stagger back. If they stay on for two minutes after I have fallen back into bed, no problem - there is a door that keeps the light out of the bedroom.

If you write your automation correctly this won’t be a problem. You just need to have the automation off delay longer than the sensor dwell time so that the sensor has a chance to re-trigger before the automation delay ends.

Similar here, I have one in a walk in wardrobe.

In reality my other light control automations have time-outs longer than a few minutes anyway so it wouldn’t be that bad except that my light control automations are also light level based so I prefer multisensors.

Having said that the wardrobe light automation is the only one that gives me trouble with not re-triggering on extended motion so I still think the sensor is to blame there (I’ll should really extend the delay time).

Just one question @francisp @123 @nickrout , if I want to do that but from more than one trigger, how can I realize it without create one automate for one device ?

Example of what I imagine, but I’m pretty sure it’s not possiblr like that:

- alias: "Time Of The Last Motion Livingroom"
  trigger:
    - platform: mqtt
      topic: zigbee2mqtt/motion_sofa_livingroom
    - platform: mqtt
      topic: zigbee2mqtt/motion_window_livingroom
  condition:
   - condition: or
     conditions:
       - condition: template
         value_template: "{{ true ==  trigger.motion_sofa_livingroom.payload_json.occupancy }}"
       - condition: template
         value_template: "{{ true ==  trigger.motion_window_livingroom.payload_json.occupancy }}"
  action:
    - service: input_number.set_value
      data_template:
        entity_id: input_number.last_move_record_livingroom
        value: "{{ as_timestamp(now()) }}"

I believe this is called a “blind period” and is present in all motion sensors, in my experience the Xiaomi ones have a medium blind of about 1.5 - 2 minutes, which is ok, for example Ikea has 3 minutes and that is indeed annoying sometimes.

But this is ok, I would not want the lights to repeatedly turn on and off while I’m still in the room. So all my lights turn off sometime (5 minutes) after the sensor registers no motion.

Using a re-trigger prevents them doing this anyway. I have my ZWave motion sensors set to a 2 second period but the automation keeps retriggering as long as there is motion and the lights only turn off after the desired setting.