ESPHome  2024.10.2
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"
8 
9 namespace esphome {
10 namespace sensor {
11 
12 class Sensor;
13 
19 class Filter {
20  public:
30  virtual optional<float> new_value(float value) = 0;
31 
33  virtual void initialize(Sensor *parent, Filter *next);
34 
35  void input(float value);
36 
37  void output(float value);
38 
39  protected:
40  friend Sensor;
41 
42  Filter *next_{nullptr};
43  Sensor *parent_{nullptr};
44 };
45 
50 class QuantileFilter : public Filter {
51  public:
61  explicit QuantileFilter(size_t window_size, size_t send_every, size_t send_first_at, float quantile);
62 
63  optional<float> new_value(float value) override;
64 
65  void set_send_every(size_t send_every);
66  void set_window_size(size_t window_size);
67  void set_quantile(float quantile);
68 
69  protected:
70  std::deque<float> queue_;
71  size_t send_every_;
72  size_t send_at_;
73  size_t window_size_;
74  float quantile_;
75 };
76 
81 class MedianFilter : public Filter {
82  public:
91  explicit MedianFilter(size_t window_size, size_t send_every, size_t send_first_at);
92 
93  optional<float> new_value(float value) override;
94 
95  void set_send_every(size_t send_every);
96  void set_window_size(size_t window_size);
97 
98  protected:
99  std::deque<float> queue_;
100  size_t send_every_;
101  size_t send_at_;
102  size_t window_size_;
103 };
104 
109 class SkipInitialFilter : public Filter {
110  public:
115  explicit SkipInitialFilter(size_t num_to_ignore);
116 
117  optional<float> new_value(float value) override;
118 
119  protected:
121 };
122 
127 class MinFilter : public Filter {
128  public:
137  explicit MinFilter(size_t window_size, size_t send_every, size_t send_first_at);
138 
139  optional<float> new_value(float value) override;
140 
141  void set_send_every(size_t send_every);
142  void set_window_size(size_t window_size);
143 
144  protected:
145  std::deque<float> queue_;
146  size_t send_every_;
147  size_t send_at_;
148  size_t window_size_;
149 };
150 
155 class MaxFilter : public Filter {
156  public:
165  explicit MaxFilter(size_t window_size, size_t send_every, size_t send_first_at);
166 
167  optional<float> new_value(float value) override;
168 
169  void set_send_every(size_t send_every);
170  void set_window_size(size_t window_size);
171 
172  protected:
173  std::deque<float> queue_;
174  size_t send_every_;
175  size_t send_at_;
176  size_t window_size_;
177 };
178 
185  public:
194  explicit SlidingWindowMovingAverageFilter(size_t window_size, size_t send_every, size_t send_first_at);
195 
196  optional<float> new_value(float value) override;
197 
198  void set_send_every(size_t send_every);
199  void set_window_size(size_t window_size);
200 
201  protected:
202  std::deque<float> queue_;
203  size_t send_every_;
204  size_t send_at_;
205  size_t window_size_;
206 };
207 
214  public:
215  ExponentialMovingAverageFilter(float alpha, size_t send_every, size_t send_first_at);
216 
217  optional<float> new_value(float value) override;
218 
219  void set_send_every(size_t send_every);
220  void set_alpha(float alpha);
221 
222  protected:
223  bool first_value_{true};
224  float accumulator_{NAN};
225  size_t send_every_;
226  size_t send_at_;
227  float alpha_;
228 };
229 
234 class ThrottleAverageFilter : public Filter, public Component {
235  public:
236  explicit ThrottleAverageFilter(uint32_t time_period);
237 
238  void setup() override;
239 
240  optional<float> new_value(float value) override;
241 
242  float get_setup_priority() const override;
243 
244  protected:
245  uint32_t time_period_;
246  float sum_{0.0f};
247  unsigned int n_{0};
248  bool have_nan_{false};
249 };
250 
251 using lambda_filter_t = std::function<optional<float>(float)>;
252 
260 class LambdaFilter : public Filter {
261  public:
262  explicit LambdaFilter(lambda_filter_t lambda_filter);
263 
264  optional<float> new_value(float value) override;
265 
266  const lambda_filter_t &get_lambda_filter() const;
267  void set_lambda_filter(const lambda_filter_t &lambda_filter);
268 
269  protected:
271 };
272 
274 class OffsetFilter : public Filter {
275  public:
276  explicit OffsetFilter(float offset);
277 
278  optional<float> new_value(float value) override;
279 
280  protected:
281  float offset_;
282 };
283 
285 class MultiplyFilter : public Filter {
286  public:
287  explicit MultiplyFilter(float multiplier);
288 
289  optional<float> new_value(float value) override;
290 
291  protected:
292  float multiplier_;
293 };
294 
296 class FilterOutValueFilter : public Filter {
297  public:
298  explicit FilterOutValueFilter(float value_to_filter_out);
299 
300  optional<float> new_value(float value) override;
301 
302  protected:
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, float new_value);
320  void set_value(float new_value) { this->value_ = new_value; }
321 
322  optional<float> new_value(float value) override;
323 
324  float get_setup_priority() const override;
325 
326  protected:
327  uint32_t time_period_;
328  float value_;
329 };
330 
331 class DebounceFilter : public Filter, public Component {
332  public:
333  explicit DebounceFilter(uint32_t time_period);
334 
335  optional<float> new_value(float value) override;
336 
337  float get_setup_priority() const override;
338 
339  protected:
340  uint32_t time_period_;
341 };
342 
343 class HeartbeatFilter : public Filter, public Component {
344  public:
345  explicit HeartbeatFilter(uint32_t time_period);
346 
347  void setup() override;
348 
349  optional<float> new_value(float value) override;
350 
351  float get_setup_priority() const override;
352 
353  protected:
354  uint32_t time_period_;
355  float last_input_;
356  bool has_value_{false};
357 };
358 
359 class DeltaFilter : public Filter {
360  public:
361  explicit DeltaFilter(float delta, bool percentage_mode);
362 
363  optional<float> new_value(float value) override;
364 
365  protected:
366  float delta_;
369  float last_value_{NAN};
370 };
371 
372 class OrFilter : public Filter {
373  public:
374  explicit OrFilter(std::vector<Filter *> filters);
375 
376  void initialize(Sensor *parent, Filter *next) override;
377 
378  optional<float> new_value(float value) override;
379 
380  protected:
381  class PhiNode : public Filter {
382  public:
383  PhiNode(OrFilter *or_parent);
384  optional<float> new_value(float value) override;
385 
386  protected:
388  };
389 
390  std::vector<Filter *> filters_;
391  bool has_value_{false};
393 };
394 
396  public:
397  CalibrateLinearFilter(std::vector<std::array<float, 3>> linear_functions)
398  : linear_functions_(std::move(linear_functions)) {}
399  optional<float> new_value(float value) override;
400 
401  protected:
402  std::vector<std::array<float, 3>> linear_functions_;
403 };
404 
406  public:
407  CalibratePolynomialFilter(std::vector<float> coefficients) : coefficients_(std::move(coefficients)) {}
408  optional<float> new_value(float value) override;
409 
410  protected:
411  std::vector<float> coefficients_;
412 };
413 
414 class ClampFilter : public Filter {
415  public:
416  ClampFilter(float min, float max, bool ignore_out_of_range);
417  optional<float> new_value(float value) override;
418 
419  protected:
420  float min_{NAN};
421  float max_{NAN};
423 };
424 
425 class RoundFilter : public Filter {
426  public:
427  explicit RoundFilter(uint8_t precision);
428  optional<float> new_value(float value) override;
429 
430  protected:
431  uint8_t precision_;
432 };
433 
434 class RoundMultipleFilter : public Filter {
435  public:
436  explicit RoundMultipleFilter(float multiple);
437  optional<float> new_value(float value) override;
438 
439  protected:
440  float multiple_;
441 };
442 
443 } // namespace sensor
444 } // namespace esphome
void setup()
lambda_filter_t lambda_filter_
Definition: filter.h:270
Simple max filter.
Definition: filter.h:155
void input(float value)
Definition: filter.cpp:13
std::deque< float > queue_
Definition: filter.h:70
This class allows for creation of simple template filters.
Definition: filter.h:260
std::vector< std::array< float, 3 > > linear_functions_
Definition: filter.h:402
std::vector< Filter * > filters_
Definition: filter.h:390
std::vector< float > coefficients_
Definition: filter.h:411
STL namespace.
std::deque< float > queue_
Definition: filter.h:173
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:81
Simple exponential moving average filter.
Definition: filter.h:213
CalibratePolynomialFilter(std::vector< float > coefficients)
Definition: filter.h:407
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:274
A simple filter that multiplies to each value it receives by multiplier.
Definition: filter.h:285
std::deque< float > queue_
Definition: filter.h:145
Simple quantile filter.
Definition: filter.h:50
std::function< optional< float >(float)> lambda_filter_t
Definition: filter.h:251
Simple skip filter.
Definition: filter.h:109
Simple min filter.
Definition: filter.h:127
std::deque< float > queue_
Definition: filter.h:99
Apply a filter to sensor values such as moving average.
Definition: filter.h:19
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
Simple throttle average filter.
Definition: filter.h:234
Base-class for all sensors.
Definition: sensor.h:57
void set_value(float new_value)
Definition: filter.h:320
Simple sliding window moving average filter.
Definition: filter.h:184
esphome::sensor::Sensor * sensor
Definition: statsd.h:37
CalibrateLinearFilter(std::vector< std::array< float, 3 >> linear_functions)
Definition: filter.h:397