Pzem 004t add and change address

Hi, I currently use a Pzem-004t connected to a Wemos D1 to measure power consumption with this yaml:

esphome:
  name: pzemac
  friendly_name: Wemos D1

esp8266:
  board: esp01_1m

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "aieJbRbkQ8pZIPF/siQ0DEDREArpdjqDbzTRwmbP9Bw="

ota:
  password: "5bb7927d4a20446b62043aa7c742e87a"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  manual_ip:
   static_ip: 192.168.1.62
   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: "BlVuGhyroRCc"

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: kWh
      state_class: total
      device_class: energy
      accuracy_decimals: 2
      filters:
        - multiply: 0.001
      icon: mdi:flash-circle
      id: home_energy
    update_interval: 1s

  - platform: total_daily_energy
    name: "Total Daily Energy"
    id: total_day_energy
    unit_of_measurement: Wh
    state_class: total
    device_class: energy
    accuracy_decimals: 0
    power_id: home_power

  

button:
  - platform: template
    name: Reset
    id: my_button
    on_press:
      uart.write: [0x01, 0x42, 0x80, 0x11]

switch:
  - platform: uart
    uart_id: PZem
    id: reset_PZem
    name: "PZEM-004T V3 Energy Reset"
    data: [0x01, 0x42, 0x80, 0x11]

 
time:
  - platform: homeassistant
    on_time:
      - seconds: 59
        minutes: 59
        hours:   23
        then:

I would like to add two more pzem on the same wemos but I am not able to change the addresses.
I saw that Esphome has a code (Peacefair PZEM-004T V3 Energy Monitor — ESPHome) to do it but I don’t know how to proceed, should I write it in the yaml? At what point? If anyone can explain to me how to do it step by step I would be grateful

hi…I’m running 3 PZEM-004T in one ESP

simply do as shown in the document (Peacefair PZEM-004T V3 Energy Monitor — ESPHome)

here is the code to change address 0x01 to 0x02
copy the code and install it in esp with proper wiring (rx and tx) to pzem-004t
run it only once it will change the address after that do in normally as shone in the document to getting the reading

This is only for Changing the address

esphome:
  name: test
  friendly_name: test
  on_boot:
    priority: -100
    then:
      - lambda: |-
          auto new_address = 0x02;

          if(new_address < 0x01 || new_address > 0xF7) // sanity check
          {
            ESP_LOGE("ModbusLambda", "Address needs to be between 0x01 and 0xF7");
            return;
          }

          esphome::modbus_controller::ModbusController *controller = id(pzem);
          auto set_addr_cmd = esphome::modbus_controller::ModbusCommandItem::create_write_single_command(
            controller, 0x0002, new_address);

          delay(200) ;
          controller->queue_command(set_addr_cmd);
          ESP_LOGI("ModbusLambda", "PZEM Addr set");
uart:
  - id: PZEM
    rx_pin: D1
    tx_pin: D2
    baud_rate: 9600
modbus:
  - id: mbus
    uart_id: PZEM
    send_wait_time: 200ms

modbus_controller:
  - id: pzem
    address: 0x1
    modbus_id: mbus
    command_throttle: 0ms
    setup_priority: -10
    update_interval: 30s

esp8266:
  board: d1_mini

logger:
    

for another PZEM-004T simply change 0x02 to 0x03 in the code

Hi, thanks for the reply. I’d like to ask you a couple of questions:

  • Do the devices also have to be powered by AC to change the address or is the connection with ESP sufficient?

I just tried this. The chip responds to nothing if not powered by mains voltages. It would only change addresses if it was powered up on AC as well as 5v.

hi,

I was equiped with one pzem and i’m trying to upgrade my installation with 2 additional ones.

cabling seems to work as my esp8266 see each pzem when I power each one individually.

i’ve tried the code here above to set the adress to the 2nd and 3rd pzem without success. when i power all the pzem in the same time, i have a CRC error.
the pzem are keeping the adress 01.

My method was to powered only the 2nd and push the code to change the adress and restart the ESP. same for the 3rd.

Are we supposed to have a message in the logs confirming the adress change ?