Modbus "Man-in-the-middle"

I have a EV charger Wallbox Pulsar Plus and as soon as my electritian can get a “PowerBoost” Carlo Gavazzi EM300 he will come and install it. There is a lot of data I would like to get into HA from that power meter so I am trying to figure out the best way to get that data.

Since the EV Charger is acting as a client and it is Modbus RTU I can not connect another client to the meter, so my idea is to build an ESP device that has two RS485 interfaces on two different UARTS and then just relay the data and at the same time pass it on to HA. So I started to write a sketch with the modbus-esp8266 library but then I thought it would be great if I could use ESPHome instead.

So my question is if that is possible? I have put together this yaml:

esphome:
  name: Modbus_hub
  platform: ESP32
  board: esp32dev

wifi:
  ssid: !secret wifi_sid
  password: !secret wifi_password
  reboot_timeout: 0s

api:
  reboot_timeout: 0s

logger:
  level: INFO
  baud_rate: 0

uart:
  - id: modbus_client_serial
    tx_pin: 1
    rx_pin: 3
    baud_rate: 9600
    stop_bits: 1
  - id: modbus_server_serial
    tx_pin: 17
    rx_pin: 16
    baud_rate: 9600
    stop_bits: 1
    
modbus:
  - id: modbus_client
    uart_id: modbus_client_serial
    flow_control_pin: 21
    send_wait_time: 500
  - id: modbus_server
    uart_id: modbus_server_serial
    flow_control_pin: 5

modbus_controller:
  - id: power_meter
    address: 0x1
    modbus_id: modbus_client_1
  - id: ev_charger
    modbus_id: modbus_server_1

sensor:
  - platform: modbus_controller
    modbus_controller_id: modbus_power_meter
    name: "L1 Current"
    id: l1_current_input
    register_type: read
    address: 0x12
    unit_of_measurement: "A"
    value_type: S_DWORD_R
    filters:
      - multiply: 0.001
  - platform: modbus_controller
    modbus_controller_id: modbus_power_meter
    name: "L2 Current"
    id: l2_current_input
    register_type: read
    address: 0x14
    unit_of_measurement: "A"
    value_type: S_DWORD_R
    filters:
      - multiply: 0.001
  - platform: modbus_controller
    modbus_controller_id: modbus_power_meter
    name: "L3 Current"
    id: l3_current_input
    register_type: read
    address: 0x16
    unit_of_measurement: "A"
    value_type: S_DWORD_R
    filters:
      - multiply: 0.001

output:
  - platform: modbus_controller
    modbus_controller_id: ev_charger
    id: l1_current_output
    lambda: WHAT TO HAVE HERE?!?!?
    address: 0x12
    value_type: S_DWORD_R

So: does anyone know if this will work? I have no way to test it until the meter is delivered and installed. And my electrician have told me it might not be until next year due to the global shortage of semi conductors.

1 Like

I do something similar with my newmotion charging point. I have an ESP listening in on the modbus signal. Interestingly I did not have to put it “in the middle”. I just have the esp connected in parrallel and it decodes both the master and slave messages. Githubv repo is here: tnagels/newmotion_energy_sniffer: A sniffer for the energy meter in NewMotion car charging stations (github.com)

1 Like

Very interesting solution. Might work with some small adjustments.
The only down side is that I will only be able to get the information that the charger asks the meter for. The meter I am about to install have a lot of interesting registers. With the “man-in-the-middle”-solution I am trying to achieve I will be able to get all the data I want and at the same time the charger will get the data it needs.

True. In my case the Newmotion controller requests all relevant information (per phase currents, voltages, total consumption,…) so that is not an issue for me. My main concern was that an issue with my esp solution should never interfere with the charge controller. That is why I chose a passive listener.

If it is Modbus RTU over RS485 it supports multidrop communication so you shouldn’t need to use two interfaces. And you should be able to request the information you want on the RS485 bus without being limited to only the queries asked by the charger.

Yes it is Modbus RTU (RS485). But Modbus RTU only allows one client but many servers on the same bus. Modbus TCP on the other hand you can have more than one client.

You are right. Hope you get it working with ESPhome.