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) {
163 }
else if (have_power) {
164 power = power_coeff / float(power_cycle);
174 float current = 0.0f;
175 float calculated_current = 0.0f;
178 if (have_power && voltage > 1.0f) {
179 calculated_current = power / voltage;
182 if (calculated_current > 0.05f) {
183 current = current_coeff / float(current_cycle);
190 if (have_voltage && have_current) {
191 const float apparent_power = voltage * current;
196 const float reactive_power = apparent_power - power;
197 if (reactive_power < 0.0f) {
198 ESP_LOGD(TAG,
"Impossible reactive power: %.4f is negative", reactive_power);
206 if (apparent_power > 0) {
207 pf = power / apparent_power;
208 if (pf < 0 || pf > 1) {
209 ESP_LOGD(TAG,
"Impossible power factor: %.4f not in interval [0, 1]", pf);
212 }
else if (apparent_power == 0 && power == 0) {
215 }
else if (current == 0 && calculated_current <= 0.05f) {
217 ESP_LOGV(TAG,
"Can't calculate power factor (current below minimum for CSE7766)");
219 ESP_LOGW(TAG,
"Can't calculate power factor from P = %.4f W, S = %.4f VA", power, apparent_power);
225#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERY_VERBOSE
230 size_t pos = buf_append_printf(buf,
sizeof(buf), 0,
"Parsed:");
232 pos = buf_append_printf(buf,
sizeof(buf),
pos,
" V=%.4fV", voltage);
235 pos = buf_append_printf(buf,
sizeof(buf),
pos,
" I=%.4fmA (~%.4fmA)", current * 1000.0f,
236 calculated_current * 1000.0f);
239 pos = buf_append_printf(buf,
sizeof(buf),
pos,
" P=%.4fW", power);
241 if (energy != 0.0f) {
242 buf_append_printf(buf,
sizeof(buf),
pos,
" E=%.4fkWh (%u)", energy, cf_pulses);
244 ESP_LOGVV(TAG,
"%s", buf);