zewelor / bt-mqtt-gateway

A simple Python script which provides a Bluetooth to MQTT gateway, easily extensible via custom workers. See https://github.com/zewelor/bt-mqtt-gateway/wiki for more information.
MIT License
549 stars 116 forks source link

Limit light level readings from MiFlora sensors #273

Closed shane-kerr closed 9 months ago

shane-kerr commented 10 months ago

Description

I noticed that periodically one of my MiFlora-monitored plants would report a DLI way too high, which is impossible for my plants. This turned out to be because they were getting a single illumination value that was incredibly high. I tracked it down, and it seems to be something like 400 million, which is suspiciously like 2^32 / 10, which is the maximum value from the sensor (there is a division by 10.0 in the code).

It might be that an all-1 reading has some meaning within the protocol that the library does not account for. Or it might be that the sensors have some bug that sometimes returns that value. I am not sure.

The solution in this PR is a hack, which basically just throws out any illumination value that is unrealistically high. I chose 10 times the maximum daylight value, as listed on the Wikipedia:

https://en.wikipedia.org/wiki/Lux

It should have no impact on properly-functioning sensors returning reasonable values.

So far it has prevented any additional weird values, although it has not yet been running two days.

Type of change

Please delete options that are not relevant.

zewelor commented 9 months ago

Thanks !