Peacefair PZEM-004T V3
The PZEM-004T V3 is a single phase AC power monitoring module that supports measurement of 80-260 V voltage, 100 A max current, power, energy, frequency and power factor (datasheet [Archive]), it talks Modbus protocol on RS-485 serial.
I bought one off AliExpress for a total of €8.17 with free shipping, including a noninvasive current measuring transformer sensor (which is usually called a CT clamp
) for ease of installation:
Hardware
The circuit is very simple. Since the PZEM uses a passive serial port thus requires an external +5 V supply, for convenience, I used a WeMos D1 mini module, which has a built-in USB 5 V to 3.3 V power supply circuit, instead of a bare ESP-12F.
An additional logic level shifter is required to convert the 5 V TTL of the PZEM to the 3.3 V accepted by the ESP.
As for the voltage probe, it can be made using a regular plug and plug into a wall socket; or directly connect a wire to a breaker inside the breaker box, along with the current probe.
Completed circuit board, PZEM and voltage probe (a regular plug):
The relay on the circuit board is prepared for unlocking the building door in a future project
Install the Current Probe
⚡ DANGER! ELECTRICAL HAZARD ⚡
You probably need to extend the current probe's cable first.
Remove the breaker box panel, find the main live or neutral wire (usually the thickest and connected to the main breaker), clamp the current probe on it:
Put back the panel and lead the probe cable out through a hole:
Connect the voltage and current probes to the PZEM module's screw terminals respectively according to the instructions printed on its enclosure, then connect power to the ESP module and it's done:
Software
ESPHome configuration example:
substitutions:
device_name: 'Energy Meter'
esphome:
name: energy-meter
comment: 'PZEM-004T energy meter'
esp8266:
board: d1_mini
logger:
level: WARN
# baud_rate: 115200
wifi:
fast_connect: true
networks:
- ssid: !secret wifi_ssid
bssid: !secret wifi_bssid
password: !secret wifi_password
domain: !secret domain_name
api:
ota:
password: !secret ota_password
button:
# for hard restart
- platform: restart
id: restart_button
name: 'Restart ${device_name}'
entity_category: diagnostic
# for resetting total energy counter on PZEM
- platform: template
name: 'Reset PZEM-004T Total Energy Counter'
entity_category: diagnostic
on_press:
then:
- pzemac.reset_energy: pzemac_1
icon: mdi:numeric-0-circle-outline
status_led:
pin:
number: 2
inverted: true
uart: # enable a software UART
tx_pin: 5
rx_pin: 4
baud_rate: 9600
modbus: # enable Modbus hub
sensor:
- platform: pzemac
id: pzemac_1
address: 1 # specify the Modbus custom address when there are multiple PZEM connected
current:
name: 'Current'
accuracy_decimals: 3
voltage:
name: 'Voltage'
accuracy_decimals: 1
power:
name: 'Power'
accuracy_decimals: 1
energy:
name: 'Energy'
# convert to kWh
filters:
- multiply: 0.001
unit_of_measurement: 'kWh'
accuracy_decimals: 3
frequency:
name: 'Frequency'
accuracy_decimals: 1
power_factor:
name: 'Power Factor'
accuracy_decimals: 2
update_interval: 30s
Now you can add it to Home Assistant and select sensor.energy
as the Energy platform data source.
If you would like to add a glance card to your lovelace dashboard, configure two utility_meter
sensors in YAML first:
energy_monthly:
source: sensor.energy
cycle: monthly
energy_daily:
source: sensor.energy
cycle: daily
Then add a lovelace glance card:
type: glance
title: Energy Consumption
show_icon: true
show_state: true
state_color: false
entities:
- entity: sensor.power
- entity: sensor.energy_daily
name: Daily
- entity: sensor.energy_monthly
name: Monthly
6 Comments
Gemul · 2022-05-22 at 16:56
Hi, just want to let you know that the rx/tx wiring in diagram and the given code is not correct. the code says TX is GPIO4, which is D2, and in diagram, D2 is connected to the TX pin (next to GND) of the PZEM. TX should be connected to RX and vice versa.
zry98 · 2022-07-02 at 03:15
Thanks for pointing that out! Fixed in the yaml.
Josip · 2022-12-07 at 15:27
Which program is installed on esp8266?
zry98 · 2022-12-08 at 14:19
ESPHome.
sanjay · 2023-03-04 at 16:29
Is it required to use level shifter ic - like this https://robu.in/product/level-converter-4-channel/
zry98 · 2023-03-04 at 17:24
Yes, I used an exact same one as you linked