How to use PZEM004T Energy Monitor with esphome

Instruction how to use PZEM_004T_V3 without ESPHome (native Modbus and module Bluetooth TTL):
https://4pda-to.translate.goog/forum/index.php?showtopic=871505&st=33420&_x_tr_sl=ru&_x_tr_tl=en&_x_tr_hl=uk&_x_tr_pto=wapp#entry123597514

Hello, I just got a pzem04t but I feel like the CT cable is very short. If I solder longer wires will it impact in the measurements?

Pretty sure I had to lengthen mine as well. As long as it’s relatively short, you’ll be fine.

Recently, on advise, I received a PZEM004T-V3 and attached it to a Wemos D1 R2 (in ESPhome). Got it working and it gives pretty expected results.

The official documentation, yes I try to read the RTFM, gives the yaml sketch how to give each PZEM a different UART id. The only reason for this can be only when you attach multiple PZEM to one ESP. However the (perfect) yaml sketch and the master/slave UART requirements are a bit missing in this official documentation.

Yes, you can derive this info from the different PZEM topics but it would be nice if best practice is collected in the official documentation. I am too fresh in this topic to update this myself. Anyone?

I notice some people calibrate were other people use the PZEM as-is. Is there still a need for it with V3?

For which case? To connect one or multiple pzem’s to one UART bus? Feel free to change/edit the docs and ad the “perfect” yaml sketch :muscle:

No, pzem’s come pre-calibrated and I never noticed people try to calibrate this device :thinking: Any source to this? Tasmota docs even claim that they “can’t” be calibrated - in esphome obviously you could filter the results and use something like calibrate_linear but most likely just make the readings worse with this. :raised_hands:

I wish I could guide other people to the perfect multi PZEM and show them what needs to be done in case single master UART or perhaps the other option of using multi UARTs but that is a bridge too far for me at this point in time. True I can copy and paste but not verify yet if it is all valid,

calibration remark is clear, but even for starters like me a dummy remark could be helpful:
“There is no need to calibrate the PZEM, ESP functions like calibrate_linear will not improve results.”

BTW Did some comparing with an old water cooker and the results were pretty in line. I was able to compare the PZEM (1051w) against Electricity Meter Power consumption phase L2 of DSMR (1049w) yesterday which agrees on this calibration (the cooker itself was 900w but likely the build up whitewash (kalk) residue changed this).

At this point I also not able to improve my github skills and update the documentation because I am on the edge of going on holiday and my girlfriend will drag me from my PC any moment.

Thanks, I did it and it seems working fine.

Another question, if I connect the L and N reversed on the pzem (the 100A version), will it damage it or affect measurements? Or it doesn’t matter?

On the CT? No, it doesn’t matter as it’s AC voltage so it’s ‘going both directions’.

No, I don’t mean the CT. I mean the actual Live and Neutral wires that connect to the pzem. Does it matter or could the be connected indistinctly?

I mean it PROBABLY won’t matter for the same reason, but I would definitely connect it properly. What reason would there be to not do it right?

I see diagrams over internet and the connections of Live and Neutral differ. Here is the one that comes with the manual and here one diagram from tasmota.

I am powering the pzem and esp32 from AC similar to this. So the charger supplies 5V to the esp32, then the esp32 supply 5V to the pzem TTL and at the same time the pzem AC part get its proper power. It might happen that when connecting the plug to the wall, the L and N would be reversed. However, it seems that it doesn’t matter I just tested it and it seems to work just fine.

1 Like

Share my working .yaml config :grinning:

I have using 6 pzem with esp01s board (VCC <-> 3.3V without any modification on pzem board)

It work normally, but after a few hour, 1 pzem always lost readng data (crc error in logs) another 5 pzem reading fine.
If I remove 1 of pzem cable, another 5 pzem will read data normally.
So I remove 1 pzem out use only 5 pzem and it work fine for month now.

Here is my final version of yaml file that all 5 pzem working fine.

substitutions:
  hostname: "esp-pzem"
  platform: esp8266
  board: esp01_1m
  password: "OTA_PASSWD"
  ssid: "WIFI_SSID"
  psa: "WIFI_PASSWD"
  key: "API_KEY"
  selfap: "PZEM_SELFAP"
  selfpsa: "AP_PASSWD"

esphome:
  name: $hostname
  platform: $platform
  board: $board

logger:
# to disable serial output set baudrate to 0
#  baud_rate: 0

api:
  encryption:
    key: $key

ota:
  password: $password

wifi:
  ssid: $ssid
  password: $psa

  ap:
    ssid: $selfap
    password: $selfpsa

