2#ifdef USE_SENSOR_FILTER
14static const char *
const TAG =
"sensor.filter";
23 ESP_LOGVV(TAG,
"Filter(%p)::input(%f)",
this, value);
24 optional<float> out = this->
new_value(value);
29 if (this->
next_ ==
nullptr) {
30 ESP_LOGVV(TAG,
"Filter(%p)::output(%f) -> SENSOR",
this, value);
33 ESP_LOGVV(TAG,
"Filter(%p)::output(%f) -> %p",
this, value, this->
next_);
38 ESP_LOGVV(TAG,
"Filter(%p)::initialize(parent=%p next=%p)",
this, parent, next);
45 : send_every_(send_every), send_at_(send_every - send_first_at) {
57 ESP_LOGVV(TAG,
"SlidingWindowFilter(%p)::new_value(%f) SENDING %f",
this, value, result);
84 size_t mid =
size / 2;
88 std::nth_element(values.
begin(), values.
begin() + mid, values.
end());
93 std::nth_element(values.
begin(), values.
begin() + mid, values.
end());
94 float upper = values[mid];
96 float lower = *std::max_element(values.
begin(), values.
begin() + mid);
97 return (lower + upper) / 2.0f;
105 ESP_LOGV(TAG,
"SkipInitialFilter(%p)::new_value(%f) SKIPPING, %zu left",
this, value,
num_to_ignore_);
109 ESP_LOGV(TAG,
"SkipInitialFilter(%p)::new_value(%f) SENDING",
this, value);
122 size_t position = ceilf(values.
size() * this->quantile_) - 1;
123 ESP_LOGVV(TAG,
"QuantileFilter(%p)::position: %zu/%zu",
this,
position + 1, values.
size());
139 size_t valid_count = 0;
140 for (
float v : this->
window_) {
141 if (!std::isnan(v)) {
146 return valid_count ? sum / valid_count : NAN;
151 : alpha_(alpha), send_every_(send_every), send_at_(send_every - send_first_at) {}
153 if (!std::isnan(value)) {
162 const float average = std::isnan(value) ? value : this->
accumulator_;
163 ESP_LOGVV(TAG,
"ExponentialMovingAverageFilter(%p)::new_value(%f) -> %f",
this, value, average);
166 ESP_LOGVV(TAG,
"ExponentialMovingAverageFilter(%p)::new_value(%f) SENDING %f",
this, value, average);
179 ESP_LOGVV(TAG,
"ThrottleAverageFilter(%p)::new_value(value=%f)",
this, value);
180 if (std::isnan(value)) {
190 ESP_LOGVV(TAG,
"ThrottleAverageFilter(%p)::interval(sum=%f, n=%i)",
this, this->
sum_, this->
n_);
211 ESP_LOGVV(TAG,
"LambdaFilter(%p)::new_value(%f) -> %f",
this, value, it.value_or(INFINITY));
228 float accuracy_mult =
pow10_int(accuracy);
229 float rounded_sensor = roundf(accuracy_mult * sensor_value);
231 for (
size_t i = 0; i < count; i++) {
232 float fv = values[i].
value();
235 if (std::isnan(fv)) {
236 if (std::isnan(sensor_value))
242 if (roundf(accuracy_mult * fv) == rounded_sensor)
264 if (last_input == 0 || now - last_input >= min_time_between_inputs ||
274 : min_a0_(min_a0), min_a1_(min_a1), max_a0_(max_a0), max_a1_(max_a1) {}
288 float delta = fabsf(value - ref);
291 if (delta > min && delta <= max) {
300 for (
size_t i = 0; i < count; i++) {
308 for (
size_t i = 0; i < count; i++)
309 filters[i]->input(value);
365 ESP_LOGVV(TAG,
"HeartbeatFilter(%p)::new_value(value=%f)",
this, value);
377 ESP_LOGVV(TAG,
"HeartbeatFilter(%p)::interval(has_value=%s, last_input=%f)",
this, YESNO(this->
has_value_),
389 for (
size_t i = 0; i < count; i++) {
390 if (!std::isfinite(functions[i][2]) || value < functions[i][2])
391 return (value * functions[i][0]) + functions[i][1];
399 for (
size_t i = 0; i < count; i++) {
400 res +=
x * coefficients[i];
407 : min_(min), max_(max), ignore_out_of_range_(ignore_out_of_range) {}
409 if (std::isfinite(this->
min_) && !(value >= this->
min_)) {
416 if (std::isfinite(this->
max_) && !(value <= this->
max_)) {
427 if (std::isfinite(value)) {
429 return roundf(accuracy_mult * value) / accuracy_mult;
436 if (std::isfinite(value)) {
437 return value - remainderf(value, this->
multiple_);
443 if (!std::isfinite(value)) {
448 double t = value + k;
449 double y = (this->
a_ - 1 / (t)) / (2 * this->
c_);
450 double x = sqrt(pow(this->
b_ / (3 * this->
c_), 3) + y *
y);
451 double resistance = exp(pow(
x -
y, 1 / 3.0) - pow(
x +
y, 1 / 3.0));
456 if (!std::isfinite(value)) {
459 double lr = log(
double(value));
460 double v = this->
a_ + this->
b_ * lr + this->
c_ * lr *
lr *
lr;
461 double temp = float(1.0 / v - 273.15);
467 : window_size_(window_size), send_first_at_(send_first_at) {}
476 bool should_send =
false;
489 ESP_LOGVV(TAG,
"StreamingFilter(%p)::new_value(%f) SENDING %f",
this, value, result);
499 if (!std::isnan(value)) {
511 if (!std::isnan(value)) {
523 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_timeout(const std voi set_timeout)(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
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.
void enable_loop()
Enable this component's loop.
void disable_loop()
Disable this component's loop.
void push_overwrite(const T &value)
Push a value, overwriting the oldest if full.
void init(index_type capacity)
Allocate capacity - can only be called once.
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...
Function-pointer-only templatable storage (4 bytes on 32-bit).
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)
void set_baseline(float(*fn)(float))
DeltaFilter(float min_a0, float min_a1, float max_a0, float max_a1)
optional< float > new_value(float value) override
float(* baseline_)(float)
void set_send_every(uint16_t send_every)
optional< float > new_value(float value) override
void set_alpha(float alpha)
ExponentialMovingAverageFilter(float alpha, uint16_t send_every, uint16_t send_first_at)
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.
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>...
TemplatableFn< float > multiplier_
optional< float > new_value(float value) override
MultiplyFilter(TemplatableFn< float > multiplier)
optional< float > new_value(float value) override
TemplatableFn< float > offset_
OffsetFilter(TemplatableFn< float > offset)
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
FixedRingBuffer< float > window_
Sliding window ring buffer - automatically overwrites oldest values when full.
optional< float > new_value(float value) final
SlidingWindowFilter(uint16_t window_size, uint16_t send_every, uint16_t send_first_at)
uint16_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.
uint16_t send_at_
Counter for send_every.
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...
StreamingFilter(uint16_t window_size, uint16_t send_first_at)
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.
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
uint32_t timeout_start_time_
float get_setup_priority() const override
virtual float get_output_value()=0
optional< float > new_value(float value) override
optional< float > new_value(float value) override
optional< float > new_value(float value) override
optional< float > throttle_with_priority_new_value(Sensor *parent, float value, const TemplatableFn< float > *values, size_t count, uint32_t &last_input, uint32_t min_time_between_inputs)
Non-template helper for ThrottleWithPriorityFilter (implementation in filter.cpp)
void or_filter_initialize(Filter **filters, size_t count, Sensor *parent, Filter *phi)
Non-template helpers for OrFilter (implementation in filter.cpp)
bool value_list_matches_any(Sensor *parent, float sensor_value, const TemplatableFn< float > *values, size_t count)
Non-template helper for value matching (implementation in filter.cpp)
optional< float > calibrate_polynomial_compute(const float *coefficients, size_t count, float value)
Non-template helper for polynomial calibration (implementation in filter.cpp)
constexpr uint32_t FILTER_ID
optional< float > calibrate_linear_compute(const std::array< float, 3 > *functions, size_t count, float value)
Non-template helper for linear calibration (implementation in filter.cpp)
std::function< optional< float >(float)> lambda_filter_t
optional< float > or_filter_new_value(Filter **filters, size_t count, float value, bool &has_value)
constexpr 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.
float pow10_int(int8_t exp)
Compute 10^exp using iterative multiplication/division.