Ability to add multiple modbus hubs

Yup, looks like this can be applied to any component. I’m looking at implementing this for Octoprint…

I have 3 Modbus Clients now. Working good! I made a few more code changes. Seems like it helped reduce memory use.

Hi

I’ve been looking for this for ages.

Tried it out and it works. Yay.

Amazing work.

Thank you. Thank you. Thank you.

Celebrated too quickly.

Getting errors from both devices now. Reverting to original config.
At least I was getting some readings on the second device.

Hi

Tried again today but still not getting any readings at all.

This is my configuration.yaml entry:

# Modbus sensors in sensors.yaml
modbus:
  type: serial
  method: rtu
  port: /dev/ttyUSB0
  baudrate: 9600
  stopbits: 1
  bytesize: 8
  parity: N
  scan_interval: 30

modbus1:
  type: serial
  method: rtu
  port: /dev/ttyUSB0
  baudrate: 9600
  stopbits: 1
  bytesize: 8
  parity: N
  scan_interval: 30

This is the sensor entry:

#Modbus SDM230 ##########
- platform: modbus
  registers:
  - name: 'Grid Voltage'
    unit_of_measurement: Volts
    slave: 1
    register: 0
    register_type: input
    count: 2
    data_type: float
  - name: 'Grid Current'                                                    
    unit_of_measurement: Amps
    slave: 1                                                                
    register: 6
    register_type: input                                                        
    count: 2                                                                 
    data_type: float 
  - name: 'Grid Power'                                                    
    unit_of_measurement: Watts
    slave: 1                                                                
    register: 12
    register_type: input                                                        
    count: 2                                                                 
    data_type: float 
  - name: 'Grid Import Energy Raw'                                                    
    unit_of_measurement: kWh
    slave: 1                                                                
    register: 72
    register_type: input                                                        
    count: 2    
    data_type: float 
    precision: 2 
    offset: 36481.26
  - name: 'Grid Export Energy Raw'                                                    
    unit_of_measurement: kWh
    slave: 1                                                                
    register: 74
    register_type: input                                                        
    count: 2                                                                 
    data_type: float 
    precision: 2
    offset: 10274.41
  - name: 'Total Hours'
    unit_of_measurement: h
    slave: 1
    register: 63792
    register_type: holding
    count: 2
    data_type: float
    precision: 2

#Modbus SDM120 ##########
- platform: modbus1
  registers:
  - name: 'Solar Voltage'
    unit_of_measurement: Volts
    slave: 2
    register: 0
    register_type: input
    count: 2
    data_type: float
  - name: 'Solar Current'                                                    
    unit_of_measurement: Amps
    slave: 2                                                                
    register: 6
    register_type: input                                                        
    count: 2                                                                 
    data_type: float 
  - name: 'Solar Power'                                                    
    unit_of_measurement: Watts
    slave: 2                                                                
    register: 12
    register_type: input                                                        
    count: 2                                                                 
    data_type: float 
  - name: 'Solar Import Energy Raw'
    unit_of_measurement: kWh
    slave: 2                                                                
    register: 72
    register_type: input                                                        
    count: 2                                                                 
    data_type: float 
    precision: 2                                                             
    offset: 21326.51

Both modbus1.py files have been copied from monkey-house’s post into their correct locations.

The only thing that I haven’t done is change ownership as suggested. I’m on Hassio and the files belong to ‘root’. Not sure if I need to change that.

I’m getting this in the Log

