Power meter pzem-004t connected via usb/ttl

Hi comrades.
can you advice, please, how to integrate serial sensor/meter (pzem-004t). Normally it shall be a message of special format sent to /dev/ttyUSB0 periodically. Certain bytes in feedback/msg shall be used to evaluate result.

Which configuration primitive should be used to define command format and feedback/translation algorithm?

Thank you

OK. I finally did it. Here’s a solution:

create new custom component (i named it pzem004):

  • new folder /config/custom_components/pzem004
  • file init.py
DOMAIN = 'pzem004'
async def async_setup(hass, config):
    return True
  • file manifest.json (it is only needed to be able to use pyserial)
{
  "domain": "pzem004",
  "name": "pzem004",
  "documentation": "https://www.home-assistant.io/",
  "dependencies": [],
  "codeowners": [],
  "requirements": ["pyserial"]
}
  • python script /config/script_power_meter.py
import serial
from binascii import unhexlify
import sys

port = "/dev/ttyUSB0"
baud = 9600

if (len(sys.argv)<=1):
    sys.exit()

is_voltage=False
is_current=False
is_active=False
is_energy=False

for arg in sys.argv:
    if (arg=="voltage"):
        is_voltage=True
    elif (arg=="current"):
        is_current=True
    elif (arg=="active"):
        is_active=True
    elif (arg=="energy"):
        is_energy=True

ser = serial.Serial(port, baud, timeout=1)
    # open the serial port

if ser.isOpen():

    voltage_str = "B0C0A80101001A"
    current_str = "B1C0A80101001B"
    active_str = "B2C0A80101001C"
    energy_str = "B3C0A80101001D"

    if (is_voltage):
        ser.write(unhexlify(voltage_str))
        out = ser.readall()
        print (out[1]*256+out[2] + out[3]*0.1)

    if (is_current):
        ser.write(unhexlify(current_str))
        out = ser.readall()
        print (out[2] + out[3]*0.01)

    if (is_active):
        ser.write(unhexlify(active_str))
        out = ser.readall()
        print (out[1]*256+out[2])

    if (is_energy):
        ser.write(unhexlify(energy_str))
        out = ser.readall()
        print (out[1]*256*256+out[2]*256+out[3])

    ser.close()
  • add following items to /config/configuration.yaml
pzem004:

sensor pzem004-current:
  - platform: command_line
    name: pzem004-current
    command: python3 script_power_meter.py current
    unit_of_measurement: "A"

sensor pzem004-voltage:
  - platform: command_line
    name: pzem004-voltage
    command: python3 script_power_meter.py voltage
    unit_of_measurement: "V"
  • reboot and add new sensors “pzem004-voltage” and “pzem004-current” on home assistant web page
1 Like

Hi, l know that there are 2 versions of Pzem004: the oldest with normal serial communication and the newest v.3 that use modbus protocol. Wich version are you using?
Did you use a serial ttl to usb converter?

Hi
it’s older/serial version. I am using serial/ttl usb connector (please, note that ttl/rx shall be connected to pzem/tx and vice versa)

1 Like

Ok, thank you. I have the new version PZEM 004 v.3 modbus (not yet used) Pzem-004T.v3.0 and i’m wondering 2 questions (i’m new on raspberry and in programming):

  1. is it possible to connect the new PZEM modbus version to Raspberry (using an usb dongle) and simply use HA modbus integration (https://www.home-assistant.io/integrations/modbus/) without any extra code?
  2. if point 1 is feasible, is it possible to connect several PZEM modbus to only one USB dongle? I should connect 6-9 of them to my HA to check a three phase system…

I’ve a question on how you connect the reference wires to live and neutral on the pzem-016… I was thinking I’d just take the live and neutral from a power socket next to my utility board… or do I specifically have to take the live from before all the loads and the neutral from after all the loads?