SolarEdge API question

Discussions about Z-Way software and Z-Wave technology in general
Post Reply
AlesKO
Posts: 85
Joined: 24 Nov 2016 09:58

SolarEdge API question

Post by AlesKO »

I have SolarEdge inverter for my solar electricity production. SolarEdge's APIs are well documented, but I do not know how to use/get current production power (</currentPower>) into z-way.

If current production is higher than 9.000W > turn boiler heater ON.
Newer worked with APIs...


URL example:
https://monitoringapi.solaredge.com/ site/{siteId}/overview?api_key=L4QLVQ1LOKCQX2193VSEICXW61NP6B1O

Return content:
<overview>
<lastUpdateTime>2022-03-29 12:51:37</lastUpdateTime>
<lifeTimeData>
<energy>8208528.0</energy>
</lifeTimeData>
<lastYearData>
<energy>4583351.0</energy>
</lastYearData>
<lastMonthData>
<energy>2088703.0</energy>
</lastMonthData>
<lastDayData>
<energy>29508.0</energy>
</lastDayData>
<currentPower>
<power>11047.0</power>
</currentPower>
<measuredBy>INVERTER</measuredBy>
</overview>
seattleneil
Posts: 174
Joined: 02 Mar 2020 22:41

Re: SolarEdge API question

Post by seattleneil »

