mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-30 22:53:59 +00: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:
		| @@ -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; | ||||
|   | ||||
| @@ -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}; | ||||
| }; | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user