MQTT motion sensors show "detected" despite being offline for hours

I’m trying to implement a simple motion-activated light in my cellar using Zigbee motion sensors and a Sonoff ZBMINI. All devices have been discovered in ZigBee2MQTT, using a Sonoff Zigbee 3.0 USB Dongle Plus as coordinator. This setup works well, also with other sensors (smart sockets, lights, presence, temperature, etc.)

My problem is that both motion sensors I’m trying out frequently drop out of the ZigBee network but continue to show up in Zigbee2MQTT with the state “detected”. I’d like to first tackle the “state” problem before fixing the “dropping out” issue.

I use both a Sonoff SNZB-03 and a TuYa IH012-RT01

The state “detected” seems to be the default state. After restarting Home Assistant, the sensor first shows up as “unknown” and then goes directly to “detected”, despite ZB@MQTT showing that it was “last seen” 17 hours ago…

ZB@MQTT reports the following state, which is probably the last known state from yesterday afternoon:

{
    "battery": 90.5,
    "battery_low": false,
    "last_seen": "2023-03-03T16:29:41+01:00",
    "linkquality": 120,
    "occupancy": true,
    "tamper": false,
    "voltage": 2900
}

However, the sensors are not configured to “retain” their values. The checkbox in the ZB2MQTT device settings is unchecked, and I even set this in the zb2mqtt/configuration.yaml file (plus a few additional trial-and-error options):

  '0x00124b002918e7bb':
    friendly_name: Keller Bewegung Links
    homeassistant: 
      expire_after: 5
    legacy: false
    optimistic: false
    retain: false
    occupancy_timeout: 10

What could cause these offline motion sensors to consistently show up as “occupied” in Home Assistant?

I would try to fix the ‘dropping off’ first, it will probably solve the ‘stuck detected’ too.

I’d like to understand the cause of this behavior first. How can offline motion sensor just stay on, even after manually resetting it and restarting Home Assistant, with all the above settings? That behavior just seems wrong.

The Tuya sensor just sent an update 1h ago, yet it’s state is still regarded as “detected”.

If the root cause is not fixed, it will just happen again.

My guess based on long use of MQTT, but not Z2M, is the states could be defined as retained messages, and/or Z2M is not updating states when Zigbee drops.
https://www.hivemq.com/blog/mqtt-essentials-part-8-retained-messages/

To test this, download a MQTT tool like https://mqtt-explorer.com/ and look at the sensor published states, especially any Last Will and Testament (LWT) topics.

1 Like

Thanks for this insight and the interesting link. I’ve watched the whole MQTT Essentials video series and learned a lot.

When looking at the MQTT data in the Mosquitto broker with MQTT Explorer, the status of the two sensors really was displayed as “retained” and I was able to delete the entry.

IIUC, the publishing client decides on the QoS level and if its messages should be retained. So I’m not sure what impact the ZB2MQTT configuration (e.g. retain: false) can have. My guess is that it’s purely ZB2MQTT internal, so it could retain messages that are not retained in the broker. But if the message is retained in the broker, these will still be used on restarting ZB2MQTT.

So for me, to ignore the wrong state that is retained in the broker, setting retain: false in ZB2MQTT doesn’t do much good.

What I will try now is to manually reset the device state in the broker from the automation. Especially for motion sensors, only state changes are interesting. So I should be able to do the following:

  1. Automation starts (either from a working sensor, or an outdated retained sensor state)
  2. Send an empty MQTT message with a zero-byte payload to the sensor topic to delete any previously retained messages
  3. Automation waits for the timeout and checks if motion is still detected
    • If the motion sensor is active, it will send an update on motion if the timeout is not too short
    • If the motion sensor is inactive, no update will be received and the automation can ignore it

Does this make sense in gerneral?

Additionally, it would be neat to have the LWT to announce "state": "offline", but I until now I was not able to find LWT messages in MQTT Explorer. Is there a way to see the LWT messages currently ready on the broker?

Hi,

I had the same thing with that motion sensor. The thing for me was to replace the sensor away from the wall and other things. It switched from 4 hours of detecting and doing nothing to undetected and after that it worked.

Maybe it saw the wall as movement orso… i dont know.

Mine is working time to time. I thing it has to do with the ranged between my SONOFF stick and the sensor. Haven’t tried to move it yet.

1 Like