10 static const char *
const TAG =
"sensor.filter";
14 ESP_LOGVV(TAG,
"Filter(%p)::input(%f)",
this, value);
20 if (this->
next_ ==
nullptr) {
21 ESP_LOGVV(TAG,
"Filter(%p)::output(%f) -> SENSOR",
this, value);
24 ESP_LOGVV(TAG,
"Filter(%p)::output(%f) -> %p",
this, value, this->
next_);
29 ESP_LOGVV(TAG,
"Filter(%p)::initialize(parent=%p next=%p)",
this, parent, next);
36 : send_every_(send_every), send_at_(send_every - send_first_at), window_size_(window_size) {}
43 this->
queue_.push_back(value);
44 ESP_LOGVV(TAG,
"MedianFilter(%p)::new_value(%f)",
this, value);
50 if (!this->
queue_.empty()) {
52 std::vector<float> median_queue;
53 for (
auto v : this->
queue_) {
55 median_queue.push_back(v);
59 sort(median_queue.begin(), median_queue.end());
61 size_t queue_size = median_queue.size();
64 median = median_queue[queue_size / 2];
66 median = (median_queue[queue_size / 2] + median_queue[(queue_size / 2) - 1]) / 2.0f;
71 ESP_LOGVV(TAG,
"MedianFilter(%p)::new_value(%f) SENDING %f",
this, value, median);
82 ESP_LOGV(TAG,
"SkipInitialFilter(%p)::new_value(%f) SKIPPING, %zu left",
this, value,
num_to_ignore_);
86 ESP_LOGV(TAG,
"SkipInitialFilter(%p)::new_value(%f) SENDING",
this, value);
92 : send_every_(send_every), send_at_(send_every - send_first_at), window_size_(window_size), quantile_(quantile) {}
100 this->
queue_.push_back(value);
101 ESP_LOGVV(TAG,
"QuantileFilter(%p)::new_value(%f), quantile:%f",
this, value, this->
quantile_);
107 if (!this->
queue_.empty()) {
109 std::vector<float> quantile_queue;
110 for (
auto v : this->
queue_) {
111 if (!std::isnan(v)) {
112 quantile_queue.push_back(v);
116 sort(quantile_queue.begin(), quantile_queue.end());
118 size_t queue_size = quantile_queue.size();
121 ESP_LOGVV(TAG,
"QuantileFilter(%p)::position: %d/%d",
this, position + 1, queue_size);
126 ESP_LOGVV(TAG,
"QuantileFilter(%p)::new_value(%f) SENDING %f",
this, value, result);
141 this->
queue_.push_back(value);
142 ESP_LOGVV(TAG,
"MinFilter(%p)::new_value(%f)",
this, value);
148 for (
auto v : this->
queue_) {
149 if (!std::isnan(v)) {
150 min = std::isnan(min) ? v : std::min(min, v);
154 ESP_LOGVV(TAG,
"MinFilter(%p)::new_value(%f) SENDING %f",
this, value, min);
169 this->
queue_.push_back(value);
170 ESP_LOGVV(TAG,
"MaxFilter(%p)::new_value(%f)",
this, value);
176 for (
auto v : this->
queue_) {
177 if (!std::isnan(v)) {
178 max = std::isnan(max) ? v : std::max(max, v);
182 ESP_LOGVV(TAG,
"MaxFilter(%p)::new_value(%f) SENDING %f",
this, value, max);
190 size_t send_first_at)
198 this->
queue_.push_back(value);
199 ESP_LOGVV(TAG,
"SlidingWindowMovingAverageFilter(%p)::new_value(%f)",
this, value);
205 size_t valid_count = 0;
206 for (
auto v : this->
queue_) {
207 if (!std::isnan(v)) {
215 average = sum / valid_count;
218 ESP_LOGVV(TAG,
"SlidingWindowMovingAverageFilter(%p)::new_value(%f) SENDING %f",
this, value, average);
228 if (!std::isnan(value)) {
237 const float average = std::isnan(value) ? value : this->
accumulator_;
238 ESP_LOGVV(TAG,
"ExponentialMovingAverageFilter(%p)::new_value(%f) -> %f",
this, value, average);
241 ESP_LOGVV(TAG,
"ExponentialMovingAverageFilter(%p)::new_value(%f) SENDING %f",
this, value, average);
254 ESP_LOGVV(TAG,
"ThrottleAverageFilter(%p)::new_value(value=%f)",
this, value);
255 if (std::isnan(value)) {
265 ESP_LOGVV(TAG,
"ThrottleAverageFilter(%p)::interval(sum=%f, n=%i)",
this, this->
sum_, this->
n_);
286 ESP_LOGVV(TAG,
"LambdaFilter(%p)::new_value(%f) -> %f",
this, value, it.value_or(INFINITY));
302 : values_to_filter_out_(
std::move(values_to_filter_out)) {}
306 float accuracy_mult = powf(10.0f, accuracy);
308 if (std::isnan(filter_value.value())) {
309 if (std::isnan(value)) {
314 float rounded_filter_out = roundf(accuracy_mult * filter_value.value());
315 float rounded_value = roundf(accuracy_mult * value);
316 if (rounded_filter_out == rounded_value) {
326 const uint32_t now =
millis();
336 : delta_(delta), current_delta_(delta), percentage_mode_(percentage_mode), last_value_(NAN) {}
338 if (std::isnan(value)) {
372 filter->input(value);
379 filter->initialize(parent, &this->
phi_);
386 this->set_timeout(
"timeout", this->time_period_, [
this]() { this->
output(this->value_.value()); });
391 : time_period_(time_period), value_(
std::move(new_value)) {}
408 ESP_LOGVV(TAG,
"HeartbeatFilter(%p)::new_value(value=%f)",
this, value);
416 ESP_LOGVV(TAG,
"HeartbeatFilter(%p)::interval(has_value=%s, last_input=%f)",
this, YESNO(this->
has_value_),
427 for (std::array<float, 3> f : this->linear_functions_) {
428 if (!std::isfinite(f[2]) || value < f[2])
429 return (value * f[0]) + f[1];
437 for (
float coefficient : this->coefficients_) {
438 res += x * coefficient;
445 : min_(min), max_(max), ignore_out_of_range_(ignore_out_of_range) {}
447 if (std::isfinite(value)) {
448 if (std::isfinite(this->
min_) && value < this->
min_) {
456 if (std::isfinite(this->
max_) && value > this->
max_) {
469 if (std::isfinite(value)) {
470 float accuracy_mult = powf(10.0f, this->
precision_);
471 return roundf(accuracy_mult * value) / accuracy_mult;
478 if (std::isfinite(value)) {
479 return value - remainderf(value, this->
multiple_);
optional< float > new_value(float value) override
optional< float > new_value(float value) override
lambda_filter_t lambda_filter_
void set_quantile(float quantile)
optional< float > new_value(float value) override
void set_interval(const std::string &name, uint32_t interval, std::function< void()> &&f)
Set an interval function with a unique name.
RoundFilter(uint8_t precision)
void set_send_every(size_t send_every)
uint32_t min_time_between_inputs_
const lambda_filter_t & get_lambda_filter() const
std::deque< float > queue_
DeltaFilter(float delta, bool percentage_mode)
PhiNode(OrFilter *or_parent)
std::vector< Filter * > filters_
optional< float > new_value(float value) override
ThrottleFilter(uint32_t min_time_between_inputs)
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
void set_send_every(size_t send_every)
bool ignore_out_of_range_
std::deque< float > queue_
TimeoutFilter(uint32_t time_period, TemplatableValue< float > new_value)
RoundMultipleFilter(float multiple)
void set_send_every(size_t send_every)
MaxFilter(size_t window_size, size_t send_every, size_t send_first_at)
Construct a MaxFilter.
void set_window_size(size_t window_size)
float get_setup_priority() const override
optional< float > new_value(float value) override
uint32_t IRAM_ATTR HOT millis()
optional< float > new_value(float value) override
optional< float > new_value(float value) override
virtual optional< float > new_value(float value)=0
This will be called every time the filter receives a new value.
MultiplyFilter(TemplatableValue< float > multiplier)
optional< float > new_value(float value) override
OrFilter(std::vector< Filter *> filters)
DebounceFilter(uint32_t time_period)
void set_window_size(size_t window_size)
optional< float > new_value(float value) override
OffsetFilter(TemplatableValue< float > offset)
optional< float > new_value(float value) override
HeartbeatFilter(uint32_t time_period)
float get_setup_priority() const override
SkipInitialFilter(size_t num_to_ignore)
Construct a SkipInitialFilter.
optional< float > new_value(float value) override
ClampFilter(float min, float max, bool ignore_out_of_range)
std::vector< TemplatableValue< float > > values_to_filter_out_
ThrottleAverageFilter(uint32_t time_period)
optional< float > new_value(float value) override
void set_alpha(float alpha)
virtual void initialize(Sensor *parent, Filter *next)
Initialize this filter, please note this can be called more than once.
void initialize(Sensor *parent, Filter *next) override
optional< float > new_value(float value) override
void set_lambda_filter(const lambda_filter_t &lambda_filter)
TemplatableValue< float > offset_
void set_send_every(size_t send_every)
TemplatableValue< float > multiplier_
optional< float > new_value(float value) override
optional< float > new_value(float value) override
ExponentialMovingAverageFilter(float alpha, size_t send_every, size_t send_first_at)
optional< float > new_value(float value) override
std::deque< float > queue_
std::deque< float > queue_
LambdaFilter(lambda_filter_t lambda_filter)
void set_window_size(size_t window_size)
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
optional< float > new_value(float value) override
std::function< optional< float >(float)> lambda_filter_t
void internal_send_state_to_frontend(float state)
float get_setup_priority() const override
void set_send_every(size_t send_every)
optional< float > new_value(float value) override
QuantileFilter(size_t window_size, size_t send_every, size_t send_first_at, float quantile)
Construct a QuantileFilter.
Apply a filter to sensor values such as moving average.
Implementation of SPI Controller mode.
SlidingWindowMovingAverageFilter(size_t window_size, size_t send_every, size_t send_first_at)
Construct a SlidingWindowMovingAverageFilter.
MinFilter(size_t window_size, size_t send_every, size_t send_first_at)
Construct a MinFilter.
int8_t get_accuracy_decimals()
Get the accuracy in decimals, using the manual override if set.
float get_setup_priority() const override
optional< float > new_value(float value) override
Base-class for all sensors.
optional< float > new_value(float value) override
optional< float > new_value(float value) override
FilterOutValueFilter(std::vector< TemplatableValue< float >> values_to_filter_out)
optional< float > new_value(float value) override
esphome::sensor::Sensor * sensor
void set_window_size(size_t window_size)
optional< float > new_value(float value) override