How to use PZEM004T Energy Monitor with esphome

Wait a bit longer?

Thanks for your message :slight_smile:
here is the log:

INFO Reading configuration /config/esphome/espmeter.yaml...
INFO Starting log output from 192.168.31.111 using esphome API
INFO Connecting to 192.168.31.111:6053 (192.168.31.111)
INFO Successfully connected to 192.168.31.111
[12:01:00][I][app:100]: ESPHome version 1.14.4 compiled on Sep 21 2020, 11:39:03
[12:01:00][C][wifi:415]: WiFi:
[12:01:00][C][wifi:283]:   SSID: [redacted]
[12:01:00][C][wifi:284]:   IP Address: 192.168.31.111
[12:01:00][C][wifi:286]:   BSSID: [redacted]
[12:01:00][C][wifi:287]:   Hostname: 'espmeter'
[12:01:00][C][wifi:291]:   Signal strength: -53 dB ▂▄▆█
[12:01:00][V][wifi:293]:   Priority: 0.0
[12:01:00][C][wifi:295]:   Channel: 8
[12:01:00][C][wifi:296]:   Subnet: 255.255.255.0
[12:01:00][C][wifi:297]:   Gateway: 192.168.31.1
[12:01:00][C][wifi:298]:   DNS1: (IP unset)
[12:01:00][C][wifi:299]:   DNS2: (IP unset)
[12:01:01][C][uart:140]: UART Bus:
[12:01:01][C][uart:142]:   TX Pin: GPIO1
[12:01:01][C][uart:145]:   RX Pin: GPIO3
[12:01:01][C][uart:147]:   Baud Rate: 9600 baud
[12:01:01][C][uart:148]:   Stop bits: 1
[12:01:01][C][uart:150]:   Using hardware serial interface.
[12:01:01][W][uart:379]:   You're using the same serial port for logging and the UART component. Please disable logging over the serial port by setting logger->baud_rate to 0.
[12:01:01][C][modbus:096]: Modbus:
[12:01:01][E][uart:391]:   Invalid stop bits: Integration requested stop_bits 2 but you have 1!
[12:01:01][C][logger:175]: Logger:
[12:01:01][C][logger:176]:   Level: VERBOSE
[12:01:02][C][logger:177]:   Log Baud Rate: 115200
[12:01:02][C][logger:178]:   Hardware UART: UART0
[12:01:02][C][logger:180]:   Level for 'mqtt.component': DEBUG
[12:01:02][C][logger:180]:   Level for 'mqtt.client': ERROR
[12:01:02][C][pzemac:061]: PZEMAC:
[12:01:02][C][pzemac:062]:   Address: 0x01
[12:01:02][C][pzemac:063]: Voltage 'PZEM-004T V3 Voltage'
[12:01:02][C][pzemac:063]:   Unit of Measurement: 'V'
[12:01:02][C][pzemac:063]:   Accuracy Decimals: 1
[12:01:02][C][pzemac:063]:   Icon: 'mdi:flash'
[12:01:02][C][pzemac:064]: Current 'PZEM-004T V3 Current'
[12:01:03][C][pzemac:064]:   Unit of Measurement: 'A'
[12:01:03][C][pzemac:064]:   Accuracy Decimals: 3
[12:01:03][C][pzemac:064]:   Icon: 'mdi:current-ac'
[12:01:03][C][pzemac:065]: Power 'PZEM-004T V3 Power'
[12:01:03][C][pzemac:065]:   Unit of Measurement: 'W'
[12:01:03][C][pzemac:065]:   Accuracy Decimals: 1
[12:01:04][C][pzemac:065]:   Icon: 'mdi:power'
[12:01:04][C][pzemac:066]: Frequency 'PZEM-004T V3 Frequency'
[12:01:04][C][pzemac:066]:   Unit of Measurement: 'hz'
[12:01:04][C][pzemac:066]:   Accuracy Decimals: 1
[12:01:04][C][pzemac:066]:   Icon: 'mdi:current-ac'
[12:01:04][C][pzemac:067]: Power Factor 'PZEM-004T V3 Power Factor'
[12:01:04][C][pzemac:067]:   Unit of Measurement: ''
[12:01:04][C][pzemac:067]:   Accuracy Decimals: 2
[12:01:04][C][pzemac:067]:   Icon: 'mdi:flash'
[12:01:04][C][captive_portal:169]: Captive Portal:
[12:01:04][C][web_server:123]: Web Server:
[12:01:04][C][web_server:124]:   Address: 192.168.31.111:80
[12:01:04][C][ota:029]: Over-The-Air Updates:
[12:01:05][C][ota:030]:   Address: 192.168.31.111:8266
[12:01:05][C][api:095]: API Server:
[12:01:05][C][api:096]:   Address: 192.168.31.111:6053
[12:01:05][C][mqtt:051]: MQTT:
[12:01:05][C][mqtt:053]:   Server Address: 192.168.31.5:1883 (192.168.31.5)
[12:01:05][C][mqtt:054]:   Username: [redacted]
[12:01:05][C][mqtt:055]:   Client ID: [redacted]
[12:01:05][C][mqtt:057]:   Discovery prefix: 'homeassistant'
[12:01:05][C][mqtt:058]:   Discovery retain: YES
[12:01:05][C][mqtt:060]:   Topic Prefix: 'espmeter'
[12:01:06][C][mqtt:062]:   Log Topic: 'espmeter'
[12:01:06][C][mqtt:065]:   Availability: 'espmeter/status'
[12:01:06][C][mqtt.sensor:024]: MQTT Sensor 'PZEM-004T V3 Voltage':
[12:01:06][C][mqtt.sensor:028]:   State Topic: 'espmeter/sensor/pzem-004t_v3_voltage/state'
[12:01:06][C][mqtt.sensor:024]: MQTT Sensor 'PZEM-004T V3 Current':
[12:01:06][C][mqtt.sensor:028]:   State Topic: 'espmeter/sensor/pzem-004t_v3_current/state'
[12:01:06][C][mqtt.sensor:024]: MQTT Sensor 'PZEM-004T V3 Power':
[12:01:07][C][mqtt.sensor:028]:   State Topic: 'espmeter/sensor/pzem-004t_v3_power/state'
[12:01:07][C][mqtt.sensor:024]: MQTT Sensor 'PZEM-004T V3 Frequency':
[12:01:07][C][mqtt.sensor:028]:   State Topic: 'espmeter/sensor/pzem-004t_v3_frequency/state'
[12:01:07][C][mqtt.sensor:024]: MQTT Sensor 'PZEM-004T V3 Power Factor':
[12:01:07][C][mqtt.sensor:028]:   State Topic: 'espmeter/sensor/pzem-004t_v3_power_factor/state'

