Integration Solar inverter huawei 2000L

Hi @Emilv2,

I was looking for a solution that you made…
I would like to implement the integration between the inverter and home assistant.
Question: Is your solution still working? I’m asking because just tried to implement and I’m receiving this error:
"Platform error sensor.huawei_solar - No module named ‘huawei_solar’"
Have I done something wrong?

Thanks

1 Like

Did you put all the files in custom_components/huawei_solar/? sensor.py, manifest.json and __init__.py should be in that folder.

Hi yes…

This is my current log

image

That means something went wrong loading the module. I just checked with the last version and it still loads for me. The custom_components folder should be in your configuration folder, next to configuration.yaml, automations.yaml etc.

I have

2020-04-21 10:32:21 ERROR (SyncWorker_4) [pymodbus.client.sync] Connection to (192.168.5.34, 502) failed: [Errno 111] Connection refused
2020-04-21 10:32:21 ERROR (SyncWorker_4) [huawei_solar.huawei_solar] failed to connect to device, is the host correct?

Should I enable anything on inverter?

No, you should not have to enable anything. Is it one of the supported devices? Or are you perhaps connected with another modbus client to it? The inverter does not support multiple connections.

I have SUN2000-8KTL-M0 software version V100R001C00SPC120. I’m only connecting with HA.

Apr 21 15:30:04 P38 hass[177]: 2020-04-21 15:30:04 ERROR (SyncWorker_5) [pymodbus.client.sync] Connection to (192.168.5.34, 502) failed: [Errno 111] Connection refused
Apr 21 15:30:04 P38 hass[177]: 2020-04-21 15:30:04 ERROR (SyncWorker_5) [huawei_solar.huawei_solar] failed to connect to device, is the host correct?
Apr 21 15:30:04 P38 hass[177]: Traceback (most recent call last):
Apr 21 15:30:04 P38 hass[177]:   File "/root/.homeassistant/deps/lib/python3.8/site-packages/huawei_solar/huawei_solar.py", line 139, in read_register
Apr 21 15:30:04 P38 hass[177]:     response = client.read_holding_registers(register, length)
Apr 21 15:30:04 P38 hass[177]:   File "/usr/local/lib/python3.8/dist-packages/pymodbus/client/common.py", line 114, in read_holding_registers
Apr 21 15:30:04 P38 hass[177]:     return self.execute(request)
Apr 21 15:30:04 P38 hass[177]:   File "/usr/local/lib/python3.8/dist-packages/pymodbus/client/sync.py", line 107, in execute
Apr 21 15:30:04 P38 hass[177]:     raise ConnectionException("Failed to connect[%s]" % (self.__str__()))
Apr 21 15:30:04 P38 hass[177]: pymodbus.exceptions.ConnectionException: Modbus Error: [Connection] Failed to connect[ModbusTcpClient(192.168.5.34:502)]
Apr 21 15:30:04 P38 hass[177]: 2020-04-21 15:30:04 ERROR (MainThread) [homeassistant.components.sensor] Error while setting up huawei_solar platform for sensor
Apr 21 15:30:04 P38 hass[177]: Traceback (most recent call last):
Apr 21 15:30:04 P38 hass[177]:   File "/root/.homeassistant/deps/lib/python3.8/site-packages/huawei_solar/huawei_solar.py", line 139, in read_register
Apr 21 15:30:04 P38 hass[177]:     response = client.read_holding_registers(register, length)
Apr 21 15:30:04 P38 hass[177]:   File "/usr/local/lib/python3.8/dist-packages/pymodbus/client/common.py", line 114, in read_holding_registers
Apr 21 15:30:04 P38 hass[177]:     return self.execute(request)
Apr 21 15:30:04 P38 hass[177]:   File "/usr/local/lib/python3.8/dist-packages/pymodbus/client/sync.py", line 107, in execute
Apr 21 15:30:04 P38 hass[177]:     raise ConnectionException("Failed to connect[%s]" % (self.__str__()))
Apr 21 15:30:04 P38 hass[177]: pymodbus.exceptions.ConnectionException: Modbus Error: [Connection] Failed to connect[ModbusTcpClient(192.168.5.34:502)]
Apr 21 15:30:04 P38 hass[177]: During handling of the above exception, another exception occurred:
Apr 21 15:30:04 P38 hass[177]: Traceback (most recent call last):
Apr 21 15:30:04 P38 hass[177]:   File "/usr/local/lib/python3.8/dist-packages/homeassistant/helpers/entity_platform.py", line 178, in _async_setup_platform
Apr 21 15:30:04 P38 hass[177]:     await asyncio.wait_for(asyncio.shield(task), SLOW_SETUP_MAX_WAIT)
Apr 21 15:30:04 P38 hass[177]:   File "/usr/lib/python3.8/asyncio/tasks.py", line 483, in wait_for
Apr 21 15:30:04 P38 hass[177]:     return fut.result()
Apr 21 15:30:04 P38 hass[177]:   File "/usr/lib/python3.8/concurrent/futures/thread.py", line 57, in run
Apr 21 15:30:04 P38 hass[177]:     result = self.fn(*self.args, **self.kwargs)
Apr 21 15:30:04 P38 hass[177]:   File "/root/.homeassistant/custom_components/huawei_solar/sensor.py", line 61, in setup_platform
Apr 21 15:30:04 P38 hass[177]:     entities.append(HuaweiSolarSensor(inverter))
Apr 21 15:30:04 P38 hass[177]:   File "/root/.homeassistant/custom_components/huawei_solar/sensor.py", line 73, in __init__
Apr 21 15:30:04 P38 hass[177]:     self._name = self._inverter.get("model_name").value
Apr 21 15:30:04 P38 hass[177]:   File "/root/.homeassistant/deps/lib/python3.8/site-packages/huawei_solar/huawei_solar.py", line 37, in get
Apr 21 15:30:04 P38 hass[177]:     response = read_register(self.client, reg.register, reg.length)
Apr 21 15:30:04 P38 hass[177]:   File "/root/.homeassistant/deps/lib/python3.8/site-packages/huawei_solar/huawei_solar.py", line 142, in read_register
Apr 21 15:30:04 P38 hass[177]:     raise ConnectionException(ex)
Apr 21 15:30:04 P38 hass[177]: huawei_solar.huawei_solar.ConnectionException: Modbus Error: [Connection] Failed to connect[ModbusTcpClient(192.168.5.34:502)]

