ESPHome  2022.11.3
filter.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include "esphome/core/helpers.h"
5 #include <queue>
6 #include <utility>
7 
8 namespace esphome {
9 namespace sensor {
10 
11 class Sensor;
12 
18 class Filter {
19  public:
29  virtual optional<float> new_value(float value) = 0;
30 
32  virtual void initialize(Sensor *parent, Filter *next);
33 
34  void input(float value);
35 
36  void output(float value);
37 
38  protected:
39  friend Sensor;
40 
41  Filter *next_{nullptr};
42  Sensor *parent_{nullptr};
43 };
44 
49 class QuantileFilter : public Filter {
50  public:
60  explicit QuantileFilter(size_t window_size, size_t send_every, size_t send_first_at, float quantile);
61 
62  optional<float> new_value(float value) override;
63 
64  void set_send_every(size_t send_every);
65  void set_window_size(size_t window_size);
66  void set_quantile(float quantile);
67 
68  protected:
69  std::deque<float> queue_;
70  size_t send_every_;
71  size_t send_at_;
72  size_t window_size_;
73  float quantile_;
74 };
75 
80 class MedianFilter : public Filter {
81  public:
90  explicit MedianFilter(size_t window_size, size_t send_every, size_t send_first_at);
91 
92  optional<float> new_value(float value) override;
93 
94  void set_send_every(size_t send_every);
95  void set_window_size(size_t window_size);
96 
97  protected:
98  std::deque<float> queue_;
99  size_t send_every_;
100  size_t send_at_;
101  size_t window_size_;
102 };
103 
108 class MinFilter : public Filter {
109  public:
118  explicit MinFilter(size_t window_size, size_t send_every, size_t send_first_at);
119 
120  optional<float> new_value(float value) override;
121 
122  void set_send_every(size_t send_every);
123  void set_window_size(size_t window_size);
124 
125  protected:
126  std::deque<float> queue_;
127  size_t send_every_;
128  size_t send_at_;
129  size_t window_size_;
130 };
131 
136 class MaxFilter : public Filter {
137  public:
146  explicit MaxFilter(size_t window_size, size_t send_every, size_t send_first_at);
147 
148  optional<float> new_value(float value) override;
149 
150  void set_send_every(size_t send_every);
151  void set_window_size(size_t window_size);
152 
153  protected:
154  std::deque<float> queue_;
155  size_t send_every_;
156  size_t send_at_;
157  size_t window_size_;
158 };
159 
166  public:
175  explicit SlidingWindowMovingAverageFilter(size_t window_size, size_t send_every, size_t send_first_at);
176 
177  optional<float> new_value(float value) override;
178 
179  void set_send_every(size_t send_every);
180  void set_window_size(size_t window_size);
181 
182  protected:
183  std::deque<float> queue_;
184  size_t send_every_;
185  size_t send_at_;
186  size_t window_size_;
187 };
188 
195  public:
196  ExponentialMovingAverageFilter(float alpha, size_t send_every, size_t send_first_at);
197 
198  optional<float> new_value(float value) override;
199 
200  void set_send_every(size_t send_every);
201  void set_alpha(float alpha);
202 
203  protected:
204  bool first_value_{true};
205  float accumulator_{NAN};
206  size_t send_every_;
207  size_t send_at_;
208  float alpha_;
209 };
210 
215 class ThrottleAverageFilter : public Filter, public Component {
216  public:
217  explicit ThrottleAverageFilter(uint32_t time_period);
218 
219  void setup() override;
220 
221  optional<float> new_value(float value) override;
222 
223  float get_setup_priority() const override;
224 
225  protected:
226  uint32_t time_period_;
227  float sum_{0.0f};
228  unsigned int n_{0};
229 };
230 
231 using lambda_filter_t = std::function<optional<float>(float)>;
232 
240 class LambdaFilter : public Filter {
241  public:
242  explicit LambdaFilter(lambda_filter_t lambda_filter);
243 
244  optional<float> new_value(float value) override;
245 
246  const lambda_filter_t &get_lambda_filter() const;
247  void set_lambda_filter(const lambda_filter_t &lambda_filter);
248 
249  protected:
251 };
252 
254 class OffsetFilter : public Filter {
255  public:
256  explicit OffsetFilter(float offset);
257 
258  optional<float> new_value(float value) override;
259 
260  protected:
261  float offset_;
262 };
263 
265 class MultiplyFilter : public Filter {
266  public:
267  explicit MultiplyFilter(float multiplier);
268 
269  optional<float> new_value(float value) override;
270 
271  protected:
272  float multiplier_;
273 };
274 
276 class FilterOutValueFilter : public Filter {
277  public:
278  explicit FilterOutValueFilter(float value_to_filter_out);
279 
280  optional<float> new_value(float value) override;
281 
282  protected:
284 };
285 
286 class ThrottleFilter : public Filter {
287  public:
288  explicit ThrottleFilter(uint32_t min_time_between_inputs);
289 
290  optional<float> new_value(float value) override;
291 
292  protected:
293  uint32_t last_input_{0};
295 };
296 
297 class DebounceFilter : public Filter, public Component {
298  public:
299  explicit DebounceFilter(uint32_t time_period);
300 
301  optional<float> new_value(float value) override;
302 
303  float get_setup_priority() const override;
304 
305  protected:
306  uint32_t time_period_;
307 };
308 
309 class HeartbeatFilter : public Filter, public Component {
310  public:
311  explicit HeartbeatFilter(uint32_t time_period);
312 
313  void setup() override;
314 
315  optional<float> new_value(float value) override;
316 
317  float get_setup_priority() const override;
318 
319  protected:
320  uint32_t time_period_;
321  float last_input_;
322  bool has_value_{false};
323 };
324 
325 class DeltaFilter : public Filter {
326  public:
327  explicit DeltaFilter(float min_delta);
328 
329  optional<float> new_value(float value) override;
330 
331  protected:
332  float min_delta_;
333  float last_value_{NAN};
334 };
335 
336 class OrFilter : public Filter {
337  public:
338  explicit OrFilter(std::vector<Filter *> filters);
339 
340  void initialize(Sensor *parent, Filter *next) override;
341 
342  optional<float> new_value(float value) override;
343 
344  protected:
345  class PhiNode : public Filter {
346  public:
347  PhiNode(OrFilter *or_parent);
348  optional<float> new_value(float value) override;
349 
350  protected:
352  };
353 
354  std::vector<Filter *> filters_;
356 };
357 
359  public:
360  CalibrateLinearFilter(float slope, float bias);
361  optional<float> new_value(float value) override;
362 
363  protected:
364  float slope_;
365  float bias_;
366 };
367 
369  public:
370  CalibratePolynomialFilter(std::vector<float> coefficients) : coefficients_(std::move(coefficients)) {}
371  optional<float> new_value(float value) override;
372 
373  protected:
374  std::vector<float> coefficients_;
375 };
376 
377 } // namespace sensor
378 } // namespace esphome
void setup()
lambda_filter_t lambda_filter_
Definition: filter.h:250
Simple max filter.
Definition: filter.h:136
void input(float value)
Definition: filter.cpp:13
std::deque< float > queue_
Definition: filter.h:69
This class allows for creation of simple template filters.
Definition: filter.h:240
std::vector< Filter * > filters_
Definition: filter.h:354
std::vector< float > coefficients_
Definition: filter.h:374
STL namespace.
std::deque< float > queue_
Definition: filter.h:154
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:80
Simple exponential moving average filter.
Definition: filter.h:194
CalibratePolynomialFilter(std::vector< float > coefficients)
Definition: filter.h:370
A simple filter that only forwards the filter chain if it doesn&#39;t receive value_to_filter_out.
Definition: filter.h:276
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:254
A simple filter that multiplies to each value it receives by multiplier.
Definition: filter.h:265
std::deque< float > queue_
Definition: filter.h:126
Simple quantile filter.
Definition: filter.h:49
std::function< optional< float >(float)> lambda_filter_t
Definition: filter.h:231
Simple min filter.
Definition: filter.h:108
std::deque< float > queue_
Definition: filter.h:98
Apply a filter to sensor values such as moving average.
Definition: filter.h:18
Definition: a4988.cpp:4
Simple throttle average filter.
Definition: filter.h:215
Base-class for all sensors.
Definition: sensor.h:48
Simple sliding window moving average filter.
Definition: filter.h:165