I made it works after switch to Tasmota. Thanks

Hi, it’s too late but for next users :

Make sure the device is connected to the AC power! The 5V only power the optocouplers, not the actual chip !!!

You can use this library to test your PZEM-004T with arduino or ESP8266/Wemos D1 :

This one is great because it is simple : no tweaking to to except set the 2 serial pins, for example :

  • Pin D5 Rx (Connects to the Tx pin on the PZEM)
  • Pin D6 Tx (Connects to the Rx pin on the PZEM)
    It should work immediately even if you put AC power after ESP8266 boot.

Once it’s working, switch to ESPhome. The docmentation specify to put “stop_bits: 2” for the PZEM-004T V3 but it was false in my case…

My working config:

uart:
  rx_pin: D5
  tx_pin: D6
  baud_rate: 9600


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: 2s

Can you share more info about getting 5V supply also any difference in accuracy compared to 004T V3 ?

hello, is there a way to reset the pzem energy counter (esphome)?
For exemple with an automation…
Thanks


#FAST SENSORS FOR ENERGY CALCULATION & LOCAL DISPLAY
  - platform: pzemac
    current:
      name: "PZEM-016 Current"
      id: aac
      internal: true
    voltage:
      name: "PZEM-016 Voltage"
      id: vac
      internal: true
    power:
      name: "PZEM-016 Power"
      id: w
      internal: true
    frequency:
      name: "PZEM-016 Frequency"
      id: f
      internal: true
    power_factor:
      name: "PZEM-016 Power Factor"
      id: pf
      internal: true
    update_interval: 2s

  - platform: total_daily_energy
    name: "PZEM-016 Daily Energy"
    power_id: w 
    filters:
        # Multiplication factor from W to kW is 0.001
      - multiply: 0.001
    unit_of_measurement: kWh
    icon: mdi:counter
    accuracy_decimals: 1   


