diff --git a/esphome/components/sensor/filter.cpp b/esphome/components/sensor/filter.cpp index 3adf28748d..375505a557 100644 --- a/esphome/components/sensor/filter.cpp +++ b/esphome/components/sensor/filter.cpp @@ -445,22 +445,18 @@ optional CalibratePolynomialFilter::new_value(float value) { ClampFilter::ClampFilter(float min, float max, bool ignore_out_of_range) : min_(min), max_(max), ignore_out_of_range_(ignore_out_of_range) {} optional ClampFilter::new_value(float value) { - if (std::isfinite(value)) { - if (std::isfinite(this->min_) && value < this->min_) { - if (this->ignore_out_of_range_) { - return {}; - } else { - return this->min_; - } + if (std::isfinite(this->min_) && !(value >= this->min_)) { + if (this->ignore_out_of_range_) { + return {}; } + return this->min_; + } - if (std::isfinite(this->max_) && value > this->max_) { - if (this->ignore_out_of_range_) { - return {}; - } else { - return this->max_; - } + if (std::isfinite(this->max_) && !(value <= this->max_)) { + if (this->ignore_out_of_range_) { + return {}; } + return this->max_; } return value; } diff --git a/tests/components/template/common-base.yaml b/tests/components/template/common-base.yaml index afc3fd9819..9dc65fbab8 100644 --- a/tests/components/template/common-base.yaml +++ b/tests/components/template/common-base.yaml @@ -117,6 +117,7 @@ sensor: - 10.0 -> 12.1 - 13.0 -> 14.0 - clamp: + # Infinity and NaN will be clamped (NaN -> min_value, +Infinity -> max_value, -Infinity -> min_value) max_value: 10.0 min_value: -10.0 - debounce: 0.1s