85#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERY_VERBOSE
95 if (header1 == 0xAA) {
96 ESP_LOGE(TAG,
"CSE7766 not calibrated!");
100 bool power_cycle_exceeds_range =
false;
101 if ((header1 & 0xF0) == 0xF0) {
103 ESP_LOGE(TAG,
"CSE7766 reports abnormal external circuit or chip damage: (0x%02X)", header1);
104 if (header1 & (1 << 3)) {
105 ESP_LOGE(TAG,
" Voltage cycle exceeds range.");
107 if (header1 & (1 << 2)) {
108 ESP_LOGE(TAG,
" Current cycle exceeds range.");
110 if (header1 & (1 << 0)) {
111 ESP_LOGE(TAG,
" Coefficient storage area is abnormal.");
118 power_cycle_exceeds_range = header1 & (1 << 1);
131 bool have_power = adj & 0x10;
132 bool have_current = adj & 0x20;
133 bool have_voltage = adj & 0x40;
135 float voltage = 0.0f;
137 voltage = voltage_coeff / float(voltage_cycle);
150 this->cf_pulses_last_ = cf_pulses;
151 energy = this->
cf_pulses_total_ * float(power_coeff) / 1000000.0f / 3600.0f;
156 if (power_cycle_exceeds_range) {
162 }
else if (have_power) {
163 power = power_coeff / float(power_cycle);
173 float current = 0.0f;
174 float calculated_current = 0.0f;
177 if (have_power && voltage > 1.0f) {
178 calculated_current = power / voltage;
181 if (calculated_current > 0.05f) {
182 current = current_coeff / float(current_cycle);
189 if (have_voltage && have_current) {
190 const float apparent_power = voltage * current;
195 const float reactive_power = apparent_power - power;
196 if (reactive_power < 0.0f) {
197 ESP_LOGD(TAG,
"Impossible reactive power: %.4f is negative", reactive_power);
205 if (apparent_power > 0) {
206 pf = power / apparent_power;
207 if (pf < 0 || pf > 1) {
208 ESP_LOGD(TAG,
"Impossible power factor: %.4f not in interval [0, 1]", pf);
211 }
else if (apparent_power == 0 && power == 0) {
214 }
else if (current == 0 && calculated_current <= 0.05f) {
216 ESP_LOGV(TAG,
"Can't calculate power factor (current below minimum for CSE7766)");
218 ESP_LOGW(TAG,
"Can't calculate power factor from P = %.4f W, S = %.4f VA", power, apparent_power);
224#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERY_VERBOSE
229 size_t pos = buf_append_printf(buf,
sizeof(buf), 0,
"Parsed:");
231 pos = buf_append_printf(buf,
sizeof(buf),
pos,
" V=%.4fV", voltage);
234 pos = buf_append_printf(buf,
sizeof(buf),
pos,
" I=%.4fmA (~%.4fmA)", current * 1000.0f,
235 calculated_current * 1000.0f);
238 pos = buf_append_printf(buf,
sizeof(buf),
pos,
" P=%.4fW", power);
240 if (energy != 0.0f) {
241 buf_append_printf(buf,
sizeof(buf),
pos,
" E=%.4fkWh (%u)", energy, cf_pulses);
243 ESP_LOGVV(TAG,
"%s", buf);