JsBergbau/MiTemperature2

Bouncing temperature compensation

JsBergbau opened this issue · 4 comments

This is an issue with the LYWSD03MMC Sensor. On successive readings/receiving of the temperature the value bounces a lot. Therefore the debouncing switch has been implemented. It works that way:
(We skip the rounding which is only used after startup until the second decimal place is not anymore within 0.2 and 0.7).
Say temperature read is 11.81 degrees. Than it is rounded downwards (floor) to 11.8 If the next reading is 11.86 it is still floored to 11.8
If it reaches 11.7 it rounded up to 11.8
If the next reading is 11.83 it is still rounded up to 11.9
When the next reading is 11.82 it is floored to 11.8
When next reading is 11.85 or 11.86 it is still florred to 11.8

Due to the way debouncing works it can only be used together with --round option. If round option is not specified, but --debounce than debouncing is ignored.

I have multiple BME280 and also DS18B20 sensors. Without any measures they show a lot less bouncing values. They just give a lot more decimal places and I'm using only 2 of them. Giving quite stable values.

LYWSD03MMC gives 17 bounces in that measurement without debouncing measures within about 20 minutes
grafik

LYWSD03MMC with debouncing. Within roughly more than half an hour "only" 6 temperature bounces. The humidity is bouncing, but there is no possibility to debounce without losing precision. Things like moving average can still be down when displaying the data
grafik

--> Debouncing ok

Hello, thanks a lot for all your work on this sensor so far. I've recently been toying with it and I wanted to share my own version of "debouncing" temperature and humidity in case it can be useful to someone.

Based on a given debouncing delay (120s for example), I send the temperature and humidity every 120s but I update each of the value, for temperature and humidity, only if they did not "bounced" back to their old value in the 120s delay.

I've made a dumb implementation through a callback but this could be integrated into the original script as well.

[...] --callback debounceSendToDomoticz.sh
#!/bin/sh
cd "$(dirname "$0")"

DEBOUNCE_DELAY=120
TMP_DIR=tmp_data_$2
if [ ! -d $TMP_DIR ]; then 
        mkdir -p $TMP_DIR
        touch $TMP_DIR/old_temp
        echo 0 > $TMP_DIR/old_temp_date
        touch $TMP_DIR/old_hum
        echo 0 > $TMP_DIR/old_hum_date
        echo 0 > $TMP_DIR/last_send_date
fi

val_temp=$3
val_hum=$4
val_date=$7

old_temp=$(cat $TMP_DIR/old_temp)
if [ "$old_temp" = "$val_temp" ]; then
        echo "Reset temp date"
        echo $val_date > $TMP_DIR/old_temp_date
fi
old_temp_date=$(cat $TMP_DIR/old_temp_date)
if [ $(($val_date - $old_temp_date)) -ge $DEBOUNCE_DELAY ]; then
        echo "Updating temp value"
        old_temp=$val_temp
        echo $val_temp > $TMP_DIR/old_temp
        echo $val_date > $TMP_DIR/old_temp_date
fi

old_hum=$(cat $TMP_DIR/old_hum)
if [ "$old_hum" = "$val_hum" ]; then
        echo "Reset hum date"
        echo $val_date > $TMP_DIR/old_hum_date
fi
old_hum_date=$(cat $TMP_DIR/old_hum_date)
if [ $(($val_date - $old_hum_date)) -ge $DEBOUNCE_DELAY ]; then
        echo "Updating hum value"
        old_hum=$val_hum
        echo $val_hum > $TMP_DIR/old_hum
        echo $val_date > $TMP_DIR/old_hum_date
fi

last_send_date=$(cat $TMP_DIR/last_send_date)
if [ $(($val_date - $last_send_date)) -ge $DEBOUNCE_DELAY ]; then
        echo $val_date > $TMP_DIR/last_send_date
        echo "sending $old_temp/$old_hum"
        ./sendToDomoticz.sh $1 $2 $old_temp $old_hum $5 $6
fi

The advantage for me is that I can keep the two decimal precisions and this work as well with the humidity.

It can still be improved at the bouncing detection part which only checks if we go back to the exact old value: we could also detect if we go from a higher value to a lower value (or the reverse).

(On a side note, calling a python script every 6 seconds, per sensor, was cpu intensive for my rpi so I switched it to a shell script)

Hi Allen57,

thanks for sharing. Suppose we have 20.12 °C, so when after 120 seconds it still has 20.12 °C you don't update the value. If it has 20.13 °C you update the new value, correct?
Sounds interesting. What are your experiences? How good does ist work? I mean it could happen that 20.13 °C is reported back, but then temperature bounces again to 20.12 °C, when I have understood your script.

