From 6c0b7230396c9857a009a88fa4b202b0f9af3776 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Wed, 7 Feb 2024 14:15:23 +0100 Subject: [PATCH] bme280*.py: Clamp the humidity results in the range 0-100 %. The sensor may deliver values slightly out of range. Signed-off-by: robert-hh --- bme280_float.py | 5 ++++- bme280_int.py | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/bme280_float.py b/bme280_float.py index ab3f613..951e283 100644 --- a/bme280_float.py +++ b/bme280_float.py @@ -201,7 +201,10 @@ class BME280: (self.dig_H2 / 65536.0 * (1.0 + self.dig_H6 / 67108864.0 * h * (1.0 + self.dig_H3 / 67108864.0 * h)))) humidity = h * (1.0 - self.dig_H1 * h / 524288.0) - # humidity = max(0, min(100, humidity)) + if (humidity < 0): + humidity = 0 + if (humidity > 100): + humidity = 100.0 if result: result[0] = temp diff --git a/bme280_int.py b/bme280_int.py index f98dbbd..84d33de 100644 --- a/bme280_int.py +++ b/bme280_int.py @@ -207,6 +207,10 @@ class BME280: h = 0 if h < 0 else h h = 419430400 if h > 419430400 else h humidity = h >> 12 + if humidity < 0: + humidity = 0 + if humidity > 100 * 1024: + humidity = 100 * 1024 if result: result[0] = temp