captive_portal:

web_server:
  port: 80

#GPIO 2 use for modbus, led can not be use 
#status_led:
#  pin: 2

time:
  - platform: homeassistant
    id: esptime 

uart:
  id: uart_modbus
  tx_pin: 0
  rx_pin: 2
  baud_rate: 9600
  data_bits: 8
  stop_bits: 1
  parity: NONE

modbus:
  id: modbus_id
  uart_id: uart_modbus
#  send_wait_time: 50ms

switch:
  - platform: restart
    name: "Restart"

text_sensor:
  - platform: version
    name: version
  - platform: wifi_info
    ip_address:
      name: ip
    ssid:
      name: ssid
    bssid:
      name: bssid

sensor:
  - platform: wifi_signal
    name: "WiFi signal"
    update_interval: never
    id: wifi_stat
  - platform: uptime
    name: $hostname Uptime
    update_interval: never
    id: upt

  - platform: pzemac
    address: 23 #0x17
    update_interval: never
    id: pzem1
    voltage:
      name: "Line Voltage"
      id: vol1
    frequency:
      name: "Line Frequency"
      id: freq1
    current:
      name: "Ch-1 Current"
      id: cur1
    power:
      name: "Ch-1 Power"
      id: power1
      state_class: measurement
    power_factor:
      name: "Ch-1 Power Factor"
      id: pow_factor1
  - platform: total_daily_energy
    name: "Ch-1 Total Daily Energy"
    id: totaldailyenergy1
    unit_of_measurement: kWh
    accuracy_decimals: 3
    device_class: energy
    filters:
      - multiply: 0.001
    power_id: power1

  - platform: pzemac
    address: 22 #0x16
    update_interval: never
    id: pzem2
#    voltage:
#      name: "Ch-2 Voltage"
#      id: vol2
#    frequency:
#      name: "Ch-2 Frequency"
#      id: freq2
    current:
      name: "Ch-2 Current"
      id: cur2
    power:
      name: "Ch-2 Power"
      id: power2
      state_class: measurement
    power_factor:
      name: "Ch-2 Power Factor"
      id: pow_factor2
  - platform: total_daily_energy
    name: "Ch-2 Total Daily Energy"
    id: totaldailyenergy2
    unit_of_measurement: kWh
    accuracy_decimals: 3
    device_class: energy
    filters:
      - multiply: 0.001
    power_id: power2

  - platform: pzemac
    address: 21 #0x15
    update_interval: never
    id: pzem3
 #   voltage:
 #     name: "Ch-3 Voltage"
 #     id: vol3
 #   frequency:
 #     name: "Ch-3 Frequency"
 #     id: freq3
    current:
      name: "Ch-3 Current"
      id: cur3
    power:
      name: "Ch-3 Power"
      id: power3
      state_class: measurement
    power_factor:
      name: "Ch-3 Power Factor"
      id: pow_factor3
  - platform: total_daily_energy
    name: "Ch-3 Total Daily Energy"
    id: totaldailyenergy3
    unit_of_measurement: kWh
    accuracy_decimals: 3
    device_class: energy
    filters:
      - multiply: 0.001
    power_id: power3

  - platform: pzemac
    address: 20 #0x14
    update_interval: never
    id: pzem4
  #  voltage:
  #    name: "Ch-4 Voltage"
  #    id: vol4
  #  frequency:
  #    name: "Ch-4 Frequency"
  #    id: freq4
    current:
      name: "Ch-4 Current"
      id: cur4
    power:
      name: "Ch-4 Power"
      id: power4
      state_class: measurement
    power_factor:
      name: "Ch-4 Power Factor"
      id: pow_factor4
  - platform: total_daily_energy
    name: "Ch-4 Total Daily Energy"
    id: totaldailyenergy4
    unit_of_measurement: kWh
    accuracy_decimals: 3
    device_class: energy
    filters:
      - multiply: 0.001
    power_id: power4

  - platform: pzemac
    address: 19 #0x13
    update_interval: never
    id: pzem5
   # voltage:
   #   name: "Ch-5 Voltage"
   #   id: vol5
   # frequency:
   #   name: "Ch-5 Frequency"
   #   id: freq5
    current:
      name: "Ch-5 Current"
      id: cur5
    power:
      name: "Ch-5 Power"
      id: power5
      state_class: measurement
    power_factor:
      name: "Ch-5 Power Factor"
      id: pow_factor5
  - platform: total_daily_energy
    name: "Ch-5 Total Daily Energy"
    id: totaldailyenergy5
    unit_of_measurement: kWh
    accuracy_decimals: 3
    device_class: energy
    filters:
      - multiply: 0.001
    power_id: power5