2018-10-17 14:47:39 ERROR (SyncWorker_15) [homeassistant.components.sensor.modbus] No response from modbus slave 1, register 72
2018-10-17 14:47:42 ERROR (SyncWorker_7) [pymodbus.factory] Unable to decode response Modbus Error: Unknown response 68
2018-10-17 14:47:45 ERROR (SyncWorker_7) [pymodbus.factory] Unable to decode response Modbus Error: Unknown response 68
2018-10-17 14:47:48 ERROR (SyncWorker_7) [pymodbus.factory] Unable to decode response Modbus Error: Unknown response 68
2018-10-17 14:47:48 ERROR (SyncWorker_7) [homeassistant.components.sensor.modbus] No response from modbus slave 1, register 74
2018-10-17 14:47:48 ERROR (SyncWorker_10) [custom_components.sensor.modbus1] No response from modbus slave 2, register 0
2018-10-17 14:47:51 ERROR (SyncWorker_8) [pymodbus.factory] Unable to decode response Modbus Error: Unknown response 68
2018-10-17 14:47:54 ERROR (SyncWorker_8) [pymodbus.factory] Unable to decode response Modbus Error: Unknown response 68
2018-10-17 14:47:57 ERROR (SyncWorker_8) [pymodbus.factory] Unable to decode response Modbus Error: Unknown response 68
2018-10-17 14:47:57 ERROR (SyncWorker_11) [custom_components.sensor.modbus1] No response from modbus slave 2, register 6
2018-10-17 14:47:57 ERROR (SyncWorker_8) [homeassistant.components.sensor.modbus] No response from modbus slave 1, register 0
2018-10-17 14:48:00 ERROR (SyncWorker_3) [pymodbus.factory] Unable to decode response Modbus Error: Unknown response 68
2018-10-17 14:48:03 ERROR (SyncWorker_3) [pymodbus.factory] Unable to decode response Modbus Error: Unknown response 68
2018-10-17 14:48:04 WARNING (MainThread) [homeassistant.components.sensor] Updating modbus1 sensor took longer than the scheduled update interval 0:00:30
2018-10-17 14:48:04 WARNING (MainThread) [homeassistant.components.sensor] Updating modbus sensor took longer than the scheduled update interval 0:00:30
2018-10-17 14:48:06 ERROR (SyncWorker_2) [custom_components.sensor.modbus1] No response from modbus slave 2, register 72
2018-10-17 14:48:06 ERROR (SyncWorker_3) [pymodbus.factory] Unable to decode response Modbus Error: Unknown response 68
2018-10-17 14:48:06 ERROR (SyncWorker_3) [homeassistant.components.sensor.modbus] No response from modbus slave 1, register 12
2018-10-17 14:48:06 ERROR (SyncWorker_9) [pymodbus.factory] Unable to decode response Modbus Error: Unknown response 68
2018-10-17 14:48:06 ERROR (SyncWorker_9) [pymodbus.factory] Unable to decode response Modbus Error: Unknown response 68
2018-10-17 14:48:06 ERROR (SyncWorker_9) [pymodbus.factory] Unable to decode response Modbus Error: Unknown response 68
2018-10-17 14:48:06 ERROR (SyncWorker_9) [homeassistant.components.sensor.modbus] No response from modbus slave 1, register 6
2018-10-17 14:48:06 ERROR (SyncWorker_17) [pymodbus.factory] Unable to decode response Modbus Error: Unknown response 68
2018-10-17 14:48:06 ERROR (SyncWorker_17) [pymodbus.factory] Unable to decode response Modbus Error: Unknown response 68
2018-10-17 14:48:06 ERROR (SyncWorker_17) [pymodbus.factory] Unable to decode response Modbus Error: Unknown response 68
2018-10-17 14:48:06 ERROR (SyncWorker_17) [homeassistant.components.sensor.modbus] No response from modbus slave 1, register 63792

What am I doing wrong?

Thank you for any suggestions.

Hi

Been trying to get this to work on my 2 modus devices without success.

They’re connected via serial rtu.

Would you post your corrected modbus1 files. as I’m on Hassio.

Thank you.

Hi,
I don’t think you need to do all of this to be able to talk to your two modbus slaves if they are both connected using serial interface and using the same communication parameters… I had to add modbus1 because I am using devices that are connected both on serial and tcp… In your case it should be enough to have modbus serial interface and then simply address slave 1 and slave 2 from that… (?) are you sure that they are communicating with 9600 bps, 1 stopbit and no parity?
I can post the files when I get back to my computer.