To be more accurate, if the first value is 20.12 °C, it sends it immediately. Then the next values, every 6 sec, could be something like 20.13 20.13 20.14 20.12 ... (and after 120s) 20,13. During this delay of 120s, no values are sent, but at the end of the delay, it will send updated values only if the values did not go back to their original value (20.12) during the 120s delay.
In this case, even if the last value is 20.13, it will send 20.12 as this value was reported during the 120s delay.

So if the values are, for example, constantly bouncing back between 20.12 and 20.13, this script will only send the same first value until it stops reporting the first value for more than 120s.

About my experience, I will be able to give you actual data comparison (with and without my script) in a few hours ;).

I've started a test with one sensor using debounce and another one not using it, but then I realized I could do my test based on the same value for the same sensor. So here are the current results (domoticz only keeps one sample per 5 minutes):

Result
With debouncing Without debouncing

2020-08-20 14:20:00 | 68 | 25.47
2020-08-20 14:25:00 | 68 | 25.47
2020-08-20 14:30:00 | 68 | 25.47
2020-08-20 14:35:00 | 68 | 25.53
2020-08-20 14:40:00 | 68 | 25.53
2020-08-20 14:45:00 | 68 | 25.53
2020-08-20 14:50:00 | 68 | 25.58
2020-08-20 14:55:00 | 67 | 25.58
2020-08-20 15:00:00 | 67 | 25.64
2020-08-20 15:05:00 | 67 | 25.64
2020-08-20 15:10:00 | 67 | 25.65
2020-08-20 15:15:00 | 67 | 25.65
2020-08-20 15:20:00 | 67 | 25.68
2020-08-20 15:25:00 | 67 | 25.69
2020-08-20 15:30:00 | 67 | 25.74
2020-08-20 15:35:00 | 67 | 25.79
2020-08-20 15:40:00 | 67 | 25.79
2020-08-20 15:45:00 | 67 | 25.82
2020-08-20 15:50:00 | 67 | 25.82
2020-08-20 15:55:00 | 67 | 25.85
2020-08-20 16:00:00 | 66 | 25.85
2020-08-20 16:05:00 | 66 | 25.85
2020-08-20 16:10:00 | 66 | 25.83
2020-08-20 16:15:00 | 65 | 25.79
2020-08-20 16:20:00 | 65 | 25.79
2020-08-20 16:25:00 | 64 | 25.75
2020-08-20 16:30:00 | 64 | 25.75
2020-08-20 16:35:00 | 63 | 25.68
2020-08-20 16:40:00 | 63 | 25.72
2020-08-20 16:45:00 | 63 | 25.72
2020-08-20 16:50:00 | 62 | 25.67
2020-08-20 16:55:00 | 62 | 25.67
2020-08-20 17:00:00 | 62 | 25.67
2020-08-20 17:05:00 | 61 | 25.65
2020-08-20 17:10:00 | 61 | 25.63
2020-08-20 17:15:00 | 61 | 25.63
2020-08-20 17:20:00 | 61 | 25.63
2020-08-20 17:25:00 | 60 | 25.63
2020-08-20 17:30:00 | 60 | 25.63
2020-08-20 17:35:00 | 60 | 25.62
2020-08-20 17:40:00 | 60 | 25.62
2020-08-20 17:45:00 | 59 | 25.64
2020-08-20 17:50:00 | 59 | 25.58
2020-08-20 17:55:00 | 59 | 25.58
2020-08-20 18:00:00 | 59 | 25.58
2020-08-20 18:05:00 | 60 | 25.65
2020-08-20 18:10:00 | 60 | 25.72
2020-08-20 18:15:00 | 61 | 25.72
2020-08-20 18:20:00 | 61 | 25.72
2020-08-20 18:25:00 | 61 | 25.74
2020-08-20 18:30:00 | 61 | 25.74
2020-08-20 18:35:00 | 60 | 25.73
2020-08-20 18:40:00 | 60 | 25.7
2020-08-20 18:45:00 | 60 | 25.7
2020-08-20 18:50:00 | 60 | 25.7
2020-08-20 18:55:00 | 60 | 25.66
2020-08-20 19:00:00 | 60 | 25.65
2020-08-20 19:05:00 | 59 | 25.68
2020-08-20 19:10:00 | 59 | 25.67
2020-08-20 19:15:00 | 59 | 25.67
2020-08-20 19:20:00 | 58 | 25.61
2020-08-20 19:25:00 | 58 | 25.58
2020-08-20 19:30:00 | 58 | 25.57
2020-08-20 19:35:00 | 58 | 25.54
2020-08-20 19:40:00 | 57 | 25.51
2020-08-20 19:45:00 | 57 | 25.51
2020-08-20 19:50:00 | 57 | 25.45
2020-08-20 19:55:00 | 57 | 25.45
2020-08-20 20:00:00 | 57 | 25.45
2020-08-20 20:05:00 | 56 | 25.39
2020-08-20 20:10:00 | 56 | 25.39
2020-08-20 20:15:00 | 56 | 25.37

