mirror of
https://github.com/esphome/esphome.git
synced 2024-11-22 15:08:10 +01:00
fix throttle average nan handling (#6275)
Co-authored-by: Samuel Sieb <samuel@sieb.net>
This commit is contained in:
parent
e66e135a63
commit
f3174c58bc
2 changed files with 7 additions and 2 deletions
|
@ -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)>;
|
||||||
|
|
Loading…
Reference in a new issue