diff --git a/esphome/components/cse7761/cse7761.cpp b/esphome/components/cse7761/cse7761.cpp index 7525b901f8..0ecaaced7f 100644 --- a/esphome/components/cse7761/cse7761.cpp +++ b/esphome/components/cse7761/cse7761.cpp @@ -204,24 +204,27 @@ void CSE7761Component::get_data_() { value = this->read_(CSE7761_REG_RMSIA, 3); this->data_.current_rms[0] = ((value >= 0x800000) || (value < 1600)) ? 0 : value; // No load threshold of 10mA value = this->read_(CSE7761_REG_POWERPA, 4); - this->data_.active_power[0] = (0 == this->data_.current_rms[0]) ? 0 : ((uint32_t) abs((int) value)); + // PowerPA is two's complement signed 32-bit per datasheet + this->data_.active_power[0] = (0 == this->data_.current_rms[0]) ? 0 : static_cast(value); value = this->read_(CSE7761_REG_RMSIB, 3); this->data_.current_rms[1] = ((value >= 0x800000) || (value < 1600)) ? 0 : value; // No load threshold of 10mA value = this->read_(CSE7761_REG_POWERPB, 4); - this->data_.active_power[1] = (0 == this->data_.current_rms[1]) ? 0 : ((uint32_t) abs((int) value)); + // PowerPB is two's complement signed 32-bit per datasheet + this->data_.active_power[1] = (0 == this->data_.current_rms[1]) ? 0 : static_cast(value); // convert values and publish to sensors - float voltage = (float) this->data_.voltage_rms / this->coefficient_by_unit_(RMS_UC); + float voltage = static_cast(this->data_.voltage_rms) / this->coefficient_by_unit_(RMS_UC); if (this->voltage_sensor_ != nullptr) { this->voltage_sensor_->publish_state(voltage); } for (uint8_t channel = 0; channel < 2; channel++) { // Active power = PowerPA * PowerPAC * 1000 / 0x80000000 - float active_power = (float) this->data_.active_power[channel] / this->coefficient_by_unit_(POWER_PAC); // W - float amps = (float) this->data_.current_rms[channel] / this->coefficient_by_unit_(RMS_IAC); // A + float active_power = + static_cast(this->data_.active_power[channel]) / this->coefficient_by_unit_(POWER_PAC); // W + float amps = static_cast(this->data_.current_rms[channel]) / this->coefficient_by_unit_(RMS_IAC); // A ESP_LOGD(TAG, "Channel %d power %f W, current %f A", channel + 1, active_power, amps); if (channel == 0) { if (this->power_sensor_1_ != nullptr) { diff --git a/esphome/components/cse7761/cse7761.h b/esphome/components/cse7761/cse7761.h index 289c5e7e19..0e03171956 100644 --- a/esphome/components/cse7761/cse7761.h +++ b/esphome/components/cse7761/cse7761.h @@ -11,10 +11,8 @@ struct CSE7761DataStruct { uint32_t frequency = 0; uint32_t voltage_rms = 0; uint32_t current_rms[2] = {0}; - uint32_t energy[2] = {0}; - uint32_t active_power[2] = {0}; + int32_t active_power[2] = {0}; uint16_t coefficient[8] = {0}; - uint8_t energy_update = 0; bool ready = false; };