Inverter is connected to internet by wifi.

Can’t really say anything from here :confused: The modbus implementation of those devices seems not to be very robust. You can try debugging them with python and see whether you can get it to work.

import pymodbus
import time
client = pymodbus.client.sync.ModbusTcpClient('192.168.5.34', '502')
client.connect()
time.sleep(3)
result = client.read_holding_registers(30000,15)
result.encode()

what happens if you run this code in python3? (you will need to install pymodbus first)

I think I have to be connected directly to inverter. If I’m in range I can connect directly to SUN2000 (inverter ip 192.168.8.1), but inverter is also connected to home WiFi and I see it in network with IP 192.168.5.34. It seems that only 192.168.8.1 have opened 502 port.

That should not be the case I think, it’s not for me anyway.

Hi Emilv2, All

i have this custom components working fine without any trouble

Great Great Thank You !!!

Huawei Inverter: SUN2000L-4.6KTL
Home Assistant 0.108.6 in a docker installation

I have one question: the installer technician setup a module (Gavazzi EM111) in the electrical cabinet which is connected to inverter. Thanks to this module i can see the power taken/bought from the grid too and not only how much power the panels are producing…

for example:

is it possible to get that value? the power i’m buying from public?

ps:i’m available to do some test or analysis

Bye and thanks

So you’re saying you can see this value on the Huawei app / website? I don’t see anything in their modbus documentation, so I don’t think you’ll be able to read it out through modbus.

There is also an api which has access to more information, so maybe you can access it through there, but you will need to get a different api username and password from your installer.

I don’t know if I did it correctly

root@P38:~# python3
Python 3.8.2 (default, Feb 26 2020, 04:23:39) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pymodbus
>>> import time
>>> client = pymodbus.client.sync.ModbusTcpClient('192.168.5.34', '502')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'pymodbus' has no attribute 'client'
>>> client.connect()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'client' is not defined
>>> time.sleep(3)

>>> result = client.read_holding_registers(30000,15)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'client' is not defined
>>> result.encode()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'result' is not defined
>>>

I can connect using:

but in HA i get:

2020-04-23 09:16:36 WARNING (MainThread) [homeassistant.components.sensor] Setup of sensor platform huawei_solar is taking over 10 seconds.
2020-04-23 09:16:42 ERROR (SyncWorker_6) [huawei_solar.huawei_solar] could not read register value, is an other device already connected?
2020-04-23 09:16:42 ERROR (MainThread) [homeassistant.components.sensor] Error while setting up huawei_solar platform for sensor
Traceback (most recent call last):
  File "C:\Python38\lib\site-packages\homeassistant\helpers\entity_platform.py", line 178, in _async_setup_platform
    await asyncio.wait_for(asyncio.shield(task), SLOW_SETUP_MAX_WAIT)
  File "C:\Python38\lib\asyncio\tasks.py", line 483, in wait_for
    return fut.result()
  File "C:\Python38\lib\concurrent\futures\thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "C:\Users\ligez\AppData\Roaming\.homeassistant\custom_components\huawei_solar\sensor.py", line 61, in setup_platform
    entities.append(HuaweiSolarSensor(inverter))
  File "C:\Users\ligez\AppData\Roaming\.homeassistant\custom_components\huawei_solar\sensor.py", line 80, in __init__
    self._nb_optimizers = self._inverter.get("nb_optimizers").value
  File "C:\Users\ligez\AppData\Roaming\.homeassistant\deps\Python38\site-packages\huawei_solar\huawei_solar.py", line 37, in get
    response = read_register(self.client, reg.register, reg.length)
  File "C:\Users\ligez\AppData\Roaming\.homeassistant\deps\Python38\site-packages\huawei_solar\huawei_solar.py", line 151, in read_register
    raise ReadException(message)
