Fix negative sqrt root in ct_clamp_sensor.cpp (#2701) (#4236)

Co-authored-by: Jerome <jerome992@internet.lu>
This commit is contained in:
jerome992 2023-03-20 05:22:22 +01:00 committed by GitHub
parent d98d6ff45f
commit cd57469e06
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -33,7 +33,10 @@ void CTClampSensor::update() {
const float rms_ac_dc_squared = this->sample_squared_sum_ / this->num_samples_; const float rms_ac_dc_squared = this->sample_squared_sum_ / this->num_samples_;
const float rms_dc = this->sample_sum_ / this->num_samples_; const float rms_dc = this->sample_sum_ / this->num_samples_;
const float rms_ac = std::sqrt(rms_ac_dc_squared - rms_dc * rms_dc); const float rms_ac_squared = rms_ac_dc_squared - rms_dc * rms_dc;
float rms_ac = 0;
if (rms_ac_squared > 0)
rms_ac = std::sqrt(rms_ac_squared);
ESP_LOGD(TAG, "'%s' - Raw AC Value: %.3fA after %d different samples (%d SPS)", this->name_.c_str(), rms_ac, ESP_LOGD(TAG, "'%s' - Raw AC Value: %.3fA after %d different samples (%d SPS)", this->name_.c_str(), rms_ac,
this->num_samples_, 1000 * this->num_samples_ / this->sample_duration_); this->num_samples_, 1000 * this->num_samples_ / this->sample_duration_);
this->publish_state(rms_ac); this->publish_state(rms_ac);