Xiaomi Mijia bluetooth temperature & humidity sensor compatibility

Yes, it is seems like @pixeldublu using my code.
Let’s try to do something with this …
But first, try to verify in the official MiHome application that your sensors have the latest firmware version. And answer a couple of questions:

  • are these spikes always positive?
  • do these spikes occur only in temperature readings, or in humidity too?
  • is there anything suspicious in the HA logs at this time?

For now, I suggest you try this code, which logs the fact of exceeding 60 degrees and ignores this value further. Please forward these log entries here. I suspect that these spikes are very large, since they are distinct even after averaging, and then, I hope, it will be enough to filter readings that fall outside the range of the sensor’s measurements. Let’s make sure of that.

yea… your gist.
From what i see both temp and humidity have spikes and always positive.
The logs are little bit harder for me as i have lots of errors from other things :slight_smile:

Will give the suggestion a try.

Spikes are always positive, yes. Both in humidity as well as temperature. I don’t see anything suspicious in the logs.

I will give your new code a try. Will post the log when it occurs again. Thanks for your help! Very much appreciated.

@Magalex. I had three temperature spike errors in the log with your new code. Not visible in the graph, so perfect.

2019-09-01 18:01:05 ERROR (SyncWorker_1) [custom_components.mitemp_bt.sensor] Temperature spike: 1587.2
2019-09-01 18:35:42 ERROR (SyncWorker_9) [custom_components.mitemp_bt.sensor] Temperature spike: 1587.2
2019-09-01 20:13:24 ERROR (SyncWorker_10) [custom_components.mitemp_bt.sensor] Temperature spike: 1587.2

I noticed that all spikes errors give the same value, in your old code the spikes in the graph had different values.

Yes, in my previous code spikes a little lower, since averaging with normal data takes place, but in the log we see directly the number itself. OK. Give me some time and I’ll post the updated version.

OK, guys, I posted an updated version of my gist.
Changes:

  1. Looks like I found the source of the division by zero error. It seems that it occurs when during the period between measurements there was no data on temperature or humidity, but there was a battery level only. Fixed.
  2. Added handling of erroneous spikes. Temperature and humidity values ​​that fall outside the measuring range of the sensor are now simply discarded.
  3. Made some useful options at the beginning of the file for convenience. I think the default values ​​are suitable for most cases. Here they are:
CONF_MITEMPBT_ROUNDING = True    # enable/disable rounding of the average of all measurements taken within CONF_MITEMPBT_PERIOD seconds

(I decided to turn off rounding, as I store data in InfluxDB and use further processing. Turning off rounding allows you to maintain the original accuracy, however, some HA widgets do not have an option to limit the number of decimals.)

CONF_MITEMPBT_DECIMALS = 2       # to how many decimal places to round if rounding is enabled
CONF_MITEMPBT_PERIOD = 60        # the period of measurement in seconds during which the sensor readings are collected and transmitted to HA after averaging
CONF_MITEMPBT_TMIN = -9.9        # sensor measurement limits to exclude spikes from the results
CONF_MITEMPBT_TMAX = 60.0        #
CONF_MITEMPBT_HMIN = 0.0         #
CONF_MITEMPBT_HMAX = 99.9        #
CONF_MITEMPBT_LOG_SPIKES = False # put information about each erroneous spike in the HA log

(limits of measurements took from the my sensor specs)

Look like that’s it. Installation instructions here (for those who are not in the know). History of this method with its differences from the original and advantages under the @tsymbaliuk post. Sorry for bad English )

UPD. Apparently, limiting the minimum and maximum does not help from spikes in some sensors. To solve the problem, added the calculation of the median instead of the mean:

CONF_MITEMPBT_USE_MEDIAN = False # use median as sensor output instead of mean (helps with "spiky" sensors).
                                 # please note that both the median and the average in any case are present as the sensor state attributes.

(By default, I decided to leave this option disabled, since when it is turned on, some of the advantages that averaging of measurements gives are lost. However, the owners of “spiky” sensors have no other choice but to turn the median on…)

For those who are interested, I give a comparison of the median (green curves) and the mean on a three-hour graph of temperature and humidity (at about 20 sensor readings per minute):

2 Likes

@Ernst @pixeldublu try the updated code from the post above. I hope all will be ok.

The condition seems to stop spikes so i added one for the humidity also.

Will report back :slight_smile:

Thanks!

2019-09-02%2007_09_04-Home%20Assistant

Weird…

Apparently, it’s really not in vain that the original component used the median instead of the mean… It seems that there is no particular choice for dealing with this… It is not enough to simply discard values ​​outside the range of the sensor. Curious. @Ernst, do you have the same situation?
@pixeldublu, send a photo of the sensor used, if not difficult.

@magalex. Haven’t seen this behavior, but I’m running your latest code for only two hours now. Will let you know tonight (CET).

1 Like

I will rewrite the code for the implementation of the median in addition to the mean, compare, see how it works. The median does not seem to me so revealing in the case of temperature measurement, but it really helps to exclude erroneous values ​​from the measurement results, even if they are not so much different from normal ones.
The question remains, which sensors are prone to this behavior, and can they be distinguished somehow?
My sensors looks like this (works without any problems):

1 Like

I had a similar error a few minutes ago, with the latest code. Also a small spike of about 1.5 degrees lower than expected.

@Ernst @pixeldublu (and all who have sensors give inexplicable spikes)
I updated my previous post by adding a median calculation. Get updated gist and try new code with option CONF_MITEMPBT_USE_MEDIAN changed to = True (at line 43).

Can I ask, how do you connect it to HA ? Via the Xiaomi gateway, or directly ?

Directly. A gateway is not needed if the HA host has bluetooth LE.

1 Like

It seems to work fine now. No more spikes. Thanks for your help! Your component should replace the current built in component in home assistant.

1 Like

LATER EDIT:

Actually looks like its stopping again after a while… 3 hours since no update…

2019-09-03%2019_27_52-Home%20Assistant 2019-09-03%2019_27_45-Home%20Assistant

You need to see if there is a related to mitemp_bt error in the HA log…

Nothing in the logs… for now i restarted the pi3 and all working but will see for how long :slight_smile: