Sungrow API

Using the Integration Pyscript Python scripting I’ve developed some code to call Sungrow’s API and access data from their inverters and other solar equipment.
You need to ask ISolarCloud (Sungrows online app for solar installations) for an App key. Once you receive it you can access all data from your plant through this code.
You can download it at https://github.com/jsanchezdelvillar/Sungrow-API

2 Likes

New version including SSL security, you can download it at the same link https://github.com/jsanchezdelvillar/Sungrow-API

1 Like

Hey mate

Thanks for sharing this. I am giving it a try. Everything is setup but unable to figure out where to see the data in HA.

Please help.

While running the automation:

  • id: ‘XXXXXXXXXXX’
    alias: Sungrow.py every 5 minutes
    description: ‘’
    triggers:
    • trigger: time_pattern
      minutes: /5
      conditions:
      actions:
    • action: pyscript.update_device_data_rsa
      data: {}
      mode: single

This error is coming up:

Can you help please?

Sorry, sorry, sorry!
First of all, there was a mistake in pyscript/config.yaml, the indentation was wrong. I have corrected it. It should be like this:

allow_all_imports: true
hass_is_global: true
apps:
  Sungrow_RSA:
    appkey: !secret sungrow_appkey
    sung_secret: !secret sungrow_secret
    username: !secret sungrow_user
    password: !secret sungrow_password
    RSA_public: !secret sungrow_rsa_public

If changing the file doesn’t work:

What you are showing is a very basic error, it just cannot find the main code. Either you don’t have the routine update_device_data_rsa or it cannot be called from the integration.

The routine is included in file __init__.py. Follows a screenshot of my folder structure.

This file includes the following at line 262:

@service
# Define payload and headers for login
async def update_device_data_RSA():

    # uses the stored token
    token = state.get("input_text.token")
    log.info (f"Token: {token}")

    # If token is not empty and not error
    if token and token != "Error":
        await get_device_data(token)
    else:
        log.warning("Unavailable token, asking for a new one")
        # Asking for a new token
        await get_token()

The ‘@service’ is what makes it public. In Developer Tools / Actions you should see an action that calls the routine. I’m enclosing an image, sorry it is in Spanish:

If you cannot see it, then the automation cannot execute it, and you would get the error you mention.

So, if changing pyscript/config.yaml doesn’t work, maybe you are missing some step in the installation. Make sure you have installed the Pyscript integration and have created and modified all needed files.

About where to see the data, once the code works the sensors will be automatically created. Their name starts with ‘pv’.

Thank you. Automation is fixed now.

I fixed the indentation earlier but named the init file as init.py instead of init.py.

Now, automation is not throwing any errors.

But still it is not working. I mean I am not getting any data loaded in to the new sensors created.

Any ideas where to look for the issue?

Inverter: SG5KTL-MT
Communication Module: WiFi V31

The code gets your token from input_text.token and prints it to the log. If there is a valid token it tries to get the device data, if not it prints to the log an error message and tries to get a new token. If you don’t have input_text.token, add it through a helper in HA and the integration will fill its value.

If you try to get a new token, it will print in the log the data for the request and the answer, both encrypted and decrypted. Depending on the answer it will fill input_text.token with the token or with an error message.

When it tries to read the device data it uses several values that you have to replace in the code:

  • point_id_list: You can get it from the API documentation in Sungrow Developer Portal, under Common Measuring Point Enumeration / Common plant measuring points. In my case I’m reading “83022”, “83033”, “83025”, “83001”, “83102”, “83072” and “83106”.
  • ps_key_list: the readme file explains how to obtain it, I decided not to code it because it needs several succesive queries to the API. In my case the ps_key_list is 5694969_11_0_0 because my ps_id is 5694969.

In async def get_device_data(token) make sure the ps_key_list is valid and remove the # symbol in the code from the lines that say #log.info. Then in the log you can check the request for data and the answer, coded and decoded.

I’m halfway to coding a new integration where you don’t need to input the ps_key or the points ids, but Sungrow API has been working for several months now without any problem, so I don’t really need it. The problem I’m having is that the new integration reads ALL the points, even those that in my case are not used (battery, three phase values…). Whenever I finish it I will be able to select the points I want to read, but for reading all points GoSungrow integration (GitHub - MickMake/GoSungrow: GoLang implementation to access the iSolarCloud API updated by SunGrow inverters.) works fine. I used it for several months before I got bored of looking to so many points, most of them empty, and decided to create my integration.

1 Like

Thank you for your reply.

Does this look good?

I can check tomorrow only if it’s working as I don’t have battery and currently solar is off.

I have also updated the ps_key_list and point_ID_list.

“# symbol removed from #log.info”.

When creating the text helper, the name should be ‘token’, instead of ‘input_text.token’. The ‘input_text’ part is added in the code to call that helper. Remember to reload all YAML configurations in Developer Tools so HA accepts the changes.

The first time you run the automation you should see the following log entries:

  • the value of input_text.token is “”
  • ‘asking for a new token’
  • the payload of the token request
  • the headers of the token request
  • the response before and after decryption.
  • If the response has an error, several messages.

Lets assume there is no error (or you already have a valid token). If you have removed the # from #log.info in get_device_data you should see:

  • unencrypted payload
  • encrypted payload (not really useful)
  • encrypted response (same, just gibberish)
  • decrypted response
  • if there is no error, device data, a list of all points with their values, extracted from the response

Then the code creates the sensors and puts inside the values from device data. It also creates some additional sensors by making calculations on the values.

If the system is connected you don’t have to wait until tomorrow, all energy and equivalent hours data, for instance, have values accumulated throughout the day and will not reset until midnight. You will just see a 0 for all production values (which would be correct).

Follows a screenshot of my HA with all values:

Getting this error:

Is there anywhere else to view automation logs?