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?
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.
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.
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.
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.
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.
there is a missing dot between states and sensor, might be that?
Edit:
Found that we should avoid using states.sensors… etc to avoid errors and use the following instead for best practice: states('sensor.frglivinv_power')
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>)
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!