home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
73.47k stars 30.69k forks source link

Modbus rtuovertcp ModbusTcpClient: Connection unexpectedly closed x seconds into read of 2 bytes without response from unit before it closed connection #57426

Closed ericvb closed 3 years ago

ericvb commented 3 years ago

The problem

Trying to debug the problem and to understand what is happening in the logfile.

I have HA running on a Ubuntu OS with IP 192.168.1.x An Arduino UNO acts like a RTUOverTCP (https://github.com/budulinek/arduino-modbus-rtu-tcp-gateway) and is connected (serial) with a modbus enabled SDM630 powermeter. The Arduino runs in another VLAN 192.168.x.x. HA is configured to connect to this Arduino RTUOverTCP.

Everything works, but regurarly the device sensor has Unavailable and when consulting the logfile 2021-10-10 14:17:08 ERROR (SyncWorker_9) [homeassistant.components.modbus.modbus] Pymodbus: sdm630_laadpaal: Modbus Error: [Connection] ModbusTcpClient(192.168.x.x:502): Connection unexpectedly closed 0.000026 seconds into read of 2 bytes without response from unit before it closed connection So I activated the logging in HA logger: default: info logs: homeassistant.components.modbus: debug pymodbus.client: debug

What is version of Home Assistant Core has the issue?

2021.10.3

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant Core

Integration causing the issue

Modbus

Link to integration documentation on our website

https://www.home-assistant.io/integrations/modbus

Example YAML snippet

# modbus communication
modbus:
  - name: "sdm630_laadpaal"
    close_comm_on_error: false
    retry_on_empty: true
    retries: 10
    delay: 10
    message_wait_milliseconds: 1000
    timeout: 10
    type: rtuovertcp
    host: 192.168.x.x
    port: 502
    sensors:
      - name: sdm630_phase_1_line_to_neutral_volts
        slave: 1
        address: 0
        count: 2
        input_type: input
        precision: 2
        data_type: float32
        unit_of_measurement: V
        device_class: voltage
        scan_interval: 60

Anything in the logs that might be useful for us?

2021-10-10 13:53:24 INFO (MainThread) [homeassistant.setup] Setting up modbus
2021-10-10 13:53:24 DEBUG (SyncWorker_2) [pymodbus.client.sync] Connection to Modbus server established. Socket ('192.168.1.x', 51631)
2021-10-10 13:53:24 WARNING (SyncWorker_2) [homeassistant.components.modbus.modbus] modbus sdm630_laadpaal communication open
2021-10-10 13:56:09 DEBUG (SyncWorker_17) [pymodbus.client.sync] New Transaction state 'SENDING'
2021-10-10 13:57:08 DEBUG (SyncWorker_6) [pymodbus.client.sync] New Transaction state 'SENDING'
2021-10-10 13:57:08 ERROR (SyncWorker_6) [homeassistant.components.modbus.modbus] Pymodbus: sdm630_laadpaal: Modbus Error: [Connection] ModbusTcpClient(192.168.x.x:502): Connection unexpectedly closed 0.000042 seconds into read of 2 bytes without response from unit before it closed connection
2021-10-10 13:58:08 DEBUG (SyncWorker_15) [pymodbus.client.sync] Connection to Modbus server established. Socket ('192.168.1.x', 39143)
2021-10-10 13:58:18 DEBUG (SyncWorker_15) [pymodbus.client.sync] New Transaction state 'SENDING'
2021-10-10 13:59:08 DEBUG (SyncWorker_5) [pymodbus.client.sync] New Transaction state 'SENDING'
2021-10-10 13:59:08 ERROR (SyncWorker_5) [homeassistant.components.modbus.modbus] Pymodbus: sdm630_laadpaal: Modbus Error: [Connection] ModbusTcpClient(192.168.x.x:502): Connection unexpectedly closed 0.001629 seconds into read of 2 bytes without response from unit before it closed connection
2021-10-10 14:00:08 DEBUG (SyncWorker_17) [pymodbus.client.sync] Connection to Modbus server established. Socket ('192.168.1.x', 49953)
2021-10-10 14:00:18 DEBUG (SyncWorker_17) [pymodbus.client.sync] New Transaction state 'SENDING'
2021-10-10 14:01:08 DEBUG (SyncWorker_18) [pymodbus.client.sync] New Transaction state 'SENDING'
2021-10-10 14:01:08 ERROR (SyncWorker_18) [homeassistant.components.modbus.modbus] Pymodbus: sdm630_laadpaal: Modbus Error: [Connection] ModbusTcpClient(192.168.x.x:502): Connection unexpectedly closed 0.000034 seconds into read of 2 bytes without response from unit before it closed connection
2021-10-10 14:02:08 DEBUG (SyncWorker_18) [pymodbus.client.sync] Connection to Modbus server established. Socket ('192.168.1.x', 58381)
2021-10-10 14:02:18 DEBUG (SyncWorker_18) [pymodbus.client.sync] New Transaction state 'SENDING'
2021-10-10 14:03:08 DEBUG (SyncWorker_0) [pymodbus.client.sync] New Transaction state 'SENDING'
2021-10-10 14:03:08 ERROR (SyncWorker_0) [homeassistant.components.modbus.modbus] Pymodbus: sdm630_laadpaal: Modbus Error: [Connection] ModbusTcpClient(192.168.x.x:502): Connection unexpectedly closed 0.000034 seconds into read of 2 bytes without response from unit before it closed connection
2021-10-10 14:04:08 DEBUG (SyncWorker_7) [pymodbus.client.sync] Connection to Modbus server established. Socket ('192.168.1.x', 60057)
2021-10-10 14:04:18 DEBUG (SyncWorker_7) [pymodbus.client.sync] New Transaction state 'SENDING'
2021-10-10 14:05:08 DEBUG (SyncWorker_9) [pymodbus.client.sync] New Transaction state 'SENDING'
2021-10-10 14:05:08 ERROR (SyncWorker_9) [homeassistant.components.modbus.modbus] Pymodbus: sdm630_laadpaal: Modbus Error: [Connection] ModbusTcpClient(192.168.x.x:502): Connection unexpectedly closed 0.000035 seconds into read of 2 bytes without response from unit before it closed connection
2021-10-10 14:06:08 DEBUG (SyncWorker_12) [pymodbus.client.sync] Connection to Modbus server established. Socket ('192.168.1.x', 52493)

Additional information

Above logging continues and continues... I've been testing with different values for the delay, retries, timeout, close_com_on_error, .... For the moment the configuration is set to retrieve only one sensor from the SDM630, but always the same error

probot-home-assistant[bot] commented 3 years ago

modbus documentation modbus source (message by IssueLinks)

probot-home-assistant[bot] commented 3 years ago

Hey there @adamchengtkc, @janiversen, @vzahradnik, mind taking a look at this issue as it has been labeled with an integration (modbus) you are listed as a code owner for? Thanks! (message by CodeOwnersMention)

janiversen commented 3 years ago

It seems your UNO is not responding, did you try to debug on the UNO.

ericvb commented 3 years ago

It could be the UNO, that can not follow if multiple requests. For that I tested with only one sensor defined in the yaml and a scan interval of 60 seconds, but same problem. Don't know if I can debug the UNO, the code is opensource, it should be possible.

Do you have practical experience with this sort of setup : HA with modbus rtuovertcp <--> modbus rtu over tcp gateway <--> serial modbus device. If yes, what for modbus rtu over tcp gateway did you use? Self-build or commercial product?

janiversen commented 3 years ago

I have heard of several that use a “converter”, but I cannot tell you which types etc. for that you need to ask in the community. I have a test setup with a software converter, but that is merely to test the code.

Surely the UNO software have some kind of debug log as well, typically the write directly to the serial port.

ericvb commented 3 years ago

Hi @janiversen, I've talked to the maker of the UNO gateway software and he had done just corrections to his code. So I installed his new version, and the problem is gone, no more errors in the logfiles!