mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-30 22:53:59 +00:00 
			
		
		
		
	fix throttle average nan handling (#6275)
Co-authored-by: Samuel Sieb <samuel@sieb.net>
This commit is contained in:
		| @@ -252,7 +252,9 @@ ThrottleAverageFilter::ThrottleAverageFilter(uint32_t time_period) : time_period | |||||||
|  |  | ||||||
| optional<float> ThrottleAverageFilter::new_value(float value) { | optional<float> ThrottleAverageFilter::new_value(float value) { | ||||||
|   ESP_LOGVV(TAG, "ThrottleAverageFilter(%p)::new_value(value=%f)", this, value); |   ESP_LOGVV(TAG, "ThrottleAverageFilter(%p)::new_value(value=%f)", this, value); | ||||||
|   if (!std::isnan(value)) { |   if (std::isnan(value)) { | ||||||
|  |     this->have_nan_ = true; | ||||||
|  |   } else { | ||||||
|     this->sum_ += value; |     this->sum_ += value; | ||||||
|     this->n_++; |     this->n_++; | ||||||
|   } |   } | ||||||
| @@ -262,12 +264,14 @@ void ThrottleAverageFilter::setup() { | |||||||
|   this->set_interval("throttle_average", this->time_period_, [this]() { |   this->set_interval("throttle_average", this->time_period_, [this]() { | ||||||
|     ESP_LOGVV(TAG, "ThrottleAverageFilter(%p)::interval(sum=%f, n=%i)", this, this->sum_, this->n_); |     ESP_LOGVV(TAG, "ThrottleAverageFilter(%p)::interval(sum=%f, n=%i)", this, this->sum_, this->n_); | ||||||
|     if (this->n_ == 0) { |     if (this->n_ == 0) { | ||||||
|       this->output(NAN); |       if (this->have_nan_) | ||||||
|  |         this->output(NAN); | ||||||
|     } else { |     } else { | ||||||
|       this->output(this->sum_ / this->n_); |       this->output(this->sum_ / this->n_); | ||||||
|       this->sum_ = 0.0f; |       this->sum_ = 0.0f; | ||||||
|       this->n_ = 0; |       this->n_ = 0; | ||||||
|     } |     } | ||||||
|  |     this->have_nan_ = false; | ||||||
|   }); |   }); | ||||||
| } | } | ||||||
| float ThrottleAverageFilter::get_setup_priority() const { return setup_priority::HARDWARE; } | float ThrottleAverageFilter::get_setup_priority() const { return setup_priority::HARDWARE; } | ||||||
|   | |||||||
| @@ -245,6 +245,7 @@ class ThrottleAverageFilter : public Filter, public Component { | |||||||
|   uint32_t time_period_; |   uint32_t time_period_; | ||||||
|   float sum_{0.0f}; |   float sum_{0.0f}; | ||||||
|   unsigned int n_{0}; |   unsigned int n_{0}; | ||||||
|  |   bool have_nan_{false}; | ||||||
| }; | }; | ||||||
|  |  | ||||||
| using lambda_filter_t = std::function<optional<float>(float)>; | using lambda_filter_t = std::function<optional<float>(float)>; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user