Magnetic door sensor of Xiaomi stays open sometimes

Hi! I need help because I don’t know why is happening this…

It’s related to de Xiaomi Windows Door sensor, normally when I open and close the door that has the magnetic sensor the warning of opening and closing appears well. But sometimes I open the door, it notifies me that it is open, I close it but it does not notify me, the sensor in HA stays open for an hour, while in the app mihome appears closed. And until it does not restart HA or spend an hour or so it does not return to the correct state.

Door opened 19:27:09 and closed 19:27:16

2019-02-13 19:27:09 DEBUG (Thread-3) [xiaomi_gateway] MCAST (report) << {'cmd': 'report', **
'model': 'magnet', 'sid': '158d0001bf36e8', 'short_id': 51374, 'data': '{"status":"open"}'}
2019-02-13 19:27:09 DEBUG (MainThread) [homeassistant.components.xiaomi_aqara] PUSH >> 
<Entity Door Window Sensor_158d0001bf36e8: off>: {'status': 'open'}
2019-02-13 19:27:16 DEBUG (Thread-3) [xiaomi_gateway] MCAST (report) << {'cmd': 'report', 
'model': 'magnet', 'sid': '158d0001bf36e8', 'short_id': 51374, 'data': '{"status":"open"}'}
2019-02-13 19:27:16 DEBUG (MainThread) [homeassistant.components.xiaomi_aqara] PUSH >> 
<Entity Door Window Sensor_158d0001bf36e8: on>: {'status': 'open'}**
2019-02-13 19:27:17 DEBUG (Thread-3) [xiaomi_gateway] MCAST (report) << {'cmd': 'report', 
'model': 'gateway', 'sid': '7811dcb26140', 'short_id': 0, 'data': '{"rgb":150994943,"illumination":338}'}

And later in logs…

2019-02-13 20:27:12 DEBUG (Thread-3) [xiaomi_gateway] MCAST (heartbeat) &lt;&lt; {'cmd':         
'heartbeat', 'model': 'magnet', 'sid': '158d0001bf36e8', 'short_id': 51374, 'data': 
'{"voltage":3015,"status":"close"}'} 2019-02-13 20:27:12 DEBUG (MainThread) 
[homeassistant.components.xiaomi_aqara] PUSH &gt;&gt; &lt;Entity Door Window 
Sensor_158d0001bf36e8: on&gt;: {'voltage': 3015, 'status': 'close'} 2019-02-13 20:27:13 DEBUG 
(SyncWorker_14) [xiaomi_gateway] _send_cmd &gt;&gt; b'{"cmd": "write", "sid": "7811dcb26140", 
"data": {"rgb": 0, "key": "dfa46018d7f0b137eb4ff8632d8cfd1f"}}' 2019-02-13 20:27:13 DEBUG 
(SyncWorker_14) [xiaomi_gateway] _send_cmd resp &lt;&lt; {'cmd': 'write_ack', 'model': 'gateway', 
'sid': '7811dcb26140', 'short_id': 0, 'data': '{"rgb":0,"illumination":808,"proto_version":"1.1.2"}'} 2019- 
02-13 20:27:13 DEBUG (SyncWorker_14) [xiaomi_gateway] write_ack &lt;&lt; {'cmd': 'write_ack', 
'model': 'gateway', 'sid': '7811dcb26140', 'short_id': 0, 'data': 

And other times it works correctly:

2019-02-13 21:49:19 DEBUG (Thread-3) [xiaomi_gateway] MCAST (report) << {'cmd': 'report', 'model': 'magnet', 'sid': '158d0001bf36e8', 'short_id': 51374, 'data': '{"status":"open"}'}
2019-02-13 21:49:19 DEBUG (MainThread) [homeassistant.components.xiaomi_aqara] PUSH >> <Entity Door Window Sensor_158d0001bf36e8: off>: {'status': 'open'}
2019-02-13 21:49:25 DEBUG (Thread-3) [xiaomi_gateway] MCAST (report) << {'cmd': 'report', 'model': 'magnet', 'sid': '158d0001bf36e8', 'short_id': 51374, 'data': '{"status":"close"}'}
2019-02-13 21:49:25 DEBUG (Thread-3) [xiaomi_gateway] MCAST (report) << {'cmd': 'report', 'model': 'motion', 'sid': '158d0001c2e95e', 'short_id': 29134, 'data': '{"status":"motion"}'}
2019-02-13 21:49:25 DEBUG (MainThread) [homeassistant.components.xiaomi_aqara] PUSH >> <Entity Door Window Sensor_158d0001bf36e8: on>: {'status': 'close'}


Can anybody help me?

Thank you!

I would suggest to investigate wireless connection between Xiaomi gateway and your HA. From what I understand gateway broadcast changes without specified recipient (ha in this case). It means that it doesn’t wait for acknowledgement, hence if connection isn’t great some messages might be missed.

My router is ZTE F680, I’ve raspberry connected trought WIFI because on ethernet don’t receive MULTICAST messages from gateway, The distance between gateway and router is less than 2 meters, and from gateway to sensor less than 3 meters. I’ll check erasing sensor from mihome app and adding it again. Thanks, I will publish my results.

How about connection from router to Raspi ?
From your description it seems that the connection to sensor is not an issue (gateway did receive the event and it was visible in Mi Home app).
While I was using Xiaomi gateway and Raspi I also had missed events and concluded that it was due to missed MULTICAST messages (this seems to be an inherent issue with Xiaomi gateway), however I did not diagnose it deeply.