Hi Mgdfp

Thank you for your reply.

That is certainly some useful information.

The connection settting should be fine but I’ll check again.

The thing is that slave 2 is being read ok just not all the time. As a consequence I get continuous errors from slave 2 and a graph that looks like this:
06

Should you have any ideas it would be most appreciated.

Thank you.

Yeah, I see what you mean and I have something of a similar problem. SDM120s or SDM230s that are reporting “no resplonse” periodically.

https://community.home-assistant.io/t/no-response-from-modbus-slave-2-register-0/58943/7

I have a plan for how I want to get around this but so far I have not had time to do anything, I will post in that thread since it is more relevant.

Hello,

The solution I found is to add multiple “sensor networks”. Each network containing multiple slaves. This allows you for example to have a RTU network at 9600 baud, and RTU network at 115200, and a TCP network all in the same HA instance. The various sensor networks will require their own usb to serial or tcp/ip to serial adapter connected to the Pi or whatever hardware you are using.

I know very little about Hassio as it is not a robust enough solution for the many things I do with HA.

I also believe that the random no response or the failing to clear buffer is a pymodbus issue.

This could also be related to line biasing on the communication wire. These systems usually require proper communication cable and end of line termination (120 Ohms if proper cable is used).

If there are further questions let me know.

Thanks so much [monkey-house] and others for your invaluable help with this problem I was having. Initially I could not make this work as you have described here but after much time wasted I found I made a stupid typo in the target folder, your “custom_components” became “custom_component” in my home assistant config area. Stupid me!

Anyway so far I have 3 PLC’s connected with 2 more to go go and 350 odd tags to connect to.

Again many thanks!

PS: If you get errors in your logs I suggest to look at how your PLC is configured as the third one I connected caused errors. It was a silly ethernet reset I had configured in my plc which once ditched everything came good.

This is a wonderful community here…never forget that people!

1 Like

I deal with modbus pretty often in my day job and while we always spec Belden 3106A (or equivalent) for the modbus cable pulls, that doesn’t always happen; same with the termination resistors, and I rarely see it cause this type of issue. In cases where improper cable is used, or lack of EOL termination, comm issues can typically be resolved by bumping down the baud rate (9600 baud, which I’m using and getting these errors, is already pretty slow), or increasing the RX/TX/etc delays, which is not possible with the current modbus RTU implementation. Also, the termination resistors are more critical on longer runs, with many nodes, and on a bus with only a few nodes, the signal reflection issue necessitating the impedance matched resistor is pretty minimal.

To be fair though, I’ll add EOL termination resistors on my network tonight and see if things improve (I’m already using Belden 3106A).

Update: I added the EOL terminator with no change.

I bought a USR-W610 to convert the RS485 to modbus TCP/IP and now everything is working fine. I’m using the exact same wires (same length) and no terminator to go to the converted instead of the the USB to serial device and I havent seen an error in the past 3 days of using it.

The modbus RTU component is definitely broken IMHO.

@Silicon_Avatar,

In my day job xp I have noticed that the proper cable and termiation can make all the difference depending on the sensor, baud and quality of the master. (I have seen improper cable and terminations work and length of run does matter for EOL terms.) I have never heard of changing the baud rate, as only matching the baud rate of the master to the baud rate of the slave has ever worked for me. Adjusting the RX/TX delays, while an option in some masters or adapters has never been necessary for me and my line of work.

I have had great success with RTU using this for the adapter.

I have also connected Modbus RTU sensors via Modbus TCP with this running on the same pi as HA.

The only issues, which are not really issues, occur when connecting to devices via RTUoverTCP. It still worked well, but errors came up in the logs. This could have been because I changed the logging level for the Modbus component to show more.

One way to fix other potential issues is to change this line REQUIREMENTS = ['pymodbus==1.3.1'] to the latest version of pymodbus (The last time I tested this was v.1.5.2) but a restart is required.

To date I have tested 3 simultaneous Modbus sensor networks running on a single instance of HA.

