Hulp met PZEM0004T energiemeters met ESPhome

Hoi Allen,

korte samenvatting van de setup:
HP thin client t610 met de laatste versie van Home Assistant;
Add on ESP home geinstalleerd;
3 stuks PZEM004T V3.0 met stroomtransformator
NodeMCU V3

Ik probeer een 3~AC energiemeter te maken.
Ik heb reeds 1 PZEM met de NodeMCU verbonden en de standaard test code van https://esphome.io/components/sensor/pzemac.html erin geladen. Dit werkt perfect.

Nu wil ik de andere 2 op dezelfde UART uitlezen. Dan moet ik volgens de handleiding van ESPhome het adres van de PZEM wijzigen (standaard 0x01).

Deze code doet het niet goed en geeft een foutmelding. Ik heb hier nog helemaal geen kaas van gegeten, dus ik weet niet wat er fout loopt...

Kan iemand mij hiermee mee op weg helpen?

code van de website:

c code:


esphome:
  ...
  on_boot:
    ## configure controller settings at setup
    ## make sure priority is lower than setup_priority of modbus_controller
    priority: -100
    then:
      - lambda: |-
          auto new_address = 0x03;

          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");

modbus:
  send_wait_time: 200ms
  id: mod_bus_pzem

modbus_controller:
  - id: pzem
    ## the current device addr
    address: 0x1
    modbus_id: mod_bus_pzem
    command_throttle: 0ms
    setup_priority: -10
    update_interval: 30s

Screenshot van mijn YAML file:

foutcode:

" Assumption is the mother of all f*ckups ", toch trap ik er geregeld in.... How to kill a mosquito with an elephant? --> Surfin' on a AMD RyZen 9 5900x, 12 cores @ 3.7GHz, opgekieteld naar 4.9GHz.
marcob

Golden Member

Is dit je hele YAMl file? Zo ja, dan mis je de basis wat nodig is voor ESP home. Hieronde reen voorbeeld wat ik als basis gebruik.
Voor de verklaring van alles verwijs ik je door naar de ESPHome site

c code:

substitutions:
  device_name: doorbell
  device_description: "ESP8266 Doorbell Module"
 
esphome:
  name: ${device_name}
  comment: "${device_description}"
  platform: ESP8266
  board: esp01_1m

wifi:
  ssid: !secret esphome_wifi_ssid
  password: !secret esphome_wifi_password

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

# Enable logging
logger:

# Enable Home Assistant API  
api:
  reboot_timeout: 15min
  password: !secret esphome_api_password

# Over the Air update
ota:
  password: !secret esphome_ota_password

# Webserver & sensor
web_server:
  port: 80

text_sensor:
  - platform: wifi_info
    ip_address:
      name: ${device_name} IP Address
    ssid:
      name: ${device_name} Connected SSID
    bssid:
      name: ${device_name} Connected BSSID
    mac_address:
      name: ${device_name} Mac Wifi Address

sensor:
  - platform: wifi_signal
    name: ${device_name} Wifi Signal Strength
    update_interval: 60s
  
  - platform: uptime
    name: ${device_name} Uptime

#######################################
# Device specific Config Begins Below #
#######################################
Een WOK is ook maar een vergiet zonder gaatjes

ik had het eerder al geprobeerd met de code die automatisch wordt toegevoegd als het device wordt aangemaakt, maar toen deed ie het ook niet. De code die nu in commentaar staat is de code om de PZEM uit te lezen, dat werkt wel...

c code:


esphome:
  name: nodemcu-energymeter

esp8266:
  board: nodemcuv2

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "blablabla"

ota:
  password: "blablabla"

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

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Nodemcu-Energymeter"
    password: "blablabla"

captive_portal:

# Example configuration entry
#uart:
#  rx_pin: D1
#  tx_pin: D2
#  baud_rate: 9600

#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: 60s
#     
  on_boot:
    ## configure controller settings at setup
    ## make sure priority is lower than setup_priority of modbus_controller
    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");

modbus:
  send_wait_time: 200ms
  id: mod_bus_pzem

modbus_controller:
  - id: pzem
    ## the current device addr
    address: 0x1
    modbus_id: mod_bus_pzem
    command_throttle: 0ms
    setup_priority: -10
    update_interval: 30s
" Assumption is the mother of all f*ckups ", toch trap ik er geregeld in.... How to kill a mosquito with an elephant? --> Surfin' on a AMD RyZen 9 5900x, 12 cores @ 3.7GHz, opgekieteld naar 4.9GHz.
marcob

Golden Member

Het stuk on_boot moet onder het kopje esphome:
dus

c code:


esphome:
  name: nodemcu-energymeter
  on_boot:
    ## configure controller settings at setup
    ## make sure priority is lower than setup_priority of modbus_controller
    priority: -100
    then:
enz

zie: https://esphome.io/components/esphome.html?on-boot#on-boot

