ESPhome modbus, how to use the service?

Hi,

I am using a M5stack core device to which I flashed ESPhome onto to which I want to send some variables to via Modbus. However I can’t get the right value to be send to the sensor I created.

image

I connected a HW-097 module with jumper cables, similar to below image.

I plugged in a USB to serial modbus device in my Home Assistant server, similar to this one:

image

This is my ESPHome .yaml code:


 esphome:
   name: m5stack-fire
   platform: ESP32
   board: m5stack-core-esp32
 
 # Enable logging
 logger:
 
 
 # Enable Home Assistant API
 api:
 
 ota:
   password: "xxx"
 
 wifi:
   ssid: "xxx"
   password: "xxx"
 
 # Enable fallback hotspot (captive portal) in case wifi connection fails
   ap:
     ssid: "M5Stack Fallback Hotspot"
     password: "xxx"
 
 captive_portal:
 
 time:
   - platform: sntp
     id: sntp_time
     
 uart:
   id: mod_bus
   tx_pin: 17
   rx_pin: 16
   baud_rate: 9600
   stop_bits: 1
 
 modbus:
   id: modbus1
   flow_control_pin: 5
 
 modbus_controller:
   - id: modbuscontrollerm5stack
     ## the Modbus device addr
     address: 0x1
     modbus_id: modbus1
 
 sensor:
   - platform: modbus_controller
     modbus_controller_id: modbuscontrollerm5stack
     name: "Woonkamer Temperatuur"
     id: woonkamer_temperatuur
     register_type: holding
     address: 0x40001
     value_type: U_WORD

This is the Modbus part in my configurations.yaml

# Example configuration.yaml entry for a serial connection
modbus:
  - name: hub1
    type: serial
    baudrate: 9600
    bytesize: 8
    method: rtu
    parity: N
    port: "/dev/serial/by-id/usb-1a86_USB2.0-Ser_-if00-port0"
    stopbits: 1

The esphome sensor “Woonkamer Temperatuur” pops up in my Home Assistant. When I try to send a value with the Service tab under Developer Tools, i am using the following:

service: modbus.write_register
data:
  value: 20
  hub: hub1
  address: 40001
  unit: 1

The thing that happens is that what I fill at address, is often set as the value for the sensor. But not always. So at least something happens and data connection is there.

However I think it has something to do with the array of bytes which will sometimes lead to a setting of the register and sometimes not. It seems like a coincidence that sometimes a readable signal is sent for the slave.

Does anyone know if should set the bytesize/parity or stopbits different in Configuration.yaml? Do I maybe have to set the value or the address in a different form? Or does the Value_type (currently U_WORD) have to be set differently?

40001 is decimal

4 is holding
0001 decimal address

Maybe it is 0x0 hex

40001 works for you?

Hi @nikito7 ,

Can you elaborate on that? What values should I choose in the service variables according to you?

This is working for you?

No, when I use the service parameters as stated it displays 40001 as value, where it should be 20.

First test with sensor only

And try address 1 or 0

Or I don’t really understand the problem :laughing:

Both ESPhome and HA are masters

hmm could be, I just reckoned ESPHome devices are always slaves and I haven’t read anything about it on the ESPHome Modbus documentation.

When I look at the ESPHome log I see the following:

What is remarkable is that the address for the controller is being shown. However the address for the Sensor isn’t shown, while other properties of the sensor are. Not sure if that says anything?

You can’t connect esphome to HA via modbus

Esphome only convert serial modbus to wifi (ha api) or mqtt

If you need that you need a alternative firmware in esp

Hi, someone know if there is the possibility to use modbus tcp in esphome? I mean modbus master tcp instead rtu…