Ability to add multiple modbus hubs

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/

This is fairly fresh implementation of modbus hubs and as I am using the OLD workaround then I think that’s about as much as I can help sorry.

The only other thing to double check…is your indentation correct?

Maybe someone else who has used this successfully may drop by?

Quick update on this subject.
Today I installed HA 0.89.2 in a new docker on my QNAP NAS.
The install went well and I have my fallback installation ready to start again if needed.

After logging in I updated the config.yaml with all my sensors, switches and binary sensors and found all my MQTT devices were working as expected. Also as expected all the modbus devices were not.

So I modified the modbus connection information as per the instructions in the documentation and added the hub: myHub info to all my devices.

On starting up the container and logging in again I found not one modbus device was connected and there were multiple errors and warnings in the log.

I had a good look at the documentation again

# 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

and after some thought I decided to try the below

#
# 
#Start Modbus Hub5
#########################################
modbus:
  - type: tcp
    host: myIP
    port: 5003
    name: hub5
#########################################    
# Meter Box PLC 
#IP: myIP
#Local Port: 5003
#Slot: 5    
#########################################
#End Modbus Hub5
#    
#Start Modbus Hub3
#########################################
  
  - type: tcp
    host: myIP
    port: 5001
    name: hub3
#########################################
# Master PLC
#IP: myIP
#Local Port: 5001
#Slot: 3
#########################################
#End Modbus Hub3                             
#
#Start Modbus Hub1
#########################################
  
  - type: tcp
    host: myIP
    port: 5002
    name: hub1
#########################################    
# Slave PLC 
#IP: myIP
#Local Port: 5002
#Slot: 1
#########################################
#End Modbus Hub1
#

On starting up and checking there were a couple of minor warnings and one error but it seemed like all the devices were connected and working in dev-state anyway.

A quick look through my ui showed random devices disconnected and others working.

Happy with that for tonight so I reverted to my existing (working) container for the night.

I will report back with more details once it’s all sorted.

UPDATE: Look here and the example config shown matches what I came up with that works for me. It appears the current modbus documentation may be incorrect or needs to be updated?

Cheers Steve Wells

Having a very quick look this morning and fixed a few typos I made in updating my config.yaml.

Result is that binary_sensors are the remaining problem which does gel with the one remaining error I have in my logs.

Invalid config for [binary_sensor.modbus]: expected int for dictionary value @ data['coils'][22]['coil']. Got None. (See /config/configuration.yaml, line 1268). Please check the docs at https://home-assistant.io/components/binary_sensor.modbus/

This is a snippet of what I have for my binary_sensor configuration…

#Start Binary Sensors
#########################################
#
binary_sensor:
#
#Modbus binary sensors
# NOTE: this is line 1268 referenced in the error message
  - platform: modbus
    coils:
#
#START hub5 binary sensors
#    
      - name: HW Tariff
        hub: hub5
        slave: 5
        coil: 1538
      - name: Front Occupied
        hub: hub5
        slave: 5
        coil: 1544

#END Modbus hub1 binary sensors

#After all the modbus sensors I have the MQTT ones
#START MQTT binary sensors
#
  - platform: mqtt
    name: "Pool Low Level"
    state_topic: "stat/sonoff23/POWER"
#    command_topic: "cmnd/sonoff23/POWER"
    availability_topic: "tele/sonoff23/LWT"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    payload_available: "Online"
    payload_not_available: "Offline"

To be honest I see no difference to the documentation example given for modbus binary sensors

For comparison sake, this is what I have for modbus sensors in my config…again all sensors are normal as are switches.

#########################################      
#
#
#START Modbus sensors
#
#
  - platform: modbus
    scan_interval: 10
    registers:
#########################################
#START Modbus hub5 sensors
#    
      - name: RTC YEAR
        hub: hub5
        slave: 5
        register: 3328
      - name: RTC MONTH
        hub: hub5
        slave: 5
        register: 3329

Can anyone advise a solution?

PS: My MQTT binary sensors are all unaffected it seems?

UPDATE: I had a look at the code in binary_sensor.py and I am wondering if (after reading the error message) it may be a matter of how the config items are ordered?

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
    vol.Required(CONF_COILS): [{
        vol.Required(CONF_COIL): cv.positive_int,
        vol.Required(CONF_NAME): cv.string,
        vol.Optional(CONF_HUB, default=DEFAULT_HUB): cv.string,
        vol.Optional(CONF_SLAVE): cv.positive_int,
    }]
})

Maybe the config needs to be ordered in the same order as the platform schema above IE:

binary_sensor:
  - platform: modbus
    scan_interval: 10
    coils:
      - coil: 100
        name: Sensor1
        hub: hub1
        slave: 1

I’m off to try it anyway!
FURTHER UPDATE: My theory failed…modbus binary sensors appear to need some work it seems.