huawei_solar.huawei_solar.ReadException: could not read register value, is an other device already connected?

I closed python before starting HA to close connection with inverter.

Ok, what happens if you do not call sleep but try several times in a row?

from pymodbus.client.sync import ModbusTcpClient
client = ModbusTcpClient('192.168.5.34', '502')
result = client.read_holding_registers(30000,15)
result = client.read_holding_registers(30000,15)
result = client.read_holding_registers(30000,15)
result = client.read_holding_registers(30000,15)
result.encode()

If that works I don’t know what is going wrong with HA, but if not I think I know what is going on. When I wrote the package I wrote it by debugging and found out you need to probe it several times, but maybe yours is slightly different. Later I found out it just needs a delay between connect and probing, but since it worked for me I just left it like that. I’ll update my code and then you can try again.

I paste it in python window and it was about 1 second delay between result = client …

Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from pymodbus.client.sync import ModbusTcpClient
>>> client = ModbusTcpClient('192.168.8.1', '502')
>>> result = client.read_holding_registers(30000,15)
>>> result = client.read_holding_registers(30000,15)
>>> result = client.read_holding_registers(30000,15)
>>> result = client.read_holding_registers(30000,15)
>>> result.encode()
b'\x1eSUN2000-8KTL-M0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
>>>

That is weird since it’s exactly what my integration does. What happens if you directly use the HuaweiSolar package in python? Install HuaweiSolar through pypi and run this:

import HuaweiSolar
inverter = HuaweiSolar('192.168.5.34')
inverter.get("model_name")

Are you running this on the same machine as HA runs?

Yes, I’m running it on the same machine as HA.
I can’t find HuaweiSolar package. Is it this: https://pypi.org/project/huawei-solar/

>>> import huawei_solar
>>> inverter = HuaweiSolar('192.168.8.1')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'HuaweiSolar' is not defined
>>> inverter = huawei_solar('192.168.8.1')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'module' object is not callable

Oops, this should work:

import huawei_solar
inverter = huawei_solar.HuaweiSolar('192.168.5.34')
inverter.get("model_name")
>>> inverter = huawei_solar.HuaweiSolar('192.168.8.1')
>>> inverter.get("model_name")
Result(value='SUN2000-8KTL-M0', unit=None)
>>> exit

It took about 5 second to get answer from “inverter.get(“model_name”)”. Still it doasn’t work with HA.
In configuration.yaml I have:

sensor: 
  - platform: huawei_solar   
    host: 192.168.8.1
2020-04-23 11:59:01 WARNING (MainThread) [homeassistant.components.sensor] Setup of sensor platform huawei_solar is taking over 10 seconds.
2020-04-23 11:59:06 ERROR (SyncWorker_1) [huawei_solar.huawei_solar] could not read register value, is an other device already connected?
2020-04-23 11:59:06 ERROR (MainThread) [homeassistant.components.sensor] Error while setting up huawei_solar platform for sensor
Traceback (most recent call last):
  File "C:\Python38\lib\site-packages\homeassistant\helpers\entity_platform.py", line 178, in _async_setup_platform
    await asyncio.wait_for(asyncio.shield(task), SLOW_SETUP_MAX_WAIT)
  File "C:\Python38\lib\asyncio\tasks.py", line 483, in wait_for
    return fut.result()
  File "C:\Python38\lib\concurrent\futures\thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "C:\Users\ligez\AppData\Roaming\.homeassistant\custom_components\huawei_solar\sensor.py", line 61, in setup_platform
    entities.append(HuaweiSolarSensor(inverter))
  File "C:\Users\ligez\AppData\Roaming\.homeassistant\custom_components\huawei_solar\sensor.py", line 80, in __init__
    self._nb_optimizers = self._inverter.get("nb_optimizers").value
  File "C:\Users\ligez\AppData\Roaming\.homeassistant\deps\Python38\site-packages\huawei_solar\huawei_solar.py", line 37, in get
    response = read_register(self.client, reg.register, reg.length)
  File "C:\Users\ligez\AppData\Roaming\.homeassistant\deps\Python38\site-packages\huawei_solar\huawei_solar.py", line 151, in read_register
    raise ReadException(message)
huawei_solar.huawei_solar.ReadException: could not read register value, is an other device already connected?

Hmm, strange. Can you ping the device from HA? (use the ping integration)