10static const char *
const TAG =
"combination";
13 LOG_SENSOR(
"",
"Combination Sensor:",
this);
14 ESP_LOGCONFIG(TAG,
" Combination Type: %s", LOG_STR_ARG(combo_type));
25 this->
add_source(sensor, std::function<
float(
float)>{[value](
float x) ->
float {
return value; }});
29 ESP_LOGCONFIG(TAG,
" Source Sensors:");
30 for (
const auto &sensor : this->
sensors_) {
31 ESP_LOGCONFIG(TAG,
" - %s", sensor->get_name().c_str());
36 ESP_LOGCONFIG(TAG,
" Source Sensors:");
38 ESP_LOGCONFIG(TAG,
" - %s", source.sensor->get_name().c_str());
43 for (
const auto &sensor : this->
sensors_) {
46 sensor->add_on_state_callback(
47 [
this](
float value) ->
void { this->
defer(
"update", [
this, value]() { this->
handle_new_value(value); }); });
64 source.sensor->add_on_state_callback([&source](
float x) ->
void {
77 this->last_update_ = now;
81 if (std::isnan(value) || std::isinf(stddev)) {
98 const bool this_first = this->
variance_ < (stddev * stddev);
99 const float mu1 = this_first ? this->
state_ : value;
100 const float mu2 = this_first ? value : this->
state_;
102 const float var1 = this_first ? this->
variance_ : stddev * stddev;
103 const float var2 = this_first ? stddev * stddev : this->
variance_;
105 const float mu = mu1 + var1 * (mu2 - mu1) / (var1 + var2);
106 const float var = var1 - (var1 * var1) / (var1 + var2);
122 source.sensor->add_on_state_callback(
123 [
this](
float value) ->
void { this->
defer(
"update", [
this, value]() { this->
handle_new_value(value); }); });
130 if (!std::isfinite(value))
136 const float sensor_state = source.sensor->state;
137 if (std::isfinite(sensor_state)) {
138 sum += sensor_state * source.compute(sensor_state);
146 if (!std::isfinite(value))
149 float max_value = (-1) * std::numeric_limits<float>::infinity();
151 for (
const auto &sensor : this->
sensors_) {
152 if (std::isfinite(sensor->state)) {
153 max_value = std::max(max_value, sensor->state);
161 if (!std::isfinite(value))
167 for (
const auto &sensor : this->
sensors_) {
168 if (std::isfinite(sensor->state)) {
170 sum += sensor->state;
178 float mean = sum / count;
186 if (!std::isfinite(value))
189 std::vector<float> sensor_states;
190 for (
const auto &sensor : this->
sensors_) {
191 if (std::isfinite(sensor->state)) {
192 sensor_states.push_back(sensor->state);
196 sort(sensor_states.begin(), sensor_states.end());
197 size_t sensor_states_size = sensor_states.size();
201 if (sensor_states_size) {
202 if (sensor_states_size % 2) {
204 median = sensor_states[sensor_states_size / 2];
207 median = (sensor_states[sensor_states_size / 2] + sensor_states[sensor_states_size / 2 - 1]) / 2.0;
215 if (!std::isfinite(value))
218 float min_value = std::numeric_limits<float>::infinity();
220 for (
const auto &sensor : this->
sensors_) {
221 if (std::isfinite(sensor->state)) {
222 min_value = std::min(min_value, sensor->state);
234 if (!std::isfinite(value))
237 std::vector<float> sensor_states;
238 for (
const auto &sensor : this->
sensors_) {
239 if (std::isfinite(sensor->state)) {
240 sensor_states.push_back(sensor->state);
244 if (sensor_states.empty()) {
249 sort(sensor_states.begin(), sensor_states.end());
251 float range = sensor_states.back() - sensor_states.front();
256 if (!std::isfinite(value))
260 for (
const auto &sensor : this->
sensors_) {
261 if (std::isfinite(sensor->state)) {
262 sum += sensor->state;
ESPDEPRECATED("Use const char* overload instead. Removed in 2026.7.0", "2026.1.0") void defer(const std voi defer)(const char *name, std::function< void()> &&f)
Defer a callback to the next loop() call.
void log_config_(const LogString *combo_type)
Logs the sensor for use in dump_config.
virtual void log_source_sensors()=0
Logs all source sensor's names.
std::vector< Sensor * > sensors_
void setup() override
Adds a callback to each source sensor.
virtual void handle_new_value(float value)=0
Computes the combination.
void log_source_sensors() override
Logs all source sensor's names in sensors_.
void add_source(Sensor *sensor)
FixedVector< SensorSource > sensor_sources_
void log_source_sensors() override
Logs all source sensors' names in sensor_sources_.
void add_source(Sensor *sensor, std::function< float(float)> const &compute)
float update_variance_value_
void correct_(float value, float stddev)
sensor::Sensor * std_dev_sensor_
void dump_config() override
void handle_new_value(float value)
void handle_new_value(float value) override
void handle_new_value(float value) override
void handle_new_value(float value) override
void handle_new_value(float value) override
void handle_new_value(float value) override
void handle_new_value(float value) override
void publish_state(float state)
Publish a new state to the front-end.
uint32_t IRAM_ATTR HOT millis()