Successfully Integrating ESPHome with ModBus: A Step-by-Step Guide

I’ve recently embarked on a project to integrate my Imeon inverter with ESPHome using the ModBus RTU protocol, and I wanted to share my journey and some tips for anyone looking to do something similar. While the process had its challenges, it was ultimately rewarding and taught me a lot about working with custom protocols in ESPHome.

The Challenge

I wanted to retrieve data from my Imeon inverter, specifically the serial number and voltage readings, but I encountered some issues with the frames being sent and received. After some research, I found that using QModMaster was a great way to test and visualize the frames before implementing them in ESPHome. Here’s what I learned:

Understanding the Frames

Using QModMaster, I was able to send and receive frames to and from my inverter. This helped me identify the correct ModBus addresses and commands needed to retrieve the data I wanted. For example, to retrieve the serial number, I used the following frame:
plaintext
[RTU]>Tx > 18:45:38:609 - 01 03 00 01 00 04 15 C9
[RTU]>Rx > 18:45:38:857 - 01 03 08 00 00 50 5D 2D 3C 7F EF DC 96

I noticed that ESPHome was appending an extra 00 at the end of the frame, which I initially thought might be causing issues. However, after some testing, I realized that this was normal and didn’t interfere with the data retrieval.

ESPHome Configuration

Here’s the ESPHome code I used to set up the sensors:
yaml
uart:
id: mod_bus
tx_pin: 17
rx_pin: 16
baud_rate: 9600
stop_bits: 1
debug: true

modbus:
id: modbus1
flow_control_pin: 5

sensor:

  • platform: modbus_controller
    modbus_controller_id: modbus1
    name: “serial number”
    id: serial_number
    custom_command: [0x01, 0x03, 0x00, 0x01, 0x00, 0x04, 0x15]
    value_type: U_QWORD_R

  • platform: modbus_controller
    modbus_controller_id: modbus1
    name: “Voltage”
    id: voltage
    custom_command: [0x01, 0x03, 0x01, 0x00, 0x00, 0x01, 0x85]
    value_type: U_WORD
    unit_of_measurement: V

I experimented with different value_type settings and found that using U_QWORD_R for the serial number and U_WORD for the voltage worked best for my setup.

Troubleshooting Tips

  1. Frame Analysis: Always test your frames in a tool like QModMaster before implementing them in ESPHome. This helps ensure that your commands are correct and that you’re accessing the right data.
  2. ESPHome Logs: Enable debugging in ESPHome to get detailed logs of the ModBus communication. This was invaluable in identifying why certain frames weren’t returning data.
  3. Custom Commands: Don’t be afraid to experiment with custom commands. Start with small, simple requests and gradually build up to more complex ones.

Final Thoughts

Integrating ESPHome with ModBus was a fantastic learning experience. It allowed me to deeply understand how ModBus works and how to troubleshoot issues in ESPHome. If you’re looking to work with custom protocols, I highly recommend starting with a tool like QModMaster and gradually moving to ESPHome once you’re confident in your commands.

If anyone has questions or tips about working with ModBus in ESPHome, I’d love to hear from you! Happy coding! :rocket: