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:
Otto Winter 2019-10-17 21:35:31 +02:00 committed by GitHub
parent 32195f77d9
commit 9a40d6ef50
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 8 deletions

View file

@ -45,14 +45,14 @@ std::string IntegrationSensor::unit_of_measurement() {
}
void IntegrationSensor::process_sensor_value_(float value) {
const uint32_t now = millis();
const float old_value = this->last_value_;
const float new_value = value;
const double old_value = this->last_value_;
const double new_value = value;
const uint32_t dt_ms = now - this->last_update_;
const float dt = dt_ms * this->get_time_factor_();
float area = 0.0f;
const double dt = dt_ms * this->get_time_factor_();
double area = 0.0f;
switch (this->method_) {
case INTEGRATION_METHOD_TRAPEZOID:
area = dt * (old_value + new_value) / 2.0f;
area = dt * (old_value + new_value) / 2.0;
break;
case INTEGRATION_METHOD_LEFT:
area = dt * old_value;

View file

@ -51,10 +51,11 @@ class IntegrationSensor : public sensor::Sensor, public Component {
return 0.0f;
}
}
void publish_and_save_(float result) {
void publish_and_save_(double result) {
this->result_ = 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 icon() override { return this->sensor_->get_icon(); }
@ -67,7 +68,7 @@ class IntegrationSensor : public sensor::Sensor, public Component {
ESPPreferenceObject rtc_;
uint32_t last_update_;
float result_{0.0f};
double result_{0.0f};
float last_value_{0.0f};
};