79#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERY_VERBOSE
89 if (header1 == 0xAA) {
90 ESP_LOGE(TAG,
"CSE7766 not calibrated!");
94 bool power_cycle_exceeds_range =
false;
95 if ((header1 & 0xF0) == 0xF0) {
97 ESP_LOGE(TAG,
"CSE7766 reports abnormal external circuit or chip damage: (0x%02X)", header1);
98 if (header1 & (1 << 3)) {
99 ESP_LOGE(TAG,
" Voltage cycle exceeds range.");
101 if (header1 & (1 << 2)) {
102 ESP_LOGE(TAG,
" Current cycle exceeds range.");
104 if (header1 & (1 << 0)) {
105 ESP_LOGE(TAG,
" Coefficient storage area is abnormal.");
112 power_cycle_exceeds_range = header1 & (1 << 1);
125 bool have_power = adj & 0x10;
126 bool have_current = adj & 0x20;
127 bool have_voltage = adj & 0x40;
129 float voltage = 0.0f;
131 voltage = voltage_coeff / float(voltage_cycle);
132 PUBLISH_STATE(this->voltage_sensor_, voltage);
136 if (this->energy_sensor_ !=
nullptr) {
137 if (this->
cf_pulses_last_ == 0 && !this->energy_sensor_->has_state()) {
142 this->cf_pulses_last_ = cf_pulses;
143 energy = this->
cf_pulses_total_ * float(power_coeff) / 1000000.0f / 3600.0f;
144 this->energy_sensor_->publish_state(energy);
148 if (power_cycle_exceeds_range) {
152 PUBLISH_STATE(this->power_sensor_, 0.0f);
153 }
else if (have_power) {
154 power = power_coeff / float(power_cycle);
156 PUBLISH_STATE(this->power_sensor_, power);
163 power = power_coeff / float(this->last_power_cycle_);
164 PUBLISH_STATE(this->power_sensor_, power);
168 this->last_power_cycle_ = 0;
169 PUBLISH_STATE(this->power_sensor_, 0.0f);
172 float current = 0.0f;
173 float calculated_current = 0.0f;
176 if (have_power && voltage > 1.0f) {
177 calculated_current = power / voltage;
180 if (calculated_current > 0.05f) {
181 current = current_coeff / float(current_cycle);
183 PUBLISH_STATE(this->current_sensor_, current);
186 if (have_voltage && have_current) {
187 const float apparent_power = voltage * current;
188 PUBLISH_STATE(this->apparent_power_sensor_, apparent_power);
189 if (have_power && this->reactive_power_sensor_ !=
nullptr) {
190 const float q_squared = apparent_power * apparent_power - power * power;
191 if (q_squared < 0.0f) {
193 ESP_LOGV(TAG,
"Impossible reactive power: S^2-P^2 is negative (%.4f)", q_squared);
194 this->reactive_power_sensor_->publish_state(0.0f);
196 this->reactive_power_sensor_->publish_state(std::sqrt(q_squared));
199 if (this->power_factor_sensor_ !=
nullptr && (have_power || power_cycle_exceeds_range)) {
201 if (apparent_power > 0) {
202 pf = power / apparent_power;
206 ESP_LOGV(TAG,
"Power factor %.4f above 1, clamped", pf);
209 }
else if (apparent_power == 0 && power == 0) {
212 }
else if (current == 0 && calculated_current <= 0.05f) {
214 ESP_LOGV(TAG,
"Can't calculate power factor (current below minimum for CSE7766)");
216 ESP_LOGW(TAG,
"Can't calculate power factor from P = %.4f W, S = %.4f VA", power, apparent_power);
218 this->power_factor_sensor_->publish_state(pf);
222#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERY_VERBOSE
227 size_t pos = buf_append_printf(buf,
sizeof(buf), 0,
"Parsed:");
229 pos = buf_append_printf(buf,
sizeof(buf),
pos,
" V=%.4fV", voltage);
232 pos = buf_append_printf(buf,
sizeof(buf),
pos,
" I=%.4fmA (~%.4fmA)", current * 1000.0f,
233 calculated_current * 1000.0f);
236 pos = buf_append_printf(buf,
sizeof(buf),
pos,
" P=%.4fW", power);
238 if (energy != 0.0f) {
239 buf_append_printf(buf,
sizeof(buf),
pos,
" E=%.4fkWh (%u)", energy, cf_pulses);
241 ESP_LOGVV(TAG,
"%s", buf);