Xiaomi body sensor gateway maybe wrong motion detection

Hi @Danielhiversen ,
I’m using home assistant with xiaomi body sensor (the old one) and i saw that sometimes, something goes wrong while detecting the “motion” status. I think the source of the problem is not your code but xiaomi gateway (firmware version: 1.4.1_149.0143) that is sending a “cmd”:“heartbeat” with status “motion”, whenever the motion is not detected (I’m 100% sure of this). There are no packets before with “report” commands…

So I fixed this bug (or strange command) changing the following code of your “PyXiaomiGateway/init.py” row 146

elif cmd == 'report' or cmd == 'heartbeat':

with:

elif cmd == 'report':

I saw this little bug dumping the packets sent from gateway for about 72h and I saw some of these packets:

Aug 21, 2017 15:09:38.365920430 CEST,{“cmd”:“heartbeat”,“model”:“motion”,“sid”:“158XXXXXXXX”,“short_id”:YYYYY,“data”:"{“voltage”:3065,“status”:“motion”}"}

Aug 21, 2017 16:59:40.267295896 CEST,{"cmd":"heartbeat","model":"motion","sid":"158XXXXXXXX","short_id":YYYYY,"data":"{\"voltage\":3065,\"status\":\"motion\"}"}

I think that the trouble coming from gateway not from your code, but I think this can be a fix…but I don’t know if it’s working for all sensors…I tried only with these xiaomi sensors:

  • body_sensor (1st version)
  • weather station (2nd version)
  • door sensor (1st version)

I want to know your opinion about this and I hope it could be useful :slight_smile:

Thanks and you’re doing a very nice work with home assistant and xiaomi :wink:

1 Like

Hi @cesco83

Thanks for your post.
I have commented on the beta thread a issue that might be similar to yours.
Today all sensors went to motion state one by one in a span of about one / two hours.

The Mi home app shows no motion, while HASS still thinks there is motion on all body sensors (old type).

I will see if your modification will solve my issues as well, I just have HASS running for two days now, so I don’t know how often the heartbeat messages are being sent.

Besides that., I don’t know how to “reset” the state, BC all sensors are triggered right now, a restart doesn’t fix the states.

This heartbeat with motion it’s quite random… I don’t know but unfortunately in my case this happened about 3 times in 7/8 days of use… now it’s happened this night at 4.00am but with the code commented, nothing triggered and all is working fine (till now) :wink:
I keep monitoring

Thanks for trying to fix this problem.

I do not have much of these “ghosts”. So if someone can confirm that this will solve the problem, I will add it to the library.

@syssi: Any comments?

:+1: May be limited to the heartbeat of motion events and configurable? I don’t have ghost issues. My bathroom light is driven by the motion sensor without issues.

@Danielhiversen I don’t know if is the latest firmware… but it seems that the xiaomi gateway is getting stuck to “motion” status, because the heartbeat of the body sensor sends “motion status”, then gateway registers it as “motion” and although home assistant asks to read the statuses of the sensor, gateway responds with “motion”…

As I said before, i did this fix only to “fix the gateway” and because I think that is more reasonable to check only “report” commands…
@syssi Yes I think that we can limit the bugfix only to motion sensors, maybe the new motion sensors aren’t affected?

Only a question: in your opinion / experience, are the sensors firmwares upgraded together with the gateway or not? Can be downgraded?

Thanks to all!

As I ‘m on vacation and all motion sensors where stuck at motion, I have asked my housekeeper to power off the gateway. That did not solved the state issue.

The sensor she walked by while restarting the gateway is working fine now.

It appears the states are indeed falsely reported by the gateway and therefore xiaomi should update their motion detection / state saving part of the gateway.

New real motion for those sensors seem to resolve the issue… until their heartbeat packet comes by…

Edit: came across this issue: https://github.com/louisZL/lumi-gateway-local-api/issues/11

