ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
integration_sensor.cpp
Go to the documentation of this file.
2#include "esphome/core/hal.h"
4#include "esphome/core/log.h"
5
7
8static const char *const TAG = "integration";
9
11 if (this->restore_) {
13 float preference_value = 0;
14 this->pref_.load(&preference_value);
15 this->result_ = preference_value;
16 }
17
18 this->last_update_ = millis();
19
20 this->publish_and_save_(this->result_);
21 this->sensor_->add_on_state_callback([this](float state) { this->process_sensor_value_(state); });
22}
23void IntegrationSensor::dump_config() { LOG_SENSOR("", "Integration Sensor", this); }
25 if (std::isnan(value))
26 return;
27 const uint32_t now = millis();
28 const double old_value = this->last_value_;
29 const double new_value = value;
30 const uint32_t dt_ms = now - this->last_update_;
31 const double dt = dt_ms * this->get_time_factor_();
32 double area = 0.0f;
33 switch (this->method_) {
35 area = dt * (old_value + new_value) / 2.0;
36 break;
38 area = dt * old_value;
39 break;
41 area = dt * new_value;
42 break;
43 }
44 this->last_value_ = new_value;
45 this->last_update_ = now;
46 this->publish_and_save_(this->result_ + area);
47}
48
49} // namespace esphome::integration
ESPPreferenceObject make_entity_preference(uint32_t version=0)
Create a preference object for storing this entity's state/settings.
void add_on_state_callback(F &&callback)
Add a callback that will be called every time a filtered value arrives.
Definition sensor.h:119
float state
This member variable stores the last state that has passed through all filters.
Definition sensor.h:138
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:28
static void uint32_t