From 7122f1e551f1fa1b1b792d4e727ea270c06787e8 Mon Sep 17 00:00:00 2001 From: Maurice Makaay Date: Wed, 26 May 2021 17:08:17 +0200 Subject: [PATCH] Fixed rounding error issue in slider sensor. Thanks Jos! --- components/xiaomi_bslamp2/sensor/slider_sensor.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/components/xiaomi_bslamp2/sensor/slider_sensor.h b/components/xiaomi_bslamp2/sensor/slider_sensor.h index 43ebce9..4f4294f 100644 --- a/components/xiaomi_bslamp2/sensor/slider_sensor.h +++ b/components/xiaomi_bslamp2/sensor/slider_sensor.h @@ -40,6 +40,13 @@ class XiaomiBslamp2SliderSensor : public sensor::Sensor, public Component { float corrected_level = max(0.0f, level - 2.0f); float final_level = range_from_ + (slope_ * corrected_level); + + // Accomodate for rounding errors that might push the result + // value just past the "range to" value. + if (final_level > range_to_) { + final_level = range_to_; + } + this->publish_state(final_level); } });