I have some of heartbeat messages reporting erroneous statuses everyday (from door sensors too I saw now)… and yes, the gateway recognizes them as report, so if you try to read the last status of the sensor it will give back the erroneous one, until you trigger the sensor

This last firmware is too buggy, I hope Xiaomi fix asap, 'cause I’m using it as real home alarm, not to play with sensors and lights :frowning_face:

Isn’t there a way to rollback to earlier version of the firmware is it?

Thanks guys for your support :wink:

EDIT: the post that you linked is dated April??? will be a forever issue…? :disappointed_relieved:

I was able to reproduce the ghost issue if I block the internet connection of my xiaomi gateway. Half a day later the gateway starts to publish wrong values of the motion sensor. This same issue was fixed for door/window sensors in the past.

I’m currently on vacation so I can’t do much… my sensors are stuck @ motion.
When home, I will preform a long time ping check, to see how my internet connection is preforming and to see if there is some kind of relation between sensors getting stuck and a problem with the internet connection.

In the meantime I suggest everyone will submit a bug report to the mi home developers as this is clearly a bug, the gateway is reporting different states to Xiaomi and local devices.

1 Like

I didn’t tried yet…but this can be a fix sigh :confused: . Why China needs to monitor all of their devices, we don’t ever know :smile:
Actually I blocked the gateway from connecting internet, to avoid any kind of ‘interference’ from MiHome servers and/or updates, I try to unblock it to see if something will change.
Thanks!!

Believe me, even Chinese has no idea. However, for this event, I think it is based on Xiaomi’s own business strategy. They sold production in low price, then they have to take something to even the cost. Well, user’s data is the best option. Furthermore, they take no account of after-sale. They just need to make a new version of devices and sold them.

Yeah yeah I know , I was joking :smile:
Where can we contact MiHome developers?

so…Did Xiaomi offer a forum for international users? There is an official forum for Chinese market including all kinds of Xiaomi production (not only MIUI). And the administration accounts usually belong to Xiaomi staffs. I can help you raise a issue at Chinese forum, but as far as I know, they just reply slowly or ignore it. BTW Mijia’s after-sale in China is awful as well.

Back to your problem, recently one of my friends had the same issue as well. I think his problem is caused by week Zigbee Signal. I suggest you make sensor closer a bit to gateway. However, if yours is caused by server behind Chinese wall, then I guess Xiaomi will not deal with it since the Mijia devices are only sold legally in Chinese market. ( Different from Yeelight, I am not pretty sure that whether Mijia officially exported abroad. ) And they had no way to withdraw that wall as well.

Anyway coming back to code bug fixing, I think that it’s better to discard heartbeat commands not only for bug fixing but as well as for all sensors used to “istant notification” like body sensor, door sensor, maybe smoke sensor too, because they need attention immediately.
For weather station and similar you can accept heartbeat commands with no troubles…
what do you think @Danielhiversen @syssi ?

Thanks again all

I disagree. The heartbeat is an important feature on packet loss / if home assistant misses an event. It’s multicast / udp via WiFi so it’s very likely to miss stuff.

@syssi I’m not so agree with you, because the heartbeat (not the one with token) is sent 1 or 2 times every day so I think is useless, if the heartbeat was every 10seconds like the token, I’m agree with you…

EDIT: first i want to try if reconnecting the GW to the internet will start working…

Good point. The long interval of the heartbeat is new to me. In this case the heartbeat is just useful to detect offline devices.

I’m testing your suggestion right now.

Are you sure about that frequency? Last time I checked it was sent frequently IIRC.

Also, I don’t think the proposed fix will fix it 100% :frowning: My two motion sensors just went into ghost mode (reporting motion, with no motion), with about 10 minutes difference. When HA restarts, the components does a “read” for each sensor, and both are reporting “status: motion”. So, it is not only the heartbeats that are wrong :frowning:

If everyone contacts Xiaomi support, the may fix it. Open the Mi Home app, go to Profile > Help & Feedback > Gateway > Report an Issue and say that the motion sensors are reporting false positives when using the local developer API.