#SLOW SENSORS FOR HOME ASSISTANT
  - platform: template #########################
    name: "PZEM-016 Current Slow"
    lambda: |-
      if (id(aac).state) {
        return (id(aac).state);
      } else {
        return 0;
      }
    unit_of_measurement: A
    icon: "mdi:alpha-a-circle"
    update_interval: 10s
  
  - platform: template #########################
    name: "PZEM-016 Voltage Slow"
    lambda: |-
      if (id(vac).state) {
        return (id(vac).state);
      } else {
        return 0;
      }
    unit_of_measurement: V
    icon: "mdi:alpha-v-circle"
    update_interval: 10s
  
  - platform: template #########################
    name: "PZEM-016 Power Slow"
    lambda: |-
      if (id(w).state) {
        return (id(w).state);
      } else {
        return 0;
      }
    unit_of_measurement: W 
    icon: "mdi:alpha-w-circle"
    update_interval: 10s
  
  - platform: template #########################
    name: "PZEM-016 Frequency Slow"
    lambda: |-
      if (id(f).state) {
        return (id(f).state);
      } else {
        return 0;
      }
    unit_of_measurement: Hz
    icon: "mdi:alpha-f-circle"
    update_interval: 10s

  - platform: template #########################
    name: "PZEM-016 Power Factor Slow"
    lambda: |-
      if (id(pf).state) {
        return (id(pf).state);
      } else {
        return 0;
      }
    unit_of_measurement: PF
    icon: "mdi:alpha-p-circle"
    update_interval: 10s

@sendorm
Can you please show my how you have wired those 3?

I’m going to make a 3 phase and another 3phase to monitor my solar installation.

also interested in the way to reset it.

You will nee to wire them via an rs485 to TTL converted. as the PZEM talks RS485 and EPS talks UART.

something like this you will need to place between the ESP and the PZEM:
https://www.amazon.de/-/nl/gp/product/B07RKY1G71

You have to use this: 68074678-0dd16e80-fd9e-11e9-8d80-5650e9e18fb8

I only had success with v3 of pzems. Also the diode’s should be schottky.

1 Like

And this is my setup, I am using a 5volt dc source.

image

Can you please give me a link to the diodes you have used?

I have used bat45.

This is the source if anyone wonders.

@sendorm

Addresses of each pzem should be changed manually. Tasmota has a command to do so.

