Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modbus: Connection unexpectedly closed #87265

Closed
kriskristoferson opened this issue Feb 3, 2023 · 23 comments
Closed

Modbus: Connection unexpectedly closed #87265

kriskristoferson opened this issue Feb 3, 2023 · 23 comments
Assignees

Comments

@kriskristoferson
Copy link

The problem

After restarting HA, a few reads are performed correctly, then the entity becomes unavailable forever.

Logger: homeassistant.components.modbus.modbus
Source: components/modbus/modbus.py:388
Integration: Modbus (documentation, issues)
First occurred: 09:57:35 (1 occurrences)
Last logged: 09:57:35

Pymodbus: Solar: Modbus Error: [Connection] ModbusTcpClient(192.168.2.134:502): Connection unexpectedly closed 3.409385681152344e-05 seconds into read of 8 bytes without response from unit before it closed connection

What version of Home Assistant Core has the issue?

2023.2

What was the last working version of Home Assistant Core?

2023.1.7

What type of installation are you running?

Home Assistant OS

Integration causing the issue

modbus

Link to integration documentation on our website

No response

Diagnostics information

No response

Example YAML snippet

- name: Solar
    type: tcp
    host: 192.168.2.134
    port: 502
    timeout: 15
    delay: 2
    retries: 20
    # message_wait_milliseconds: 30
    retry_on_empty: true
    # close_comm_on_error: false
    sensors:
      - name: Solar SunPower
        slave: 1
        address: 32080
        unit_of_measurement: W
        data_type: int32
        count: 2
        scale: 1
        precision: 1
        scan_interval: 10
{...}

Anything in the logs that might be useful for us?

No response

Additional information

No response

@renaudallard
Copy link

py_modbus was upgraded in 2023.2, you should check if your integration takes intto account the new version of modbus

@kriskristoferson
Copy link
Author

py_modbus was upgraded in 2023.2, you should check if your integration takes intto account the new version of modbus

I suppose something has changed but I have no idea what I could check, it's standard modbus integration, it's been working for months, now it's having trouble keeping the connection to the solar inverter, although e.g. with an air conditioner it still works without a problem

@home-assistant
Copy link

home-assistant bot commented Feb 3, 2023

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!

Code owner commands

Code owners of modbus can trigger bot actions by commenting:

  • @home-assistant close Closes the issue.
  • @home-assistant rename Awesome new title Change the title of the issue.
  • @home-assistant reopen Reopen the issue.
  • @home-assistant unassign modbus Removes the current integration label and assignees on the issue, add the integration domain after the command.

(message by CodeOwnersMention)


modbus documentation
modbus source
(message by IssueLinks)

@janiversen
Copy link
Member

Please run with debug as pr documentation, it is quite hard to help you without a debug log.

Often the disconnect happens because the device receives something it does not like.

@frenck frenck changed the title Problem with modbus after update to 2023.2 Modbus: Connection unexpectedly closed Feb 3, 2023
@kriskristoferson
Copy link
Author

Ok, short log after restarting HA with settings

logger:
  default: warning
  logs:
    homeassistant.components.modbus: debug
    pymodbus: debug
Logger: homeassistant.components.modbus.modbus
Source: components/modbus/modbus.py:388
Integration: Modbus (documentation, issues)
First occurred: 18:58:43 (1 occurrences)
Last logged: 18:58:43

Pymodbus: Solar: Modbus Error: [Connection] ModbusTcpClient(192.168.2.134:502): Connection unexpectedly closed 0.48309755325317383 seconds into read of 8 bytes without response from unit before it closed connection

home-assistant_2023-02-03T17-59-45.671Z.log

@janiversen
Copy link
Member

That cannot be ! in that case it never never connected, never send or received anything.

@janiversen
Copy link
Member

Ahh did not see the attached log.

@janiversen
Copy link
Member

Your solar device closes the connection from time to time, but it reconnects nicely so I am not sure what the problem. The transaction before and after are ok.

The new version is a lot faster, and maybe your device do not like that, anyhow this does not seem to be a modbus integration problem.

@kriskristoferson
Copy link
Author

I've tried changing various integration configuration options several times without success.
I have no idea...
Maybe the wifi dongle in the Huawei Sun 2000 inverter is not perfect. I remember some problems with modbus support at the beginning.
Maybe. After all, the same integration with the heat pump works without any problems.
But... maybe the new version doesn't have to be that fast since there have been no such problems for almost two years.

