ESPHome  2024.11.2
combination.h
Go to the documentation of this file.
1 #pragma once
2 
5 
6 #include <vector>
7 
8 namespace esphome {
9 namespace combination {
10 
12  public:
13  float get_setup_priority() const override { return esphome::setup_priority::DATA; }
14 
16  virtual void log_source_sensors() = 0;
17 
18  protected:
21  void log_config_(const LogString *combo_type);
22 };
23 
26  public:
28  void setup() override;
29 
30  void add_source(Sensor *sensor);
31 
34  virtual void handle_new_value(float value) = 0;
35 
37  void log_source_sensors() override;
38 
39  protected:
40  std::vector<Sensor *> sensors_;
41 };
42 
43 // Base class for opertions that require one parameter to compute the combination
45  public:
46  void add_source(Sensor *sensor, std::function<float(float)> const &stddev);
47  void add_source(Sensor *sensor, float stddev);
48 
50  void log_source_sensors() override;
51 
52  protected:
53  std::vector<std::pair<Sensor *, std::function<float(float)>>> sensor_pairs_;
54 };
55 
57  public:
58  void dump_config() override;
59  void setup() override;
60 
61  void set_process_std_dev(float process_std_dev) {
62  this->update_variance_value_ = process_std_dev * process_std_dev * 0.001f;
63  }
64  void set_std_dev_sensor(Sensor *sensor) { this->std_dev_sensor_ = sensor; }
65 
66  protected:
67  void update_variance_();
68  void correct_(float value, float stddev);
69 
70  // Optional sensor for publishing the current error
71  sensor::Sensor *std_dev_sensor_{nullptr};
72 
73  // Tick of the last update
74  uint32_t last_update_{0};
75  // Change of the variance, per ms
76  float update_variance_value_{0.f};
77 
78  // Best guess for the state and its variance
79  float state_{NAN};
80  float variance_{INFINITY};
81 };
82 
84  public:
85  void dump_config() override { this->log_config_(LOG_STR("linear")); }
86  void setup() override;
87 
88  void handle_new_value(float value);
89 };
90 
92  public:
93  void dump_config() override { this->log_config_(LOG_STR("max")); }
94 
95  void handle_new_value(float value) override;
96 };
97 
99  public:
100  void dump_config() override { this->log_config_(LOG_STR("mean")); }
101 
102  void handle_new_value(float value) override;
103 };
104 
106  public:
107  void dump_config() override { this->log_config_(LOG_STR("median")); }
108 
109  void handle_new_value(float value) override;
110 };
111 
113  public:
114  void dump_config() override { this->log_config_(LOG_STR("min")); }
115 
116  void handle_new_value(float value) override;
117 };
118 
120  public:
121  void dump_config() override { this->log_config_(LOG_STR("most_recently_updated")); }
122 
123  void handle_new_value(float value) override;
124 };
125 
127  public:
128  void dump_config() override { this->log_config_(LOG_STR("range")); }
129 
130  void handle_new_value(float value) override;
131 };
132 
134  public:
135  void dump_config() override { this->log_config_(LOG_STR("sum")); }
136 
137  void handle_new_value(float value) override;
138 };
139 
140 } // namespace combination
141 } // namespace esphome
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:19
std::vector< std::pair< Sensor *, std::function< float(float)> > > sensor_pairs_
Definition: combination.h:53
void log_config_(const LogString *combo_type)
Logs the sensor for use in dump_config.
Definition: combination.cpp:15
void set_process_std_dev(float process_std_dev)
Definition: combination.h:61
virtual void dump_config()
Definition: component.cpp:186
virtual void setup()
Where the component&#39;s initialization should happen.
Definition: component.cpp:48
float get_setup_priority() const override
Definition: combination.h:13
Base class for operations that do not require an extra parameter to compute the combination.
Definition: combination.h:25
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
Base-class for all sensors.
Definition: sensor.h:57
esphome::sensor::Sensor * sensor
Definition: statsd.h:38
virtual void log_source_sensors()=0
Logs all source sensor&#39;s names.