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.

Hi,
Did you finish your project? I need to make something similar and I’m wondering if it is possible with ESPHome.

Unfortunately not. This project has very low priority at the moment.

The reason is that my electrical company replaced my meter to one that has a P1-port, so I can now access all that data from the meter instead. No need to use the one for the EV-charger.

I’m thinking of using this component in a different way while using the same ingredients.
Would it be possible to take the P1 measurements from Home-Assistant, read it with the ESP and convert them to modbus so it can be fed into the charger as if it was the PowerBoost module that is connected.
The cost for the meter are somewhere around the €200 and it will measure exactly the same thing as the P1 port can already provide.

Did you try the .yaml you provided and did the charger respond? Or do you only have the charger connected directly to the meter without the ESP in between?

No. I never tried it at all. The PowerBoost was delivered first in December, and at that time I knew I was about to get the new meter, so I never continued with this project.

But by looking at the display on the PowerBoost I can tell you that it reports negative values if I am selling power to the grid (I have solar PV).

If you have the same charger as I (Wallbox Pulsar) it is compatible with OCCP, wich you can find a custom integration for in HACS. Then you should be able to set max power. Or you can do it via the offical integration in HA. No need for you to build a Modbus node.
The downside of the official integration is that it is using the cloud service, which is a bit slow when you update the max power.

I have a similar challenge with a pool heat pump (there is a modbus link between the touchscreen and the main board which I need to sniff).

I’ve created a branch of the standard esphome modbus / modbus_controller modules which adds a “disable_send” feature. You can then setup a sensor which will send both the master request and the slave response that it detects on the bus.

The current version is very hacky and not documented. If there is interest I will document.

repo here: https://github.com/pcr20/esphome/tree/dev

Neat! I think my Tropical Heat Pump works the same way. I gave up last season trying to read the modbus, might just break down and get their $200 cloud (WiFi) to modbus adapter.

Hello,
I am having issues getting your modified version of modbus controller working.
As soon as i configure a controller with the address of an existing controller, it crash and reboot.
If i used a non existing address, logs get spammed with unknown modbus devices so that part is working.

Would you know why ?

Also, is it working with multiple modbus controllers on the same modbus ?