@janiversen
Copy link
Member

I have a Huawei SUN2000 connected via wifi to a HA running on a raspberry pi 4. This setup have been running for about 3 weeks without a single reconnect.

@kriskristoferson
Copy link
Author

My SUN2000-8KTL-M0 firmware was a bit outdated.
I did an update today:
dongle SPC127->SPC133
inverter SPC141->SPC150
No changes for the better. I am attaching the current logs.
Always after restarting the HA there are a few valid readings and then everything dies.
I don't know if there was any significant reason to change py_modbus, maybe it's worth going back to the older version?
Alternatively, maybe I could do it myself, if possible can I get the appropriate files and instructions?
The problem is annoying and causes incorrect energy stats in the HA.

FIle: home-assistant_2023-02-05T09-57-17.852Za.log

@janiversen
Copy link
Member

janiversen commented Feb 5, 2023

The log does not seem to have any modbus errors.

At least not by a fast scan, maybe your problem is timeout.

@janiversen
Copy link
Member

Closed as it is the device that closes the connection, but the modbus integration reconnects as it should.

@kriskristoferson
Copy link
Author

It would be nice if the issue was resolved instead of closing the ticket.
I found a vm Proxmox HA backup dated Jan 31 with version 2023.1.7, restored it for a few minutes, here are the logs.
Voila!
Inverter readings work fine all the time, entity 'sensor.solar_sunpower' doesn't become "unavailable".
So in my opinion something wrong happened with the way of reading modbus sensors along with 2023.2.
Howgh!
home-assistant_2023-02-16T18-49-16.441Z.log

@janiversen janiversen reopened this Feb 17, 2023
@janiversen
Copy link
Member

no problem reopening this, but the problem remains the same and is outside the control of the modbus integration.

Version 2923.2 contains a faster pymodbus implementation, which do affect some devices, and calls for use of e.g. the timeout parameter.

Version 2023.2, does not contain new read/write handling.

@janiversen
Copy link
Member

so in essence, please supply us with something that indicates an error in the modbus integration, otherwise we cannot really help you.

@kriskristoferson
Copy link
Author

For me this entity is nothing but modbus integration and it stopped working :)
With these parameters on 2023.1.7 it worked great for months:

timeout: 15
delay: 2
retries: 20
retry_on_empty: true

On 2023.2 this no longer works.
"timeout" seems big anyway.
Maybe "retry_on_empty" stopped working for some reason?
In the logs from 2023.1.7 I see a familiar error:

[homeassistant.components.modbus.modbus] Pymodbus: Solar: Modbus Error: [Connection] ModbusTcpClient(192.168.2.134:502): Connection unexpectedly closed 0.000094 seconds into read of 8 bytes without response from unit before it closed connection
And despite the error, the entity does not become "unavailable".
In the meantime I also tried increasing "message_wait_milliseconds" but it didn't help.

@janiversen
Copy link
Member

“Connection unexpectedly closed” means your device closed the connection. The modbus connection cannot do a lot more than accept that fact.

Could it be that you have a custom component installed that interferes, by f.x. use an old pymodbus version (we have seen that quite a lot).

@kriskristoferson
Copy link
Author

That's all I have:

obraz

@janiversen
Copy link
Member

You need to compare the last message sent in the good and the bad case. Of course remembering that transaction id and the MBA in general will be different. The payloads should be identical, in which case the only difference is timing.

I have no idea what your custom_components does, but if you look in your log, Homeassistant tell you to run without custom_components when reporting issues.

@kriskristoferson
Copy link
Author

Anomaly today :)
It probably won't bring anything new... After a few reboots yesterday, the entity lived for several hours until I rebooted HA today. He's alive again only a few moments after reboot.
I didn't change anything in the configuration at all.

obraz

@janiversen
Copy link
Member

What can I say, if you did not upgrade, there was no changes in the modbus integration. You are back into a need to analyze what happens when it happens.

@kriskristoferson
Copy link
Author

I got it :)
I found (somewhat by accident) the source of the problem.
A few months ago I tested the inverter readings with NodeRed. After that, one active node remained, it did not take readings (interval set to 99 hours) but remained active and probably somehow "talked" to the inverter (showed "connected").
Apparently this was not a problem before upgrading to version 2023.2

So far it looks good.
Sorry for the confusion.
You can close the issue.

@github-actions github-actions bot locked and limited conversation to collaborators Mar 26, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants