mirror of
https://github.com/esphome/esphome.git
synced 2024-12-20 16:18:49 +01:00
Integration sensor use double precision (#715)
Fixes https://github.com/esphome/issues/issues/534 Kept the RTC value as a float in order not to introduce a breaking preferences change.
This commit is contained in:
parent
32195f77d9
commit
9a40d6ef50
@ -45,14 +45,14 @@ std::string IntegrationSensor::unit_of_measurement() {
|
|||||||
}
|
}
|
||||||
void IntegrationSensor::process_sensor_value_(float value) {
|
void IntegrationSensor::process_sensor_value_(float value) {
|
||||||
const uint32_t now = millis();
|
const uint32_t now = millis();
|
||||||
const float old_value = this->last_value_;
|
const double old_value = this->last_value_;
|
||||||
const float new_value = value;
|
const double new_value = value;
|
||||||
const uint32_t dt_ms = now - this->last_update_;
|
const uint32_t dt_ms = now - this->last_update_;
|
||||||
const float dt = dt_ms * this->get_time_factor_();
|
const double dt = dt_ms * this->get_time_factor_();
|
||||||
float area = 0.0f;
|
double area = 0.0f;
|
||||||
switch (this->method_) {
|
switch (this->method_) {
|
||||||
case INTEGRATION_METHOD_TRAPEZOID:
|
case INTEGRATION_METHOD_TRAPEZOID:
|
||||||
area = dt * (old_value + new_value) / 2.0f;
|
area = dt * (old_value + new_value) / 2.0;
|
||||||
break;
|
break;
|
||||||
case INTEGRATION_METHOD_LEFT:
|
case INTEGRATION_METHOD_LEFT:
|
||||||
area = dt * old_value;
|
area = dt * old_value;
|
||||||
|
@ -51,10 +51,11 @@ class IntegrationSensor : public sensor::Sensor, public Component {
|
|||||||
return 0.0f;
|
return 0.0f;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void publish_and_save_(float result) {
|
void publish_and_save_(double result) {
|
||||||
this->result_ = result;
|
this->result_ = result;
|
||||||
this->publish_state(result);
|
this->publish_state(result);
|
||||||
this->rtc_.save(&result);
|
float result_f = result;
|
||||||
|
this->rtc_.save(&result_f);
|
||||||
}
|
}
|
||||||
std::string unit_of_measurement() override;
|
std::string unit_of_measurement() override;
|
||||||
std::string icon() override { return this->sensor_->get_icon(); }
|
std::string icon() override { return this->sensor_->get_icon(); }
|
||||||
@ -67,7 +68,7 @@ class IntegrationSensor : public sensor::Sensor, public Component {
|
|||||||
ESPPreferenceObject rtc_;
|
ESPPreferenceObject rtc_;
|
||||||
|
|
||||||
uint32_t last_update_;
|
uint32_t last_update_;
|
||||||
float result_{0.0f};
|
double result_{0.0f};
|
||||||
float last_value_{0.0f};
|
float last_value_{0.0f};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user