OK, guys, I posted an updated version of my gist.
Changes:
- 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.
- Added handling of erroneous spikes. Temperature and humidity values that fall outside the measuring range of the sensor are now simply discarded.
- 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):