Esphome Pzem004t Zero Reading

I have a Esp8266 running esphome connected to two PZEM004T v3 reading the parameters of two lines. The entire setup has a power backup so that even when the lines which are read by the pzemoo4t are offline, the esp8266 setup is powered.

The system work fine expect the following issue. When there is a power cut, instead of reading the voltage and other parameters as zero, the device maintains the last non zero reading.

i,e for example if the voltage was 220 when the power was cut, instead of updating voltage to 0v, it would just maintain 220v.

How can I solve this to make sure that when there is a power cut, zero values are reported?

  - platform: pzemac
    address: 1
    modbus_id: mbus
      name: "I1-Current"
      name: "V1-Voltage"
      name: "E1-Energy"
      id: l1_energy
      accuracy_decimals: 4
      internal: true
      - lambda: return x * 0.001;
      unit_of_measurement: kWh
      name: "P1-Power"
      name: "L1 Frequency"
      internal: true
      name: "L1 Power Factor"
      internal: true
    update_interval: 10s

Ever find a solution? I have the same issue.

If im correct the PZEM004 is powered by mains and not the esp, it’s optocoupled on the communication lines between ESP and PZEM004.
So if power is cut then the PZEM004 is off and the last value will be the last value ESP received.
I think I have seen someone doing mods on the PZEM004 to be powered up by the 5v from espmodule or some thing.

edit: Think I was wrong, How to use PZEM004T Energy Monitor with esphome
I have not read all, but looks like it was more that the PZEM004 uses 5volt and ESP uses 3.3volts.

But does anything get logged, errors etc when the power is off?

My PZEM004 and ESP has AC disconnected simultaneously. All sensor entities continue to report last value except an online state which switches from “connected” to “disconnected”, so I can test for this event, but when “disconnected” I need all the other entities - voltage, current, energy, power to read zero. I don’t know how to do this… Any help is appreciated.

How about doing a template for each part?

So add id on the connected sensor (called isconnected in the example below) and on each of the sensors you want to modify.
Then create a platform: template under sensor.
This template sensor will be the one you use in home assistant etc.

The Lambda will check the state if id “isconnected” (change to what ever ID you set) state equals to “connected” (change to what ever state is connected for you).
If true then it will return the state of the id “voltage”. else it returns 0

p.s. remove name or add internal: true on the original sensors if want only use the templates. or you will get double entities in home assistant.

- platform: template
    name: Voltage
    lambda: !lambda |-
      if (id(isconnected).state == 'connected') {
        return id(voltage).state;
      } else {
        return 0;
- platform: template
    name: Current
    lambda: !lambda |-
      if (id(isconnected).state == 'connected') {
        return id(current).state;
      } else {
        return 0;

I have not tested this :slight_smile: so anything can happend!

Thank you for your suggestion. I assume this code is loaded into as part of the ESP configuration, not as a HA template? If so, it depends on the ESP remaining powered when the PZEM is unpowered. In my current set up both are powered together and when offline, HA does provide a state of “disconnected” for the ESP. I’m a novice, so forgive me if I’m being unclear.

Ok, the OP has its esphome connected to backup power making it still run and be active while the power is off.
I thought you had the same setup.

In home assistant you can add template sensors too. I have never used them yet… but looking at the docs:

Maybe something like this can work, again not tested so not sure if this will work.
But here you will create a template sensor called New Voltage, the state will be 0 if the state of sensor.voltage (replace with the esp sensor name used) is disconnected, else it will take the state of the esphome sensor.

  - sensor:
      - name: "New Voltage"
        state: >
          {% if is_state('sensor.voltage', 'disconnected') %}
          {% else %}
            {{ states.sensor.voltage }}
          {% endif %}

If you have attributes on the espsensor you can also get them by using
state_attr('sensor.voltage','attribute') instead of states.sensor.voltage
change attribute with one availabel for that sensor.
You can see what attributes you have available by checking the developer-tools > state page on home assistant.

Thank you so much for your response! I will try this…

Tried this:

  - sensor:
      - name: "frglivinvtst"
        state: >
          {% if is_state('sensor.frglivinv_power', 'unavailable') %}
          {% else %}
             {{ states sensor.frglivinv_power }}
          {% endif %} 

It always returns zero, even when sensor.frgliv_power is available and non zero. I don’t understand.why.


there is a missing dot between states and sensor, might be that?

Found that we should avoid using states.sensors… etc to avoid errors and use the following instead for best practice:
try this instead

That helped! Thank you. Now it produces the below if frglivinv_power is non zero, and 0 if it is unavailable, but I need just the value of sensor.frglivinv_power (here=454) to be reported so that I can plot it, not the whole string…

<template TemplateState(<state sensor.frglivinv_power=454; state_class=measurement, unit_of_measurement=W, device_class=power, friendly_name=frglivinv Power @ 2022-12-09T09:38:15.232484-08:00>)

yes, so two ways

use states.sensor.frglivinv_power.state (as states.sensor.frglivinv_power is a object not a value, so the end .state will get just the state and not the complete object)
or use this better way: states('sensor.frglivinv_power')

you can read more about this here or for anybody else

Thank you so much for your help, this now works!!! I wish I could find a good book on HA. The help you point to is useful, but a good book would be great. Thank you again!

perfect, good job! Now go and experiment with more sensors and switches :slight_smile:

1 Like