2#ifdef USE_SENSOR_FILTER
14static const char *
const TAG =
"sensor.filter";
18 ESP_LOGVV(TAG,
"Filter(%p)::input(%f)",
this, value);
19 optional<float> out = this->
new_value(value);
24 if (this->
next_ ==
nullptr) {
25 ESP_LOGVV(TAG,
"Filter(%p)::output(%f) -> SENSOR",
this, value);
28 ESP_LOGVV(TAG,
"Filter(%p)::output(%f) -> %p",
this, value, this->
next_);
33 ESP_LOGVV(TAG,
"Filter(%p)::initialize(parent=%p next=%p)",
this, parent, next);
40 : send_every_(send_every), send_at_(send_every - send_first_at) {
52 ESP_LOGVV(TAG,
"SlidingWindowFilter(%p)::new_value(%f) SENDING %f",
this, value, result);
79 size_t mid =
size / 2;
83 std::nth_element(values.
begin(), values.
begin() + mid, values.
end());
88 std::nth_element(values.
begin(), values.
begin() + mid, values.
end());
89 float upper = values[mid];
91 float lower = *std::max_element(values.
begin(), values.
begin() + mid);
92 return (lower + upper) / 2.0f;
100 ESP_LOGV(TAG,
"SkipInitialFilter(%p)::new_value(%f) SKIPPING, %zu left",
this, value,
num_to_ignore_);
104 ESP_LOGV(TAG,
"SkipInitialFilter(%p)::new_value(%f) SENDING",
this, value);
117 size_t position = ceilf(values.
size() * this->quantile_) - 1;
118 ESP_LOGVV(TAG,
"QuantileFilter(%p)::position: %zu/%zu",
this,
position + 1, values.
size());
134 size_t valid_count = 0;
135 for (
float v : this->
window_) {
136 if (!std::isnan(v)) {
141 return valid_count ? sum / valid_count : NAN;
146 : alpha_(alpha), send_every_(send_every), send_at_(send_every - send_first_at) {}
148 if (!std::isnan(value)) {
157 const float average = std::isnan(value) ? value : this->
accumulator_;
158 ESP_LOGVV(TAG,
"ExponentialMovingAverageFilter(%p)::new_value(%f) -> %f",
this, value, average);
161 ESP_LOGVV(TAG,
"ExponentialMovingAverageFilter(%p)::new_value(%f) SENDING %f",
this, value, average);
174 ESP_LOGVV(TAG,
"ThrottleAverageFilter(%p)::new_value(value=%f)",
this, value);
175 if (std::isnan(value)) {
186 ESP_LOGVV(TAG,
"ThrottleAverageFilter(%p)::interval(sum=%f, n=%i)",
this, this->
sum_, this->
n_);
206 ESP_LOGVV(TAG,
"LambdaFilter(%p)::new_value(%f) -> %f",
this, value, it.value_or(INFINITY));
223 float accuracy_mult =
pow10_int(accuracy);
224 float rounded_sensor = roundf(accuracy_mult * sensor_value);
226 for (
size_t i = 0; i < count; i++) {
227 float fv = values[i].
value();
230 if (std::isnan(fv)) {
231 if (std::isnan(sensor_value))
237 if (roundf(accuracy_mult * fv) == rounded_sensor)
259 if (last_input == 0 || now - last_input >= min_time_between_inputs ||
269 : min_time_between_inputs_(min_time_between_inputs) {}
281 : min_a0_(min_a0), min_a1_(min_a1), max_a0_(max_a0), max_a1_(max_a1) {}
295 float delta = fabsf(value - ref);
298 if (delta > min && delta <= max) {
307 for (
size_t i = 0; i < count; i++) {
315 for (
size_t i = 0; i < count; i++)
316 filters[i]->input(value);
371 ESP_LOGVV(TAG,
"HeartbeatFilter(%p)::new_value(value=%f)",
this, value);
384 ESP_LOGVV(TAG,
"HeartbeatFilter(%p)::interval(has_value=%s, last_input=%f)",
this, YESNO(this->
has_value_),
394 for (
size_t i = 0; i < count; i++) {
395 if (!std::isfinite(functions[i][2]) || value < functions[i][2])
396 return (value * functions[i][0]) + functions[i][1];
404 for (
size_t i = 0; i < count; i++) {
405 res +=
x * coefficients[i];
412 : min_(min), max_(max), ignore_out_of_range_(ignore_out_of_range) {}
414 if (std::isfinite(this->
min_) && !(value >= this->
min_)) {
421 if (std::isfinite(this->
max_) && !(value <= this->
max_)) {
432 if (std::isfinite(value)) {
434 return roundf(accuracy_mult * value) / accuracy_mult;
441 if (std::isfinite(value)) {
442 return value - remainderf(value, this->
multiple_);
448 if (!std::isfinite(value)) {
453 double t = value + k;
454 double y = (this->
a_ - 1 / (t)) / (2 * this->
c_);
455 double x = sqrt(pow(this->
b_ / (3 * this->
c_), 3) + y *
y);
456 double resistance = exp(pow(
x -
y, 1 / 3.0) - pow(
x +
y, 1 / 3.0));
461 if (!std::isfinite(value)) {
464 double lr = log(
double(value));
465 double v = this->
a_ + this->
b_ * lr + this->
c_ * lr *
lr *
lr;
466 double temp = float(1.0 / v - 273.15);
472 : window_size_(window_size), send_first_at_(send_first_at) {}
481 bool should_send =
false;
494 ESP_LOGVV(TAG,
"StreamingFilter(%p)::new_value(%f) SENDING %f",
this, value, result);
504 if (!std::isnan(value)) {
516 if (!std::isnan(value)) {
528 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.
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
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
void initialize(Sensor *parent, Filter *next) 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)
void initialize(Sensor *parent, Filter *next) override
uint32_t min_time_between_inputs_
ThrottleFilter(uint32_t min_time_between_inputs)
optional< float > new_value(float value) override
uint32_t min_time_between_inputs_
optional< float > new_value(float value) override
ThrottleWithPriorityNanFilter(uint32_t min_time_between_inputs)
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)
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.