12static constexpr uint32_t NEW_VALUE_DEFER_ID = 0;
15 LOG_SENSOR(
"",
"Combination Sensor:",
this);
16 ESP_LOGCONFIG(TAG,
" Combination Type: %s", LOG_STR_ARG(combo_type));
31 ESP_LOGCONFIG(TAG,
" Source Sensors:");
32 for (
const auto &sensor : this->
sensors_) {
33 ESP_LOGCONFIG(TAG,
" - %s", LOG_STR_ARG(sensor->get_log_name()));
38 ESP_LOGCONFIG(TAG,
" Source Sensors:");
40 ESP_LOGCONFIG(TAG,
" - %s", LOG_STR_ARG(source.sensor->get_log_name()));
45 for (
const auto &sensor : this->
sensors_) {
48 sensor->add_on_state_callback([
this](
float value) ->
void {
58 if (this->std_dev_sensor_ !=
nullptr) {
59 LOG_SENSOR(
" ",
"Standard Deviation Sensor:", this->std_dev_sensor_);
63void KalmanCombinationComponent::setup() {
67 source.sensor->add_on_state_callback([&source](
float x) ->
void {
68 static_cast<KalmanCombinationComponent *
>(source.parent)->
correct_(
x, source.compute(
x));
80 this->last_update_ = now;
84 if (std::isnan(
value) || std::isinf(stddev)) {
91 PUBLISH_STATE(this->std_dev_sensor_, stddev);
99 const bool this_first = this->
variance_ < (stddev * stddev);
100 const float mu1 = this_first ? this->
state_ :
value;
101 const float mu2 = this_first ?
value : this->
state_;
103 const float var1 = this_first ? this->
variance_ : stddev * stddev;
104 const float var2 = this_first ? stddev * stddev : this->
variance_;
106 const float mu = mu1 + var1 * (mu2 - mu1) / (var1 + var2);
107 const float var = var1 - (var1 * var1) / (var1 + var2);
114 PUBLISH_STATE(this->std_dev_sensor_, std::sqrt(var));
118 for (
auto &source : this->sensor_sources_) {
121 source.sensor->add_on_state_callback([
this](
float value) ->
void {
130 if (!std::isfinite(
value))
135 for (
const auto &source : this->sensor_sources_) {
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.0f;
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;
virtual void dump_config()
void defer(const char *name, std::function< void()> &&f)
Defer a callback to the next loop() call with a const char* name.
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)
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.
ESPHOME_LOG_TAG(TAG, "combination")
uint32_t IRAM_ATTR HOT millis()