Note* I have had to change the reverse_order and structure lines to get certain sensors to work.

Let me know if you have any questions.

Regards,

PTP

1 Like

Ok, so it’s now broken in 0.87.0 :frowning: It was working great in 0.86.x
I readjusted the new files as follow…

  • /config/custom_components/modbus1.py:

    REQUIREMENTS = [‘pymodbus==1.3.1’]

with:

REQUIREMENTS = ['pymodbus==1.5.2']

In the following files

  • /config/custom_components/switch/modbus1.py
  • /config/custom_components/sensor/modbus1.py
  • /config/custom_components/binary_sensor/modbus1.py
  • /config/custom_components/climate/modbus1.py

Replace

from homeassistant.components import modbus

with:

import custom_components.modbus1 as modbus

It looks like it’s now working :smiley:

1 Like

I am glad someone smarter than me added support for multiple hubs! Thanks HA contributor!

The ability to write either single values or arrays to registers has also been sorted…well done team!

Can I ask what version of homeassistant would I need to be running?

Thanks in advance!

Hi all, I’ve confirmed that I’m running 0.88.2 and looking at the docs, multiple hubs should now work.

However with my config of -
modbus:

  • type: tcp
    host: 172.17.1.202
    port: 502
    hub: default
    name: sma_battery

I get an error of -
Invalid config for [modbus]: extra keys not allowed @ data[‘modbus’][0][‘host’]. Got ‘172.17.1.202’
extra keys not allowed @ data[‘modbus’][0][‘hub’]. Got ‘default’
not a valid value for dictionary value @ data[‘modbus’][0][‘type’]. Got ‘tcp’
required key not provided @ data[‘modbus’][0][‘baudrate’]. Got None
required key not provided @ data[‘modbus’][0][‘bytesize’]. Got None
required key not provided @ data[‘modbus’][0][‘method’]. Got None
required key not provided @ data[‘modbus’][0][‘parity’]. Got None
required key not provided @ data[‘modbus’][0][‘stopbits’]. Got None. (See /config/configuration.yaml, line 21). Please check the docs at https://home-assistant.io/components/modbus/

Am I doing something wrong? Thanks in advance.

Same issue with 0.89.0.

In the documentation it cites

For serial…

Example configuration.yaml entry for a serial connection

modbus:
name: hub1
type: serial
method: rtu
port: /dev/ttyUSB0
baudrate: 9600
stopbits: 1
bytesize: 8
parity: N

Then for TCP…

Example configuration.yaml entry for multiple TCP connections

modbus:

  • type: tcp
    host: IP_ADDRESS_1
    port: 2020
    hub: hub1

  • type: tcp
    host: IP_ADDRESS_2
    port: 501
    hub: hub2

Bit confusing I know…for serial it’s name: hub1 while for TCP it’s hub: hub1

But for your config I would try:

modbus:
type: tcp
host: 172.17.1.202
port: 502
hub: your_hub_name

Then…Leave out the item…

name: your_name

Try that I think.

Cheers for trying wellsy, I thought I’d tried that before so I just did it again -
modbus:

  • type: tcp
    host: 172.17.1.202
    port: 502
    hub: hub1

gives the following error -
Invalid config for [modbus]: extra keys not allowed @ data[‘modbus’][0][‘host’]. Got ‘172.17.1.202’
extra keys not allowed @ data[‘modbus’][0][‘hub’]. Got ‘hub1’
not a valid value for dictionary value @ data[‘modbus’][0][‘type’]. Got ‘tcp’
required key not provided @ data[‘modbus’][0][‘baudrate’]. Got None
required key not provided @ data[‘modbus’][0][‘bytesize’]. Got None
required key not provided @ data[‘modbus’][0][‘method’]. Got None
required key not provided @ data[‘modbus’][0][‘parity’]. Got None
required key not provided @ data[‘modbus’][0][‘stopbits’]. Got None. (See /config/configuration.yaml, line 21). Please check the docs at https://home-assistant.io/components/modbus/