ESPHome  2024.11.1
filter.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <queue>
4 #include <utility>
5 #include <vector>
7 #include "esphome/core/helpers.h"
9 
10 namespace esphome {
11 namespace sensor {
12 
13 class Sensor;
14 
20 class Filter {
21  public:
31  virtual optional<float> new_value(float value) = 0;
32 
34  virtual void initialize(Sensor *parent, Filter *next);
35 
36  void input(float value);
37 
38  void output(float value);
39 
40  protected:
41  friend Sensor;
42 
43  Filter *next_{nullptr};
44  Sensor *parent_{nullptr};
45 };
46 
51 class QuantileFilter : public Filter {
52  public:
62  explicit QuantileFilter(size_t window_size, size_t send_every, size_t send_first_at, float quantile);
63 
64  optional<float> new_value(float value) override;
65 
66  void set_send_every(size_t send_every);
67  void set_window_size(size_t window_size);
68  void set_quantile(float quantile);
69 
70  protected:
71  std::deque<float> queue_;
72  size_t send_every_;
73  size_t send_at_;
74  size_t window_size_;
75  float quantile_;
76 };
77 
82 class MedianFilter : public Filter {
83  public:
92  explicit MedianFilter(size_t window_size, size_t send_every, size_t send_first_at);
93 
94  optional<float> new_value(float value) override;
95 
96  void set_send_every(size_t send_every);
97  void set_window_size(size_t window_size);
98 
99  protected:
100  std::deque<float> queue_;
101  size_t send_every_;
102  size_t send_at_;
103  size_t window_size_;
104 };
105 
110 class SkipInitialFilter : public Filter {
111  public:
116  explicit SkipInitialFilter(size_t num_to_ignore);
117 
118  optional<float> new_value(float value) override;
119 
120  protected:
122 };
123 
128 class MinFilter : public Filter {
129  public:
138  explicit MinFilter(size_t window_size, size_t send_every, size_t send_first_at);
139 
140  optional<float> new_value(float value) override;
141 
142  void set_send_every(size_t send_every);
143  void set_window_size(size_t window_size);
144 
145  protected:
146  std::deque<float> queue_;
147  size_t send_every_;
148  size_t send_at_;
149  size_t window_size_;
150 };
151 
156 class MaxFilter : public Filter {
157  public:
166  explicit MaxFilter(size_t window_size, size_t send_every, size_t send_first_at);
167 
168  optional<float> new_value(float value) override;
169 
170  void set_send_every(size_t send_every);
171  void set_window_size(size_t window_size);
172 
173  protected:
174  std::deque<float> queue_;
175  size_t send_every_;
176  size_t send_at_;
177  size_t window_size_;
178 };
179 
186  public:
195  explicit SlidingWindowMovingAverageFilter(size_t window_size, size_t send_every, size_t send_first_at);
196 
197  optional<float> new_value(float value) override;
198 
199  void set_send_every(size_t send_every);
200  void set_window_size(size_t window_size);
201 
202  protected:
203  std::deque<float> queue_;
204  size_t send_every_;
205  size_t send_at_;
206  size_t window_size_;
207 };
208 
215  public:
216  ExponentialMovingAverageFilter(float alpha, size_t send_every, size_t send_first_at);
217 
218  optional<float> new_value(float value) override;
219 
220  void set_send_every(size_t send_every);
221  void set_alpha(float alpha);
222 
223  protected:
224  bool first_value_{true};
225  float accumulator_{NAN};
226  size_t send_every_;
227  size_t send_at_;
228  float alpha_;
229 };
230 
235 class ThrottleAverageFilter : public Filter, public Component {
236  public:
237  explicit ThrottleAverageFilter(uint32_t time_period);
238 
239  void setup() override;
240 
241  optional<float> new_value(float value) override;
242 
243  float get_setup_priority() const override;
244 
245  protected:
246  uint32_t time_period_;
247  float sum_{0.0f};
248  unsigned int n_{0};
249  bool have_nan_{false};
250 };
251 
252 using lambda_filter_t = std::function<optional<float>(float)>;
253 
261 class LambdaFilter : public Filter {
262  public:
263  explicit LambdaFilter(lambda_filter_t lambda_filter);
264 
265  optional<float> new_value(float value) override;
266 
267  const lambda_filter_t &get_lambda_filter() const;
268  void set_lambda_filter(const lambda_filter_t &lambda_filter);
269 
270  protected:
272 };
273 
275 class OffsetFilter : public Filter {
276  public:
277  explicit OffsetFilter(TemplatableValue<float> offset);
278 
279  optional<float> new_value(float value) override;
280 
281  protected:
283 };
284 
286 class MultiplyFilter : public Filter {
287  public:
288  explicit MultiplyFilter(TemplatableValue<float> multiplier);
289  optional<float> new_value(float value) override;
290 
291  protected:
293 };
294 
296 class FilterOutValueFilter : public Filter {
297  public:
298  explicit FilterOutValueFilter(std::vector<TemplatableValue<float>> values_to_filter_out);
299 
300  optional<float> new_value(float value) override;
301 
302  protected:
303  std::vector<TemplatableValue<float>> values_to_filter_out_;
304 };
305 
306 class ThrottleFilter : public Filter {
307  public:
308  explicit ThrottleFilter(uint32_t min_time_between_inputs);
309 
310  optional<float> new_value(float value) override;
311 
312  protected:
313  uint32_t last_input_{0};
315 };
316 
317 class TimeoutFilter : public Filter, public Component {
318  public:
319  explicit TimeoutFilter(uint32_t time_period, TemplatableValue<float> new_value);
320 
321  optional<float> new_value(float value) override;
322 
323  float get_setup_priority() const override;
324 
325  protected:
326  uint32_t time_period_;
328 };
329 
330 class DebounceFilter : public Filter, public Component {
331  public:
332  explicit DebounceFilter(uint32_t time_period);
333 
334  optional<float> new_value(float value) override;
335 
336  float get_setup_priority() const override;
337 
338  protected:
339  uint32_t time_period_;
340 };
341 
342 class HeartbeatFilter : public Filter, public Component {
343  public:
344  explicit HeartbeatFilter(uint32_t time_period);
345 
346  void setup() override;
347 
348  optional<float> new_value(float value) override;
349 
350  float get_setup_priority() const override;
351 
352  protected:
353  uint32_t time_period_;
354  float last_input_;
355  bool has_value_{false};
356 };
357 
358 class DeltaFilter : public Filter {
359  public:
360  explicit DeltaFilter(float delta, bool percentage_mode);
361 
362  optional<float> new_value(float value) override;
363 
364  protected:
365  float delta_;
368  float last_value_{NAN};
369 };
370 
371 class OrFilter : public Filter {
372  public:
373  explicit OrFilter(std::vector<Filter *> filters);
374 
375  void initialize(Sensor *parent, Filter *next) override;
376 
377  optional<float> new_value(float value) override;
378 
379  protected:
380  class PhiNode : public Filter {
381  public:
382  PhiNode(OrFilter *or_parent);
383  optional<float> new_value(float value) override;
384 
385  protected:
387  };
388 
389  std::vector<Filter *> filters_;
390  bool has_value_{false};
392 };
393 
395  public:
396  CalibrateLinearFilter(std::vector<std::array<float, 3>> linear_functions)
397  : linear_functions_(std::move(linear_functions)) {}
398  optional<float> new_value(float value) override;
399 
400  protected:
401  std::vector<std::array<float, 3>> linear_functions_;
402 };
403 
405  public:
406  CalibratePolynomialFilter(std::vector<float> coefficients) : coefficients_(std::move(coefficients)) {}
407  optional<float> new_value(float value) override;
408 
409  protected:
410  std::vector<float> coefficients_;
411 };
412 
413 class ClampFilter : public Filter {
414  public:
415  ClampFilter(float min, float max, bool ignore_out_of_range);
416  optional<float> new_value(float value) override;
417 
418  protected:
419  float min_{NAN};
420  float max_{NAN};
422 };
423 
424 class RoundFilter : public Filter {
425  public:
426  explicit RoundFilter(uint8_t precision);
427  optional<float> new_value(float value) override;
428 
429  protected:
430  uint8_t precision_;
431 };
432 
433 class RoundMultipleFilter : public Filter {
434  public:
435  explicit RoundMultipleFilter(float multiple);
436  optional<float> new_value(float value) override;
437 
438  protected:
439  float multiple_;
440 };
441 
442 } // namespace sensor
443 } // namespace esphome
void setup()
lambda_filter_t lambda_filter_
Definition: filter.h:271
Simple max filter.
Definition: filter.h:156
void input(float value)
Definition: filter.cpp:13
std::deque< float > queue_
Definition: filter.h:71
This class allows for creation of simple template filters.
Definition: filter.h:261
std::vector< std::array< float, 3 > > linear_functions_
Definition: filter.h:401
std::vector< Filter * > filters_
Definition: filter.h:389
std::vector< float > coefficients_
Definition: filter.h:410
STL namespace.
std::deque< float > queue_
Definition: filter.h:174
void output(float value)
Definition: filter.cpp:19
virtual optional< float > new_value(float value)=0
This will be called every time the filter receives a new value.
Simple median filter.
Definition: filter.h:82
TemplatableValue< float > value_
Definition: filter.h:327
std::vector< TemplatableValue< float > > values_to_filter_out_
Definition: filter.h:303
Simple exponential moving average filter.
Definition: filter.h:214
CalibratePolynomialFilter(std::vector< float > coefficients)
Definition: filter.h:406
A simple filter that only forwards the filter chain if it doesn&#39;t receive value_to_filter_out.
Definition: filter.h:296
virtual void initialize(Sensor *parent, Filter *next)
Initialize this filter, please note this can be called more than once.
Definition: filter.cpp:28
A simple filter that adds offset to each value it receives.
Definition: filter.h:275
TemplatableValue< float > offset_
Definition: filter.h:282
TemplatableValue< float > multiplier_
Definition: filter.h:292
A simple filter that multiplies to each value it receives by multiplier.
Definition: filter.h:286
std::deque< float > queue_
Definition: filter.h:146
Simple quantile filter.
Definition: filter.h:51
std::function< optional< float >(float)> lambda_filter_t
Definition: filter.h:252
Simple skip filter.
Definition: filter.h:110
Simple min filter.
Definition: filter.h:128
std::deque< float > queue_
Definition: filter.h:100
Apply a filter to sensor values such as moving average.
Definition: filter.h:20
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
Simple throttle average filter.
Definition: filter.h:235
Base-class for all sensors.
Definition: sensor.h:57
Simple sliding window moving average filter.
Definition: filter.h:185
esphome::sensor::Sensor * sensor
Definition: statsd.h:38
CalibrateLinearFilter(std::vector< std::array< float, 3 >> linear_functions)
Definition: filter.h:396