I can make the 3 phase work in Tasmota. (followed this https://zorruno.com/w/EnergyMonitoringPZEM004T) , but I can’t use them in EspHome afterwards…

Can you post your esphome code.

FWWI:

be aware there’s a difference between:
- platform: pzemac

- platform: pzemdc

This works for me:

uart:
  tx_pin: D1
  rx_pin: D2
  baud_rate: 9600
  # stop_bits: 2

sensor:
  - platform: pzemac
    update_interval: 1s
    current:
      name: "${esp_name} A"
      #filters:
      #- calibrate_linear:
      # Map from sensor -> measured value
      #    - 0.0 -> 0.0
      #    - 1.231 -> 2.695
      #    - 1.606 -> 3.651
      #    - 1.928 -> 4.920
    voltage:
      name: "${esp_name} V"
    power:
      name: "${esp_name} W"
      unit_of_measurement: W
      id: "${esp_name}_W"
      icon: mdi:flash-outline
      filters:
       - calibrate_linear:
       # Map from sensor -> measured value
          - 0.0 -> 0.0
          - 256.0 -> 248.0
          - 662.0 -> 642.0
          - 1050.0 -> 1030.0
       - lambda: if (x < 5.0) return 0.0; else return x;
    frequency:
      name: "${esp_name} Hz"
    power_factor:
      name: "${esp_name} Pf"
    energy:
      name: "${esp_name} kWh"
      filters:
        # Multiplication factor from Wh to kWh is 0.001
        - multiply: 0.001
      unit_of_measurement: kWh

My first 3 phase esp works with EspHome :metal:
This is my code

substitutions:
  devicename: smappee_fluvius
  ip: 192.168.xx.xx
  
  ssid: !secret ssid
  password: !secret password
  
esphome:
  name: ${devicename}
  platform: ESP8266
  board: nodemcuv2

wifi:
  ssid: ${ssid}
  password: ${password}
  power_save_mode: none
  manual_ip:
    static_ip: ${ip}
    gateway: 192.168.79.1
    subnet: 255.255.255.0
    dns1: 8.8.8.8
    dns2: 8.8.4.4

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: ${devicename}
    password: ${password}
    manual_ip: 
      static_ip: 192.168.100.2
      gateway: 192.168.100.1
      subnet: 255.255.255.0
      dns1: 8.8.8.8
      dns2: 8.8.4.4

captive_portal:

web_server:
  port: 80

debug:

# Enable logging
logger:
  level: DEBUG
  baud_rate: 0

# Enable Home Assistant API
api:
  password: ${password}

ota:
  password: ${password}

uart:
  - id: ubus
    tx_pin: GPIO1
    rx_pin: GPIO3
    baud_rate: 9600
    stop_bits: 1

modbus:
  id: mbus
  uart_id: ubus
  
switch:
  - platform: restart
    name: Smappee fluvius herstarten  
    
sensor:
  - platform: wifi_signal
    name: "WiFi Smappee fluvius"
    update_interval: 60s
    
  - platform: pzemac
    address: 1
    modbus_id: mbus
    current:
      name: "L1 stroom"
    voltage:
      name: "L1 spanning"
    energy:
      name: "L1 verbruik"
    power:
      name: "L1 vermogen"
    frequency:
      name: "L1 frequentie"
    power_factor:
      name: "L1 cos phi"
    update_interval: 2s
  - platform: pzemac
    address: 2
    current:
      name: "L2 stroom"
    voltage:
      name: "L2 spanning"
    energy:
      name: "L2 verbruik"
    power:
      name: "L2 vermogen"
    frequency:
      name: "L2 frequentie"
    power_factor:
      name: "L2 cos phi"
    update_interval: 2s
  - platform: pzemac
    modbus_id: mbus
    address: 3
    current:
      name: "L3 stroom"
    voltage:
      name: "L3 spanning"
    energy:
      name: "L3 verbruik"
    power:
      name: "L3 vermogen"
    frequency:
      name: "L3 frequentie"
    power_factor:
      name: "L3 cos phi"
    update_interval: 2s

I’ve had to add this part

modbus:
  id: mbus
  uart_id: ubus
  
...
sensor:
  - platform: pzemac
    address: 1
    modbus_id: mbus
...

It should be a lot easier if we could set the address in EspHome instead of first flashing the esp with Tasmota -> set address -> reflash esp with EspHome -> :crossed_fingers: it works with EspHome

3 Likes

If you are using Pzem004-T v3, you should connect it to a Pc (using a serial to usb converter) and then use the Peacefair PC software to change pzem address. I used this way with pzem016. Pzem004-T v3 should work in the same way. Look here for pc software:

This software is windows only, I do not have acces anymore to a windows pc.
But I have managed to set the address by first flash an esp as Tasmota and then

Setting Module ID

As well as wiring for multidrop, the devices need to have an ID set (Sometimes referred to as IP address online, but it is a serial address). Tasmota now also allows you to set this easily with the ModuleAddress command- previously this was done with some serial commands (or with windows software linked below, but I tried this unsuccessfully).

To set the address, connect each module one at a time and on the Tasmota console, run eg

                  ModuleAddress 2

You'll get a response to say 'Done', but there is no reply to ensure it is carried out successfully. Obviously, set 1, 2 or 3 as needed but remember to disconnect the others when you set each one.

And then connect them to and esp with esphome flashed

Hey benek984, love what you have done wit the LCD Display, Is it a 2004 with I2C?

Also is it ESPHOME or Arduino Code?,
Could you share your config?

Kind Regards,