diff --git a/esphome/components/sensor/filter.cpp b/esphome/components/sensor/filter.cpp index 0e52f9d94f..4863c00a29 100644 --- a/esphome/components/sensor/filter.cpp +++ b/esphome/components/sensor/filter.cpp @@ -53,11 +53,13 @@ optional SlidingWindowFilter::new_value(float value) { // Buffer not yet full - just append this->window_.push_back(value); this->window_count_++; - this->window_head_ = this->window_count_ % this->window_size_; } else { // Buffer full - overwrite oldest value (ring buffer) this->window_[this->window_head_] = value; - this->window_head_ = (this->window_head_ + 1) % this->window_size_; + this->window_head_++; + if (this->window_head_ >= this->window_size_) { + this->window_head_ = 0; + } } // Check if we should send a result