Pzem-004t readout freeze if no AC - software solution

Hello.
After digging a LOT into the problem, I’ve finally made the SW fix that works.
Problem - using a standard PZEM-004T board, it doesn’t respond when there is no AC connected. There’s a HW fix on the internet (powering “hot” side with DC/DC) - but it’s easier with SW obviously.

I need to monitor grid outages and it’s critical for me now in the place I live in (Ukraine).

Idea :

  • define a analogue of last_seen (it’s not present in ESPHome)
  • add a hearbeat timer and forcibly send a 0 value when timeout passed (AC disaapeared)
  • use uptime sensor to avoid SMTP

Code is as below ( I’ve copy-pasted meaningful parts, please adopt to your yaml):

globals:
   - id: phase_a_voltage_last_ts
     type: int
     restore_value: no
     initial_value: '0'

Now PZEM:

sensor:
  - platform: uptime
    id: grid_input_main_uptime
    update_interval : 1s

  - platform: pzemac
    id: phaze_a
    modbus_id: modbus_1
    
    voltage:
      name: "Phase A Voltage"
      id: phase_a_voltage
      filters:
      - lambda: |-
          id(phase_a_voltage_last_ts) = id(grid_input_main_uptime).state;
          return ((x > 300) || (x < 0)) ? -1 : x;
      - filter_out: -1
      force_update: true

And timer:

interval:
  - interval: 5s
    then:
        lambda: |-
          int tsnow = id(grid_input_main_uptime).state;

          if ((tsnow - id(phase_a_voltage_last_ts)) > 15){
            ESP_LOGD("main", "Phase A timed out!");
            id(phase_a_voltage).publish_state(0);  
            id(phase_a_voltage_last_ts) = tsnow;

            id(phase_a_current).publish_state(0); 
            id(phase_a_power).publish_state(0); 
            }
2 Likes

You could have tried tasmota which doesn’t have this issue. I appreciate the good work. thanks