86#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERY_VERBOSE
96 if (header1 == 0xAA) {
97 ESP_LOGE(TAG,
"CSE7766 not calibrated!");
101 bool power_cycle_exceeds_range =
false;
102 if ((header1 & 0xF0) == 0xF0) {
104 ESP_LOGE(TAG,
"CSE7766 reports abnormal external circuit or chip damage: (0x%02X)", header1);
105 if (header1 & (1 << 3)) {
106 ESP_LOGE(TAG,
" Voltage cycle exceeds range.");
108 if (header1 & (1 << 2)) {
109 ESP_LOGE(TAG,
" Current cycle exceeds range.");
111 if (header1 & (1 << 0)) {
112 ESP_LOGE(TAG,
" Coefficient storage area is abnormal.");
119 power_cycle_exceeds_range = header1 & (1 << 1);
132 bool have_power = adj & 0x10;
133 bool have_current = adj & 0x20;
134 bool have_voltage = adj & 0x40;
136 float voltage = 0.0f;
138 voltage = voltage_coeff / float(voltage_cycle);
151 this->cf_pulses_last_ = cf_pulses;
152 energy = this->
cf_pulses_total_ * float(power_coeff) / 1000000.0f / 3600.0f;
157 if (power_cycle_exceeds_range) {
164 }
else if (have_power) {
165 power = power_coeff / float(power_cycle);
176 power = power_coeff / float(this->last_power_cycle_);
183 this->last_power_cycle_ = 0;
189 float current = 0.0f;
190 float calculated_current = 0.0f;
193 if (have_power && voltage > 1.0f) {
194 calculated_current = power / voltage;
197 if (calculated_current > 0.05f) {
198 current = current_coeff / float(current_cycle);
205 if (have_voltage && have_current) {
206 const float apparent_power = voltage * current;
211 const float q_squared = apparent_power * apparent_power - power * power;
212 if (q_squared < 0.0f) {
213 ESP_LOGD(TAG,
"Impossible reactive power: S^2-P^2 is negative (%.4f)", q_squared);
221 if (apparent_power > 0) {
222 pf = power / apparent_power;
225 ESP_LOGD(TAG,
"Power factor %.4f above 1, clamped", pf);
228 }
else if (apparent_power == 0 && power == 0) {
231 }
else if (current == 0 && calculated_current <= 0.05f) {
233 ESP_LOGV(TAG,
"Can't calculate power factor (current below minimum for CSE7766)");
235 ESP_LOGW(TAG,
"Can't calculate power factor from P = %.4f W, S = %.4f VA", power, apparent_power);
241#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERY_VERBOSE
246 size_t pos = buf_append_printf(buf,
sizeof(buf), 0,
"Parsed:");
248 pos = buf_append_printf(buf,
sizeof(buf),
pos,
" V=%.4fV", voltage);
251 pos = buf_append_printf(buf,
sizeof(buf),
pos,
" I=%.4fmA (~%.4fmA)", current * 1000.0f,
252 calculated_current * 1000.0f);
255 pos = buf_append_printf(buf,
sizeof(buf),
pos,
" P=%.4fW", power);
257 if (energy != 0.0f) {
258 buf_append_printf(buf,
sizeof(buf),
pos,
" E=%.4fkWh (%u)", energy, cf_pulses);
260 ESP_LOGVV(TAG,
"%s", buf);