interval:
  - interval: 10s
    then:
      - delay: 150ms
      - lambda: "id(pzem1).update();"
      - delay: 150ms
      - lambda: "id(pzem2).update();"
      - delay: 150ms
      - lambda: "id(pzem3).update();"
      - delay: 150ms
      - lambda: "id(pzem4).update();"
      - delay: 150ms
      - lambda: "id(pzem5).update();"
      - delay: 150ms
      - lambda: "id(wifi_stat).update();"
      - delay: 150ms
      - lambda: "id(upt).update();"

2 Likes

Nice setup, I am interested in the top 2 floors. Did you made them or can you buy them?

what type diodes you have bat45 or same?

I have used bat45

Hi,
I have Wemos D1 mini with PZEM-004T V3 connected as shown in the picture below. The problem I’m struggling with is “Unknown” sensor values.

Code:

esphome:
  name: power_consumption

esp8266:
  board: d1_mini

# Enable logging
logger:
 level: DEBUG
 baud_rate: 0

# Enable Home Assistant API
api:

ota:
  password: ######

wifi:
  ssid: ######
  password: ######

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: ######
    password: ######

captive_portal:

uart:
  rx_pin: GPIO3
  tx_pin: GPIO1
  baud_rate: 9600
  stop_bits: 1

modbus:

sensor:
  - platform: pzemac
    current:
      name: PZEM-004T V3 Current
    voltage:
      name: PZEM-004T V3 Voltage
    energy:
      name: PZEM-004T V3 Energy
    power:
      name: PZEM-004T V3 Power
    frequency:
      name: PZEM-004T V3 Frequency
    power_factor:
      name: PZEM-004T V3 Power Factor
    update_interval: 30s

I’ve also tried the following:

  • Switching to GPIO12/GPIO14 or GPIO4/GPIO5
  • Connecting 10k Ohm resistor between 3.3V - TX pin
  • Switching RX with TX pin
  • Connecting PZEM 5V with WEMOS 3.3V
  • Switching CT clamp on hot wire

The sensor was tested in ArduinoIDE giving measurments without any problem, but when connecting to HomeAssistnat I contantly get “unknown” values. I’m only trying to use Wemos with one PZEM so I don’t think I need to address the sensor.
Is level converter necessary to get this think work or there is something wrong with the code?
Any thoughts?
Thanks!

Just asking: Did you test this sensor (PZEM-004T V3) in the Arduino IDE with another device or with the same Wemos D1 mini?

I have a working connection with the combination Wemos D1 R2 / PZEM-004T V3 in ESPhome. In another combination I got a Wemos D1 mini V4 (with USB-C and Ic2 connection, save me to solder :grinning:), Both are recognized as:

esp8266:
  board: esp01_1m

The other difference with my PZEM-004T V3 / Wemos D1 R2 version is:

# Enable logging
logger:
  baud_rate: 0 # Due to TX/RX of Wemos D1 R2

uart:
  rx_pin: RX
  tx_pin: TX
  baud_rate: 9600

Not sure if this will fix your problem. Did not check if you made any typos. Is your log error-less?
I also would recommend to add the web-server:

# include a webserver because it is just very handy during your test phase :D
web_server:
  port: 80
  include_internal: true
  ota: false
  local: true
  log: false
  version: 2
  id: webserver

Good luck

good evening, I have a wemos d1 and a PZEM-004T V3 I parameterized it with esphome using vcc of 3.3v, tx, rx, gnd without any other component. the two devices are 2m apart. So far so good until 3 days ago I had to install a further 3 PZEM-004T V3 having modified the 3 addresses set thus 0x02 - 0x03 - 0x04 with the Arduino IDE with esp32. I reconnected everything to the wemos and set everything correctly and it tells me SENSOR UNKNOWN. info this distance is 8 meters without any components.

I have to add something

Ditch the arduino!!! use d1 with tasmota. 50/50 is long cable voltage 3.3V+1k resistor drop to pzem and dont work, or electrical interference on long rx,tx,5v cables.First wrap the rx/tx cable with tin foil,or run pzem with 5v and no 1k resistor

I have all in 3phase rat nest with shotky diodes on d1 rx side, 5v DC for D1 and pzem no 1k resistor and no Pzem adress. Works normal

1 Like

Pzem is power hungrey. I have problem with 3.3v and 1k resistor. Works on bench, when in use dont work. seperate 5v line from dc for d1 and pzem

2 Likes