There are several approaches. One approach is to use the "HTTP Device" app and have the app poll the SolarEdge API every 5 minutes. Just add the app and with luck, you'll have a new Z-Way device that shows the power value. Something like the following:
HTTP Device.JPG
HTTP Device.JPG (115.66 KiB) Viewed 17671 times
Once you have the app properly reporting the power production value (you will have to tweak the URL and you may have to tweak the Javascript parsing), the final part of this approach is to create an automation Rule (i.e., an If-Then-Else statement) to turn your water boiler on when the value of the HTTP Device is > 9.000 (check the option "Don't send On command, if device is already turned On, similarly for Off) and select Activate reverse event under the Else tab.
AlesKO
Posts: 85
Joined: 24 Nov 2016 09:58

Re: SolarEdge API question

Post by AlesKO »

Thanks. Did not know it is so simple approach. Tryed 'HTTP Device' with exchange rate example and it works just fine.

But looks like SolarEdge's .xml has different (?) structure and is not returning value. It says: "This XML file does not appear to have any style information associated with it. The document tree is shown below."

Tryed two different Javascript approaches. First is your suggested code, the other one is: "parseFloat($$.findOne('/overview/currentPower/power/text()'))".

Neither of them returns a value.
How can I attach generated .xml or can I send you PM with real URL to get result?
seattleneil
Posts: 174
Joined: 02 Mar 2020 22:41

Re: SolarEdge API question

Post by seattleneil »

You're closer to success than you realize. My guess is that you only need a couple of suggestions and you'll be able to solve the problem yourself. Since this forum serves to help other Z-Way users, please consider the following semi-generic suggestions.

1. The z-way log file is very helpful for troubleshooting the https request and parsing the response. By default, the log file is /var/log/z-way-server.log.

2. Instead of making any assumptions about what gets returned from the https request, change the Inline Javascript to the following:
debugPrint("HTTPS returned:",JSON.stringify($$)); parseFloat('1234.5')

3. Ideally, the log file will contain the string "HTTPS returned" followed by the text that needs to be parsed. The log file should also reveal whether the https request was successful. The Z-Way UI should display 1234.5 for the device's value.

4. If I had to guess, the Javascript response probably needs to look something like the following:
var https_resp = JSON.stringify($$); parseFloat(https_resp.substr(https_resp.indexOf('<power'>)+7))
By adding debugPrint() statements into the Javascript, I expect you'll meet with success. Note that instead of waiting 300 seconds for the next poll interval, you can manually refresh the device's status by clicking on the timestamp on the UI.

If you're stuck, just send me a PM by clicking on "seattleneil" where there's an option to send a PM.
AlesKO
Posts: 85
Joined: 24 Nov 2016 09:58

Re: SolarEdge API question

Post by AlesKO »

I have been very busy lately. Only yesterday I had time to dig into the problem & your suggestions.
Can't believe how easy it is if you have forum experts willing to help.

So, what I did:
Included debug line into code: debugPrint("HTTPS returns:",JSON.stringify($$))
In log file I could see full string returned from solaredge API. Great thing! <power> is returned as "power".

With few 'try and error' attempts I finally wrote this code:
debugPrint("HTTPS returns:",JSON.stringify($$)); var https_resp = JSON.stringify($$); parseFloat(https_resp.substr(https_resp.indexOf("power")+7))

I also wrote 'W' sign into the 'Sensor scale' field. I know it is not scale but it works and Watt sign follows number shown on my Dashboard.

I am so happy.
Seattleneil, thank you very much!
seattleneil
Posts: 174
Joined: 02 Mar 2020 22:41

Re: SolarEdge API question

Post by seattleneil »

The primary purpose of this reply is to describe another advanced feature of Z-Way.

But first, congrats on figuring out the JavaScript incantation for parsing the SolarEdge API response. The HTTP Device app is powerful, but there's definitely a learning curve and the Z-Way documentation assumes the reader is familiar with JavaScript programming. IMHO, the Z-Way team should consider improving the Z-Way documentation by describing the steps necessary to create and test an HTTP Device app.

I use the HTTP Device app for reporting data from ESP8266-based temperature and humidity sensors, controlling Wi-Fi plug modules running Tasmota and making HTTP calls to a local python http server to execute CGI scripts (i.e., I run "python3 -m http.server --bind localhost --cgi 8000" and use the HTTP device app to invoke a python CGI script with something like http: //127.0.0.1:8000/cgi-bin/alarm_control.py?command=off).

The Easy Scripting app is another advanced feature of Z-Way. For a realistic example, I'll build on your initial question about controlling your boiler. Now that you have a Z-Way virtual device that reports your solar panel's power production, I assume you've created two If-Then automation rules - one to turn your boiler on when the power level exceeds 9000 watts and a second rule to turn your boiler off when the power level falls below a level like 8000 watts. Perhaps you're controlling the boiler using a single rule with an else statement, but then your boiler may toggle on and off rapidly when the power production fluctuates around 9000 watts (i.e., you can build in some hysteresis by using two rules with two different power levels). Using the Easy Scripting app, the If-Then logic can be more intelligent. Using the Easy Scripting app, you could create a custom If-Then-Else rule based on power production with time-delay hysteresis so that the the boiler turns on and off no more than 2 times per hour. The Easy App logic would look something like this:

Code: Select all

### YOUR_VIRTUAL_DEVICE (e.g., HTTP_Device_sensorMultilevel_9)

var deltaTime = new Date().getTime() / 1000 - zway.devices[8].instances[0].SwitchBinary.data.updateTime;
var powerWatts = controller.devices.get('HTTP_Device_sensorMultilevel_9').get('metrics:level');
debugPrint("Boiler Control: deltaTime =",deltaTime,"powerWatts =",powerWatts);
if (deltaTime > 1800) {
    if (powerWatts < 8000 && vdev("ZWayVDev_zway_8-0-37").value() === "on") {
        vdev("ZWayVDev_zway_8-0-37").off();
    } else if (powerWatts > 9000 && vdev("ZWayVDev_zway_8-0-37").value() === "off") {
        vdev("ZWayVDev_zway_8-0-37").on();
    }
}
Kepex
Posts: 3
Joined: 30 May 2022 11:58

Re: SolarEdge API question

Post by Kepex »

You can make HTTP requests to the SolarEdge API using a programming language of your choice. For example, you can use Python with the requests library. Here's a simple example:
import requests

# Replace 'YOUR_API_KEY' and 'YOUR_SITE_ID' with your actual API key and site ID
api_key = 'YOUR_API_KEY'
site_id = 'YOUR_SITE_ID'
api_url = f'https://monitoringapi.solaredge.com/sit ... y={api_key}'

response = requests.get(api_url)
data = response.json()

current_power = float(data['overview']['currentPower']['power'])

print(f'Current Power: {current_power} W')


Z-Way provides scripting capabilities that allow you to run scripts based on certain events, lead enrichment, or conditions. You can create a script that periodically queries the SolarEdge API and checks if the current power is higher than 9000 W. If it is, the script can trigger an action, such as turning on the boiler heater.

Here's a simplified example in pseudo-code:
// Replace 'YOUR_API_KEY' and 'YOUR_SITE_ID' with your actual API key and site ID
var apiKey = 'YOUR_API_KEY';
var siteId = 'YOUR_SITE_ID';
var apiUrl = 'https://monitoringapi.solaredge.com/site/' + siteId + '/overview?api_key=' + apiKey;

// Function to get current power from SolarEdge API
function getCurrentPower() {
// Make HTTP request to SolarEdge API and parse the response
// ...

// Return the current power
return currentPower;
}

// Check current power and turn on boiler heater if it's higher than 9000 W
var currentPower = getCurrentPower();
if (currentPower > 9000) {
// Code to turn on boiler heater in Z-Way
// ...
}
Post Reply