Een WOK is ook maar een vergiet zonder gaatjes

Bedankt!!! Dat hielp....
Staat nogal vaag uitgelegd in de docu van de PZEM AC
Dan zeurde die nog dat er geen UART gespecifieerd was....
Deze code werkte:

c code:


esphome:
  name: nodemcu-energymeter
  on_boot:
    ## configure controller settings at setup
    ## make sure priority is lower than setup_priority of modbus_controller
    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");

# Example configuration entry
uart:
  rx_pin: D1
  tx_pin: D2
  baud_rate: 9600

modbus:
  send_wait_time: 200ms
  id: mod_bus_pzem

modbus_controller:
  - id: pzem
    ## the current device addr
    address: 0x1
    modbus_id: mod_bus_pzem
    command_throttle: 0ms
    setup_priority: -10
    update_interval: 30s 
  

esp8266:
  board: nodemcuv2

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "blablabla"

ota:
  password: "blablabla"

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

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Nodemcu-Energymeter"
    password: "blablabla"

#captive_portal:

# Example configuration entry
#uart:
#  rx_pin: D1
#  tx_pin: D2
#  baud_rate: 9600

#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: 60s
#     
" Assumption is the mother of all f*ckups ", toch trap ik er geregeld in.... How to kill a mosquito with an elephant? --> Surfin' on a AMD RyZen 9 5900x, 12 cores @ 3.7GHz, opgekieteld naar 4.9GHz.

Voor de geïnteresseerden:

3 fase energiemeter met ESPhome en 3x PZEM-004T V3.0+ NodeMcu V3

Alledrie TX en RX parallel aangesloten op pinnen D1 en D2 van de NodeMcu.

Eerst van 2 sensoren de adressen veranderen (standaard 0x01) met volgende code:
Let wel; voor het adres te veranderen enkel de sensor aansluiten die je wil wijzigen..

c code:


esphome:
  name: nodemcu-energymeter
  on_boot:
    ## configure controller settings at setup
    ## make sure priority is lower than setup_priority of modbus_controller
    priority: -100
    then:
      - lambda: |-
          # Here the new address you would like to use
          auto new_address = 0x03;

          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");

# Example configuration entry
uart:
  rx_pin: D1
  tx_pin: D2
  baud_rate: 9600

modbus:
  send_wait_time: 200ms
  id: mod_bus_pzem

modbus_controller:
  - id: pzem
    ## the current device address -- PZEM has standard 0x01
    address: 0x1
    modbus_id: mod_bus_pzem
    command_throttle: 0ms
    setup_priority: -10
    update_interval: 30s 
  

esp8266:
  board: nodemcuv2

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "Hier staan uw gegevens"

ota:
  password: "Hier staan uw gegevens"

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

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Nodemcu-Energymeter"
    password: "Hier staan uw gegevens"

captive_portal:

Dan Alle sensoren aansluiten en volgende code geeft de info van alledrie sensoren door:

c code:


esphome:
  name: nodemcu-energymeter

esp8266:
  board: nodemcuv2

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "Hier staan uw gegevens"

ota:
  password: "Hier staan uw gegevens"

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

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Nodemcu-Energymeter"
    password: "Hier staan uw gegevens"

captive_portal:


uart:
  rx_pin: D1
  tx_pin: D2
  baud_rate: 9600

modbus:
  id: mod_bus_pzem

# Sensor on phase 1 
sensor:
  - platform: pzemac
    current:
      name: "L1 Current"
    voltage:
      name: "L1 Voltage"
    energy:
      name: "L1 Energy"
    power:
      name: "L1 Power"
    frequency:
      name: "L1 Frequency"
    power_factor:
      name: "L1 Power Factor"
    update_interval: 60s
    address: 0x01
    modbus_id: mod_bus_pzem

# Sensor on phase 2
  - platform: pzemac
    current:
      name: "L2 Current"
    voltage:
      name: "L2 Voltage"
    energy:
      name: "L2 Energy"
    power:
      name: "L2 Power"
    frequency:
      name: "L2 Frequency"
    power_factor:
      name: "L2 Power Factor"
    update_interval: 60s
    address: 0x02
    modbus_id: mod_bus_pzem

# Sensor on phase 3
  - platform: pzemac
    current:
      name: "L3 Current"
    voltage:
      name: "L3 Voltage"
    energy:
      name: "L3 Energy"
    power:
      name: "L3 Power"
    frequency:
      name: "L3 Frequency"
    power_factor:
      name: "L3 Power Factor"
    update_interval: 60s
    address: 0x03
    modbus_id: mod_bus_pzem

" Assumption is the mother of all f*ckups ", toch trap ik er geregeld in.... How to kill a mosquito with an elephant? --> Surfin' on a AMD RyZen 9 5900x, 12 cores @ 3.7GHz, opgekieteld naar 4.9GHz.