@@ -15,6 +15,7 @@ void PZEMAC::on_modbus_data(const std::vector<uint8_t> &data) {
1515 ESP_LOGW (TAG, " Invalid size for PZEM AC!" );
1616 return ;
1717 }
18+ this ->last_update_time_ = millis ();
1819
1920 // See https://github.com/esphome/feature-requests/issues/49#issuecomment-538636809
2021 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
@@ -46,28 +47,83 @@ void PZEMAC::on_modbus_data(const std::vector<uint8_t> &data) {
4647 uint16_t raw_power_factor = pzem_get_16bit (16 );
4748 float power_factor = raw_power_factor / 100 .0f ;
4849
49- ESP_LOGD (TAG, " PZEM AC: V=%.1f V, I=%.3f A, P=%.1f W, E=%.1f Wh, F=%.1f Hz, PF=%.2f" , voltage, current, active_power,
50- active_energy, frequency, power_factor);
51- if (this ->voltage_sensor_ != nullptr )
52- this ->voltage_sensor_ ->publish_state (voltage);
53- if (this ->current_sensor_ != nullptr )
54- this ->current_sensor_ ->publish_state (current);
55- if (this ->power_sensor_ != nullptr )
56- this ->power_sensor_ ->publish_state (active_power);
57- if (this ->energy_sensor_ != nullptr )
58- this ->energy_sensor_ ->publish_state (active_energy);
59- if (this ->frequency_sensor_ != nullptr )
50+ ESP_LOGD (TAG,
51+ " PZEM AC: Addr 0x%02X, V=%.1f V, I=%.3f A, P=%.1f W, E=%.1f Wh, E(pre)=%.1f Wh, E-E(pre)=%.1f Wh, F=%.1f "
52+ " Hz, PF=%.2f" ,
53+ int (this ->address_ ), voltage, current, active_power, active_energy, this ->last_energy_sensor ,
54+ active_energy - this ->last_energy_sensor , frequency, power_factor);
55+ if (this ->voltage_sensor_ != nullptr ) {
56+ if (voltage < 450 ) {
57+ this ->voltage_sensor_ ->publish_state (voltage);
58+ } else {
59+ this ->voltage_sensor_ ->publish_state (220 .0f );
60+ }
61+ }
62+ if (this ->current_sensor_ != nullptr ) {
63+ if (current < 150 ) {
64+ this ->current_sensor_ ->publish_state (current);
65+ } else {
66+ this ->current_sensor_ ->publish_state (1 .0f );
67+ }
68+ }
69+ if (this ->power_sensor_ != nullptr ) {
70+ if (active_power < 16000 ) {
71+ this ->power_sensor_ ->publish_state (active_power);
72+ } else {
73+ this ->power_sensor_ ->publish_state (101 .0f );
74+ }
75+ }
76+ if (this ->energy_sensor_ != nullptr ) {
77+ if (this ->last_energy_sensor == 0 ) {
78+ this ->energy_sensor_ ->publish_state (active_energy);
79+ this ->last_energy_sensor = active_energy;
80+ } else {
81+ if (abs (active_energy - this ->last_energy_sensor ) < 1000 ) {
82+ this ->energy_sensor_ ->publish_state (active_energy);
83+ this ->last_energy_sensor = active_energy;
84+ } else {
85+ this ->energy_sensor_ ->publish_state (this ->last_energy_sensor );
86+ }
87+ }
88+ }
89+ if (this ->frequency_sensor_ != nullptr ) {
6090 this ->frequency_sensor_ ->publish_state (frequency);
61- if (this ->power_factor_sensor_ != nullptr )
91+ }
92+ if (this ->power_factor_sensor_ != nullptr ) {
6293 this ->power_factor_sensor_ ->publish_state (power_factor);
94+ }
95+ }
96+
97+ void PZEMAC::update () {
98+ this ->send (PZEM_CMD_READ_IN_REGISTERS, 0 , PZEM_REGISTER_COUNT);
99+
100+ if (this ->get_update_interval () != SCHEDULER_DONT_RUN &&
101+ (millis () - this ->last_update_time_ ) > this ->get_update_interval () * 2 ) {
102+ ESP_LOGE (TAG, " PZEM AC Addr 0x%02X: Timeout!!!" , int (this ->address_ ));
103+ if (this ->voltage_sensor_ != nullptr ) {
104+ this ->voltage_sensor_ ->publish_state (0 .0f );
105+ }
106+ if (this ->current_sensor_ != nullptr ) {
107+ this ->current_sensor_ ->publish_state (0 .0f );
108+ }
109+ if (this ->power_sensor_ != nullptr ) {
110+ this ->power_sensor_ ->publish_state (0 .0f );
111+ }
112+ if (this ->energy_sensor_ != nullptr ) {
113+ this ->energy_sensor_ ->publish_state (this ->last_energy_sensor );
114+ }
115+ if (this ->frequency_sensor_ != nullptr ) {
116+ this ->frequency_sensor_ ->publish_state (0 .0f );
117+ }
118+ if (this ->power_factor_sensor_ != nullptr ) {
119+ this ->power_factor_sensor_ ->publish_state (0 .0f );
120+ }
121+ }
63122}
64123
65- void PZEMAC::update () { this ->send (PZEM_CMD_READ_IN_REGISTERS, 0 , PZEM_REGISTER_COUNT); }
66124void PZEMAC::dump_config () {
67- ESP_LOGCONFIG (TAG,
68- " PZEMAC:\n "
69- " Address: 0x%02X" ,
70- this ->address_ );
125+ ESP_LOGCONFIG (TAG, " PZEMAC:" );
126+ ESP_LOGCONFIG (TAG, " Address: 0x%02X" , this ->address_ );
71127 LOG_SENSOR (" " , " Voltage" , this ->voltage_sensor_ );
72128 LOG_SENSOR (" " , " Current" , this ->current_sensor_ );
73129 LOG_SENSOR (" " , " Power" , this ->power_sensor_ );
0 commit comments