Integration Solar inverter huawei 2000L

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)

Yes, I can ping (binary_sensor online). I started HA, still the same error in HA log. Then in second windows I tested it with

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

HA is clean with minimal configuration, only Ping and Huawei_solar integration

# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:
# Uncomment this if you are using SSL/TLS, running in Docker container, etc.
# http:
#   base_url: example.duckdns.org:8123
# Text to speech
tts:
  - platform: google_translate
group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
sensor: 
  - platform: huawei_solar   
    host: 192.168.8.1
binary_sensor:
  - platform: ping
    host: 192.168.8.1
    count: 2
    scan_interval: 60

What happens if you run inverter.get("nb_optimizers") ?

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.
>>> import huawei_solar
>>> inverter = huawei_solar.HuaweiSolar('192.168.8.1')
>>> inverter.get("nb_optimizers")
could not read register value, is an other device already connected?
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python38\lib\site-packages\huawei_solar\huawei_solar.py", line 37, in get
    response = read_register(self.client, reg.register, reg.length)
  File "C:\Python38\lib\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?
>>> inverter.get("model_name")
Result(value='SUN2000-8KTL-M0', unit=None)
>>>

It seems like we’re getting somewhere, I think your device does not support optimizers and instead of returning 0 it just fails.
Any of these that fail?

inverter.get("active_power").value
inverter.get("daily_yield_energy").value
inverter.get("accumulated_yield_energy").value
inverter.get("reactive_power").value
inverter.get("power_factor").value
inverter.get("efficiency").value
inverter.get("grid_voltage").value
inverter.get("grid_current").value
inverter.get("grid_frequency").value
inverter.get("startup_time").value.time()
inverter.get("shutdown_time").value.time()
inverter.get("internal_temperature").value
inverter.get("device_status").value
inverter.get("nb_online_optimizers").value
inverter.get("day_active_power_peak").value

import huawei_solar
inverter = huawei_solar.HuaweiSolar(‘192.168.8.1’)
inverter.get(“active_power”).value
1793
inverter.get(“daily_yield_energy”).value
0.04
inverter.get(“accumulated_yield_energy”).value
31.27
inverter.get(“reactive_power”).value
1
inverter.get(“power_factor”).value
1.0
inverter.get(“efficiency”).value
96.92
inverter.get(“grid_voltage”).value
424.9
inverter.get(“grid_current”).value
2.487
inverter.get(“grid_frequency”).value
49.99
inverter.get(“startup_time”).value.time()
datetime.time(14, 16, 8)
inverter.get(“shutdown_time”).value.time()
datetime.time(18, 11, 39)
inverter.get(“internal_temperature”).value
16.7
inverter.get(“device_status”).value
‘On-grid’
inverter.get(“nb_online_optimizers”).value
could not read register value, is an other device already connected?
Traceback (most recent call last):
File “”, line 1, in
File “C:\Python38\lib\site-packages\huawei_solar\huawei_solar.py”, line 37, in get
response = read_register(self.client, reg.register, reg.length)
File “C:\Python38\lib\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?
inverter.get(“day_active_power_peak”).value
0

I will edit the code so that it assumes 0/NA on a fail. Where do you live that the grid voltage is 425V? :thinking:

Once github is back working again i’ll be trying to use this script as well!!

I would be on that voltage as well as i have a 3phase inverter… Do you think you script will work on that… as i know there is 3 outputs for grid voltage, grid current etc…

It’s not grid voltage, but voltage between L1 and L2. It’s working now, after I commented lines with optimizers in sensor.py. Inverter have L1, L2 and L3 so I will need some modification. Now I know how to do it. Thanks for your help.

Ahhh you have the 3-phase model like me! and my reply above…
So yes we will both need this modifications!