2020-08-20 14:20:00 | 68 | 25.45
2020-08-20 14:25:00 | 68 | 25.53
2020-08-20 14:30:00 | 68 | 25.51
2020-08-20 14:35:00 | 68 | 25.53
2020-08-20 14:40:00 | 68 | 25.55
2020-08-20 14:45:00 | 67 | 25.55
2020-08-20 14:50:00 | 67 | 25.6
2020-08-20 14:55:00 | 67 | 25.6
2020-08-20 15:00:00 | 67 | 25.67
2020-08-20 15:05:00 | 67 | 25.65
2020-08-20 15:10:00 | 67 | 25.68
2020-08-20 15:15:00 | 67 | 25.72
2020-08-20 15:20:00 | 67 | 25.71
2020-08-20 15:25:00 | 67 | 25.71
2020-08-20 15:30:00 | 67 | 25.75
2020-08-20 15:35:00 | 67 | 25.8
2020-08-20 15:40:00 | 67 | 25.81
2020-08-20 15:45:00 | 67 | 25.81
2020-08-20 15:50:00 | 67 | 25.85
2020-08-20 15:55:00 | 67 | 25.84
2020-08-20 16:00:00 | 66 | 25.82
2020-08-20 16:05:00 | 66 | 25.83
2020-08-20 16:10:00 | 65 | 25.81
2020-08-20 16:15:00 | 65 | 25.76
2020-08-20 16:20:00 | 64 | 25.79
2020-08-20 16:25:00 | 64 | 25.71
2020-08-20 16:30:00 | 63 | 25.67
2020-08-20 16:35:00 | 63 | 25.72
2020-08-20 16:40:00 | 63 | 25.69
2020-08-20 16:45:00 | 62 | 25.69
2020-08-20 16:50:00 | 62 | 25.65
2020-08-20 16:55:00 | 62 | 25.65
2020-08-20 17:00:00 | 61 | 25.62
2020-08-20 17:05:00 | 61 | 25.61
2020-08-20 17:10:00 | 61 | 25.62
2020-08-20 17:15:00 | 60 | 25.61
2020-08-20 17:20:00 | 61 | 25.63
2020-08-20 17:25:00 | 60 | 25.65
2020-08-20 17:30:00 | 60 | 25.62
2020-08-20 17:35:00 | 60 | 25.61
2020-08-20 17:40:00 | 59 | 25.59
2020-08-20 17:45:00 | 59 | 25.57
2020-08-20 17:50:00 | 59 | 25.59
2020-08-20 17:55:00 | 59 | 25.55
2020-08-20 18:00:00 | 60 | 25.61
2020-08-20 18:05:00 | 60 | 25.65
2020-08-20 18:10:00 | 61 | 25.7
2020-08-20 18:15:00 | 61 | 25.72
2020-08-20 18:20:00 | 60 | 25.72
2020-08-20 18:25:00 | 61 | 25.71
2020-08-20 18:30:00 | 61 | 25.69
2020-08-20 18:35:00 | 60 | 25.69
2020-08-20 18:40:00 | 60 | 25.7
2020-08-20 18:45:00 | 60 | 25.67
2020-08-20 18:50:00 | 60 | 25.67
2020-08-20 18:55:00 | 60 | 25.67
2020-08-20 19:00:00 | 60 | 25.72
2020-08-20 19:05:00 | 59 | 25.65
2020-08-20 19:10:00 | 59 | 25.63
2020-08-20 19:15:00 | 58 | 25.63
2020-08-20 19:20:00 | 58 | 25.62
2020-08-20 19:25:00 | 58 | 25.57
2020-08-20 19:30:00 | 57 | 25.53
2020-08-20 19:35:00 | 57 | 25.52
2020-08-20 19:40:00 | 58 | 25.49
2020-08-20 19:45:00 | 57 | 25.48
2020-08-20 19:50:00 | 57 | 25.45
2020-08-20 19:55:00 | 57 | 25.42
2020-08-20 20:00:00 | 56 | 25.41
2020-08-20 20:05:00 | 56 | 25.41
2020-08-20 20:10:00 | 56 | 25.36
2020-08-20 20:15:00 | 56 | 25.37