ESPHome  2022.6.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  float sum_{0.0};
184  std::deque<float> queue_;
185  size_t send_every_;
186  size_t send_at_;
187  size_t window_size_;
188 };
189 
196  public:
197  ExponentialMovingAverageFilter(float alpha, size_t send_every, size_t send_first_at);
198 
199  optional<float> new_value(float value) override;
200 
201  void set_send_every(size_t send_every);
202  void set_alpha(float alpha);
203 
204  protected:
205  bool first_value_{true};
206  float accumulator_{0.0f};
207  size_t send_every_;
208  size_t send_at_;
209  float alpha_;
210 };
211 
216 class ThrottleAverageFilter : public Filter, public Component {
217  public:
218  explicit ThrottleAverageFilter(uint32_t time_period);
219 
220  void setup() override;
221 
222  optional<float> new_value(float value) override;
223 
224  float get_setup_priority() const override;
225 
226  protected:
227  uint32_t time_period_;
228  float sum_{0.0f};
229  unsigned int n_{0};
230 };
231 
232 using lambda_filter_t = std::function<optional<float>(float)>;
233 
241 class LambdaFilter : public Filter {
242  public:
243  explicit LambdaFilter(lambda_filter_t lambda_filter);
244 
245  optional<float> new_value(float value) override;
246 
247  const lambda_filter_t &get_lambda_filter() const;
248  void set_lambda_filter(const lambda_filter_t &lambda_filter);
249 
250  protected:
252 };
253 
255 class OffsetFilter : public Filter {
256  public:
257  explicit OffsetFilter(float offset);
258 
259  optional<float> new_value(float value) override;
260 
261  protected:
262  float offset_;
263 };
264 
266 class MultiplyFilter : public Filter {
267  public:
268  explicit MultiplyFilter(float multiplier);
269 
270  optional<float> new_value(float value) override;
271 
272  protected:
273  float multiplier_;
274 };
275 
277 class FilterOutValueFilter : public Filter {
278  public:
279  explicit FilterOutValueFilter(float value_to_filter_out);
280 
281  optional<float> new_value(float value) override;
282 
283  protected:
285 };
286 
287 class ThrottleFilter : public Filter {
288  public:
289  explicit ThrottleFilter(uint32_t min_time_between_inputs);
290 
291  optional<float> new_value(float value) override;
292 
293  protected:
294  uint32_t last_input_{0};
296 };
297 
298 class DebounceFilter : public Filter, public Component {
299  public:
300  explicit DebounceFilter(uint32_t time_period);
301 
302  optional<float> new_value(float value) override;
303 
304  float get_setup_priority() const override;
305 
306  protected:
307  uint32_t time_period_;
308 };
309 
310 class HeartbeatFilter : public Filter, public Component {
311  public:
312  explicit HeartbeatFilter(uint32_t time_period);
313 
314  void setup() override;
315 
316  optional<float> new_value(float value) override;
317 
318  float get_setup_priority() const override;
319 
320  protected:
321  uint32_t time_period_;
322  float last_input_;
323  bool has_value_{false};
324 };
325 
326 class DeltaFilter : public Filter {
327  public:
328  explicit DeltaFilter(float min_delta);
329 
330  optional<float> new_value(float value) override;
331 
332  protected:
333  float min_delta_;
334  float last_value_{NAN};
335 };
336 
337 class OrFilter : public Filter {
338  public:
339  explicit OrFilter(std::vector<Filter *> filters);
340 
341  void initialize(Sensor *parent, Filter *next) override;
342 
343  optional<float> new_value(float value) override;
344 
345  protected:
346  class PhiNode : public Filter {
347  public:
348  PhiNode(OrFilter *or_parent);
349  optional<float> new_value(float value) override;
350 
351  protected:
353  };
354 
355  std::vector<Filter *> filters_;
357 };
358 
360  public:
361  CalibrateLinearFilter(float slope, float bias);
362  optional<float> new_value(float value) override;
363 
364  protected:
365  float slope_;
366  float bias_;
367 };
368 
370  public:
371  CalibratePolynomialFilter(std::vector<float> coefficients) : coefficients_(std::move(coefficients)) {}
372  optional<float> new_value(float value) override;
373 
374  protected:
375  std::vector<float> coefficients_;
376 };
377 
378 } // namespace sensor
379 } // namespace esphome
void setup()
lambda_filter_t lambda_filter_
Definition: filter.h:251
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:241
std::vector< Filter * > filters_
Definition: filter.h:355
std::vector< float > coefficients_
Definition: filter.h:375
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:195
CalibratePolynomialFilter(std::vector< float > coefficients)
Definition: filter.h:371
A simple filter that only forwards the filter chain if it doesn&#39;t receive value_to_filter_out.
Definition: filter.h:277
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:255
A simple filter that multiplies to each value it receives by multiplier.
Definition: filter.h:266
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:232
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:216
Base-class for all sensors.
Definition: sensor.h:47
Simple sliding window moving average filter.
Definition: filter.h:165