How to use PZEM004T Energy Monitor with esphome

By using a template button: :control_knobs:

and make use of the uart.write action for the on_press trigger :raised_hands:

or even easier by utilizing the pzemac.reset_energy action :point_down:

winner winner chicken dinner :baby_chick:

1 Like

This is my code, it works fine but I can’t get yesterday’s energy, the sensor is unknown. Where is the error?

esphome:

  name: pzemac

  friendly_name: Wemos D1

esp8266:

  board: esp01_1m

# Enable logging

logger:

# Enable Home Assistant API

api:

  encryption:

    key: "XXXX"

ota:

  password: "xxxxxxx"

wifi:

  ssid: !secret wifi_ssid

  password: !secret wifi_password

  manual_ip:

   static_ip: 192.xxx.xxx.xxx

   gateway: 192.168.1.254

   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: "Wemos-D1 Fallback Hotspot"

    password: "xxx"

captive_portal:

uart:

  id: PZem

  rx_pin: 14

  tx_pin: 12

  baud_rate: 9600

sensor:

  - platform: wifi_signal

    name: "WiFi Sensor"

    update_interval: 60s

  - platform: pzemac

    current:

      name: Home Current

      unit_of_measurement: A

      accuracy_decimals: 1

      icon: mdi:flash-circle

    voltage:

      name: Home Voltage

      unit_of_measurement: V

      icon: mdi:flash-circle

    power:

      name: Home Power

      unit_of_measurement: W

      accuracy_decimals: 1        

      icon: mdi:flash-circle

      id: home_power

    power_factor:

      name: Home Power Factor

      id: pow_factor

 

    energy:

      name: Home Energy

      unit_of_measurement: Wh

      accuracy_decimals: 0

      #filters:

      #  - multiply: 0.001

      icon: mdi:flash-circle

      id: home_energy

    update_interval: 5s

  - platform: total_daily_energy

    name: "Total Daily Energy"

    unit_of_measurement: Wh

    accuracy_decimals: 0

    power_id: home_power    

 

  - platform: template

    name: Energy Yesterday

    id: set_yesterday

    unit_of_measurement: Wh

    accuracy_decimals: 0

    icon: mdi:lightning-bolt

button:

  - platform: template

    name: Reset

    on_press:

      uart.write: [0x01, 0x42, 0x80, 0x11]

time:

  - platform: homeassistant

    on_time:

      - seconds: 59

        minutes: 59

        hours: 23

        then:

          - sensor.template.publish:

              id: set_yesterday

              state: !lambda |-

                return id(home_energy).state;

I’m using this module to monitor my home consumption and it works quite nice. Even idle consumption is accurate. However my main breaker is only 40A, so I wanted to increase resolution, by using 50A transformer. Bought SCT016S 50A-100mA transformer, however it turned out to be very inaccurate at low currents, standard transformer is much better.
Now I’m thinking I could connect two 100A transformers in parallel, to increase accuracy. Would that work, or it’s not possible to connect multiple transformers?

Because the pzem004t (pcb) module is calibrated for the coil/clamp it is shipped with :bulb: As I know 10A and 100A versions are sold but the coils/clamp are likely not interchangable :warning:

How comes that?

PZEM-004T v3

Voltage

  • Measuring range:80~260V

  • Resolution: 0.1V

  • Measurement accuracy: 0.5%

Power factor

  • Measuring range: 0.00~1.00

  • Resolution: 0.01

  • Measurement accuracy: 1%

Frequency

  • Measuring range: 45Hz~65Hz

  • Resolution: 0.1Hz

  • Measurement accuracy: 0.5%

Current

  • Measuring range: 0~10A(PZEM-004T-10A); 0~100A(PZEM-004T-100A)

  • Starting measure current: 0.01A(PZEM-004T-10A); 0.02A(PZEM-004T-100A)

  • Resolution: 0.001A

  • Measurement accuracy: 0.5%

Active power

  • Measuring range: 0~2.3kW(PZEM-004T-10A); 0~23kW(PZEM-004T-100A)

  • Starting measure power: 0.4W

  • Resolution: 0.1W

  • Display format:

<1000W, it displays one decimal, such as: 999.9W

≥1000W, it display only integer, such as: 1000W

  • Measurement accuracy: 0.5%

Active Energy

  • Measuring range: 0~9999.99kWh

  • Resolution: 1Wh

  • Measurement accuracy: 0.5%

  • Display format:

<10kWh, the display unit is Wh(1kWh=1000Wh), such as: 9999Wh

≥10kWh, the display unit is kWh, such as: 9999.99kWh

Which resolution is the one not sufficient for you? The current of 0.001A steps? :thinking:

No, nothing to with calibration, I tested actual current transformer produces, and it’s badly nonlinear with low currents.

Also, resolution and accuracy do increase if more turns in the transformer, for example see this, It’s 10 times here, but the principle is would be the here using two transformers for 2 time increase.

Your solar inverter actually produces very high frequency pulses into the mains, the average of which is a reasonable approximation to the mains waveform.
However it has a great deal of electrical noise on it (like loads of spikes).
These can really confuse things expecting a fairly nice sine wave.
If you can put ferrites around the conductors, they will limit the high frequency noise.
Also keeping your clamp as far away from the inverter as you can may help reduce how much of the high frequency noise is seen but the PZEM module.
Steve

1 Like

Can indeed confirm you can wire two current transformers in parallel, and it works fine. (probably need to be in the same orientation). This can be slightly expensive alternative for increased accuracy if looping incoming wire can’t be feasibly done.

If I want to add three Pzem 004 to the Wemos do I have to connect them to the Wemos to change the address or is it not necessary?

By default the pzem004t have all the same address I think. Changing them so that each module got a unique address is necessary when you want to use more than one on the same bus :bulb:

1 Like

Thanks for the reply, but I’m a newbie and don’t know how to proceed.
As reported here
Peacefair PZEM-004T V3 Energy Monitor — ESPHome
I tried inserting the code, and actually in the log I notice that the modbus address is changed. But according to the link this code needs to be cleared. So I wonder, how should I proceed. Do I first write the code for three Pzem sensors giving each one its own ID, then temporarily paste the code to change the address and repeat three times, each time with a different ID?

Yes, something like this.

You just need to change the address in the yaml (based on this snippet) and attach the next module.

So uploading the yaml with address x and attach one pzem module
Then check logs if it works (like you did already) go ahead disconnect that module and at best mark it (write the address on the module itself for future reference).

Then you do the same by uploading a yaml but with a new adress, again attaching one module and so forth.

In the end you have modules with different address and can just proceed like normal - the yaml snippet for code changing shouldn’t be present anymore - it only needs to be one time and is save in the module.

1 Like

Thanks a lot, I’ll try it soon and tell you

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’.