10static 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 : window_size_(window_size), send_every_(send_every), send_at_(send_every - send_first_at) {
60 ESP_LOGVV(TAG,
"SlidingWindowFilter(%p)::new_value(%f) SENDING %f",
this, value, result);
87 size_t size = values.
size();
88 size_t mid = size / 2;
92 std::nth_element(values.
begin(), values.
begin() + mid, values.
end());
97 std::nth_element(values.
begin(), values.
begin() + mid, values.
end());
98 float upper = values[mid];
100 float lower = *std::max_element(values.
begin(), values.
begin() + mid);
101 return (lower + upper) / 2.0f;
109 ESP_LOGV(TAG,
"SkipInitialFilter(%p)::new_value(%f) SKIPPING, %zu left",
this, value,
num_to_ignore_);
113 ESP_LOGV(TAG,
"SkipInitialFilter(%p)::new_value(%f) SENDING",
this, value);
126 size_t position = ceilf(values.
size() * this->quantile_) - 1;
127 ESP_LOGVV(TAG,
"QuantileFilter(%p)::position: %zu/%zu",
this,
position + 1, values.
size());
143 size_t valid_count = 0;
146 if (!std::isnan(v)) {
151 return valid_count ? sum / valid_count : NAN;
156 : alpha_(alpha), send_every_(send_every), send_at_(send_every - send_first_at) {}
158 if (!std::isnan(value)) {
167 const float average = std::isnan(value) ? value : this->
accumulator_;
168 ESP_LOGVV(TAG,
"ExponentialMovingAverageFilter(%p)::new_value(%f) -> %f",
this, value, average);
171 ESP_LOGVV(TAG,
"ExponentialMovingAverageFilter(%p)::new_value(%f) SENDING %f",
this, value, average);
184 ESP_LOGVV(TAG,
"ThrottleAverageFilter(%p)::new_value(value=%f)",
this, value);
185 if (std::isnan(value)) {
195 ESP_LOGVV(TAG,
"ThrottleAverageFilter(%p)::interval(sum=%f, n=%i)",
this, this->
sum_, this->
n_);
216 ESP_LOGVV(TAG,
"LambdaFilter(%p)::new_value(%f) -> %f",
this, value, it.value_or(INFINITY));
235 float accuracy_mult = powf(10.0f, accuracy);
236 float rounded_sensor = roundf(accuracy_mult * sensor_value);
238 for (
auto &filter_value : this->
values_) {
239 float fv = filter_value.value();
242 if (std::isnan(fv)) {
243 if (std::isnan(sensor_value))
249 if (roundf(accuracy_mult * fv) == rounded_sensor)
280 :
ValueListFilter(prioritized_values), min_time_between_inputs_(min_time_between_inputs) {}
295 : delta_(delta), current_delta_(delta), last_value_(NAN), percentage_mode_(percentage_mode) {}
297 if (std::isnan(value)) {
319 if (!this->or_parent_->has_value_) {
320 this->or_parent_->output(value);
321 this->or_parent_->has_value_ =
true;
329 filter->input(value);
335 for (
auto *filter : this->
filters_) {
336 filter->initialize(parent, &this->
phi_);
346 if (now - this->timeout_start_time_ >= this->time_period_) {
348 this->
output(this->get_output_value());
351 this->disable_loop();
360 this->pending_value_ = value;
363 this->timeout_start_time_ =
millis();
373 this->timeout_start_time_ =
millis();
381 this->set_timeout(
"debounce", this->time_period_, [
this, value]() { this->
output(value); });
393 ESP_LOGVV(TAG,
"HeartbeatFilter(%p)::new_value(value=%f)",
this, value);
405 ESP_LOGVV(TAG,
"HeartbeatFilter(%p)::interval(has_value=%s, last_input=%f)",
this, YESNO(this->
has_value_),
417 : linear_functions_(linear_functions) {}
421 if (!std::isfinite(f[2]) || value < f[2])
422 return (value * f[0]) + f[1];
428 : coefficients_(coefficients) {}
434 res +=
x * coefficient;
441 : min_(min), max_(max), ignore_out_of_range_(ignore_out_of_range) {}
443 if (std::isfinite(value)) {
444 if (std::isfinite(this->
min_) && value < this->
min_) {
452 if (std::isfinite(this->
max_) && value > this->
max_) {
465 if (std::isfinite(value)) {
466 float accuracy_mult = powf(10.0f, this->
precision_);
467 return roundf(accuracy_mult * value) / accuracy_mult;
474 if (std::isfinite(value)) {
475 return value - remainderf(value, this->
multiple_);
481 if (!std::isfinite(value)) {
486 double t = value + k;
487 double y = (this->
a_ - 1 / (t)) / (2 * this->
c_);
488 double x = sqrt(pow(this->
b_ / (3 * this->
c_), 3) + y *
y);
489 double resistance = exp(pow(
x -
y, 1 / 3.0) - pow(
x +
y, 1 / 3.0));
494 if (!std::isfinite(value)) {
497 double lr = log(
double(value));
498 double v = this->
a_ + this->
b_ * lr + this->
c_ * lr * lr * lr;
499 double temp = float(1.0 / v - 273.15);
505 : window_size_(window_size), send_first_at_(send_first_at) {}
514 bool should_send =
false;
527 ESP_LOGVV(TAG,
"StreamingFilter(%p)::new_value(%f) SENDING %f",
this, value, result);
537 if (!std::isnan(value)) {
549 if (!std::isnan(value)) {
561 if (!std::isnan(value)) {
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_interval(const std voi set_interval)(const char *name, uint32_t interval, std::function< void()> &&f)
Set an interval function with a unique name.
Fixed-capacity vector - allocates once at runtime, never reallocates This avoids std::vector template...
void push_back(const T &value)
Add element without bounds checking Caller must ensure sufficient capacity was allocated via init() S...
FixedVector< std::array< float, 3 > > linear_functions_
optional< float > new_value(float value) override
CalibrateLinearFilter(std::initializer_list< std::array< float, 3 > > linear_functions)
CalibratePolynomialFilter(std::initializer_list< float > coefficients)
optional< float > new_value(float value) override
FixedVector< float > coefficients_
bool ignore_out_of_range_
ClampFilter(float min, float max, bool ignore_out_of_range)
optional< float > new_value(float value) override
optional< float > new_value(float value) override
float get_setup_priority() const override
DebounceFilter(uint32_t time_period)
DeltaFilter(float delta, bool percentage_mode)
optional< float > new_value(float value) override
void set_send_every(size_t send_every)
optional< float > new_value(float value) override
ExponentialMovingAverageFilter(float alpha, size_t send_every, size_t send_first_at)
void set_alpha(float alpha)
Apply a filter to sensor values such as moving average.
virtual optional< float > new_value(float value)=0
This will be called every time the filter receives a new value.
virtual void initialize(Sensor *parent, Filter *next)
Initialize this filter, please note this can be called more than once.
optional< float > new_value(float value) override
FilterOutValueFilter(std::initializer_list< TemplatableValue< float > > values_to_filter_out)
HeartbeatFilter(uint32_t time_period)
optional< float > new_value(float value) override
float get_setup_priority() const override
const lambda_filter_t & get_lambda_filter() const
LambdaFilter(lambda_filter_t lambda_filter)
lambda_filter_t lambda_filter_
void set_lambda_filter(const lambda_filter_t &lambda_filter)
optional< float > new_value(float value) override
float compute_result() override
float compute_result() override
float find_extremum_()
Helper to find min or max value in window, skipping NaN values Usage: find_extremum_<std::less<float>...
optional< float > new_value(float value) override
MultiplyFilter(TemplatableValue< float > multiplier)
TemplatableValue< float > multiplier_
optional< float > new_value(float value) override
OffsetFilter(TemplatableValue< float > offset)
TemplatableValue< float > offset_
optional< float > new_value(float value) override
PhiNode(OrFilter *or_parent)
OrFilter(std::initializer_list< Filter * > filters)
optional< float > new_value(float value) override
void initialize(Sensor *parent, Filter *next) override
FixedVector< Filter * > filters_
float compute_result() override
QuantileFilter(size_t window_size, size_t send_every, size_t send_first_at, float quantile)
Construct a QuantileFilter.
RoundFilter(uint8_t precision)
optional< float > new_value(float value) override
optional< float > new_value(float value) override
RoundMultipleFilter(float multiple)
Base-class for all sensors.
void internal_send_state_to_frontend(float state)
int8_t get_accuracy_decimals()
Get the accuracy in decimals, using the manual override if set.
SkipInitialFilter(size_t num_to_ignore)
Construct a SkipInitialFilter.
optional< float > new_value(float value) override
size_t window_head_
Index where next value will be written.
size_t window_size_
Maximum window size.
optional< float > new_value(float value) final
size_t window_count_
Number of valid values in window (0 to window_size_)
FixedVector< float > window_
Access the sliding window values (ring buffer implementation) Use: for (size_t i = 0; i < window_coun...
size_t send_at_
Counter for send_every.
size_t send_every_
Send result every N values.
virtual float compute_result()=0
Called by new_value() to compute the filtered result from the current window.
SlidingWindowFilter(size_t window_size, size_t send_every, size_t send_first_at)
float compute_result() override
Base class for filters that need a sorted window (Median, Quantile).
FixedVector< float > get_window_values_()
Helper to get non-NaN values from the window (not sorted - caller will use nth_element) Returns empty...
virtual void process_value(float value)=0
Called by new_value() to process each value in the batch.
virtual float compute_batch_result()=0
Called by new_value() to compute the result after collecting window_size values.
StreamingFilter(size_t window_size, size_t send_first_at)
virtual void reset_batch()=0
Called by new_value() to reset internal state after sending a result.
optional< float > new_value(float value) final
void reset_batch() override
void process_value(float value) override
float compute_batch_result() override
void reset_batch() override
float compute_batch_result() override
void process_value(float value) override
float compute_batch_result() override
void reset_batch() override
void process_value(float value) override
optional< float > new_value(float value) override
ThrottleAverageFilter(uint32_t time_period)
float get_setup_priority() const override
uint32_t min_time_between_inputs_
ThrottleFilter(uint32_t min_time_between_inputs)
optional< float > new_value(float value) override
optional< float > new_value(float value) override
ThrottleWithPriorityFilter(uint32_t min_time_between_inputs, std::initializer_list< TemplatableValue< float > > prioritized_values)
uint32_t min_time_between_inputs_
float get_setup_priority() const override
optional< float > new_value(float value) override
optional< float > new_value(float value) override
optional< float > new_value(float value) override
Base class for filters that compare sensor values against a list of configured values.
ValueListFilter(std::initializer_list< TemplatableValue< float > > values)
FixedVector< TemplatableValue< float > > values_
bool value_matches_any_(float sensor_value)
Check if sensor value matches any configured value (with accuracy rounding)
std::function< optional< float >(float)> lambda_filter_t
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
uint32_t IRAM_ATTR HOT millis()
Application App
Global storage of Application pointer - only one Application can exist.