-
-
Save cooldil/0b2c5ee22befbbfcdefd06c9cf2b7a98 to your computer and use it in GitHub Desktop.
snippet to write solaredge optimisers panel data in an influxdb database
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests, pickle | |
from datetime import datetime | |
import json, pytz | |
import pandas as pd | |
from influxdb import DataFrameClient | |
# Set "static" variables | |
login_url = "https://monitoring.solaredge.com/solaredge-apigw/api/login" | |
panels_url = "https://monitoring.solaredge.com/solaredge-web/p/playbackData" | |
DAILY_DATA = "4" | |
WEEKLY_DATA = "5" | |
# Set "customizable" variables. Update as appropriate | |
COOKIEFILE = 'solaredge.cookies' | |
TIMEPERIOD = DAILY_DATA | |
SOLAREDGE_USER = "" # web username | |
SOLAREDGE_PASS = "" # web password | |
SOLAREDGE_SITE_ID = "" # site id | |
INFLUXDB_IP = "" | |
INFLUXDB_PORT = 8086 | |
INFLUXDB_DATABASE = "" | |
INFLUXDB_SERIES = "optimizers" | |
INFLUXDB_RETENTION_POLICY = "autogen" | |
session = requests.session() | |
try: # Make sure the cookie file exists | |
with open(COOKIEFILE, 'rb') as f: | |
f.close() | |
except IOError: # Create the cookie file | |
session.post(login_url, headers = {"Content-Type": "application/x-www-form-urlencoded"}, data={"j_username": SOLAREDGE_USER, "j_password": SOLAREDGE_PASS}) | |
panels = session.post(panels_url, headers = {"Content-Type": "application/x-www-form-urlencoded", "X-CSRF-TOKEN": session.cookies["CSRF-TOKEN"]}, data={"fieldId": SOLAREDGE_SITE_ID, "timeUnit": TIMEPERIOD}) | |
try: | |
with open(COOKIEFILE, 'wb') as f: | |
pickle.dump(session.cookies, f) | |
f.close() | |
except IOError: | |
print('Unable to create cookie file (readonly filesystem?): ' + COOKIEFILE) | |
try: | |
with open(COOKIEFILE, 'rb') as f: | |
session.cookies.update(pickle.load(f)) | |
except IOError: | |
print('Unable to open cookie file: ' + COOKIEFILE) | |
print('Will continue running with new login...') | |
session.post(login_url, headers = {"Content-Type": "application/x-www-form-urlencoded"}, data={"j_username": SOLAREDGE_USER, "j_password": SOLAREDGE_PASS}) | |
# Get the cookie expiration | |
for cookie in session.cookies: | |
if cookie.name == 'SolarEdge_SSO-1.4': | |
cookie_expiration = cookie.expires | |
panels = session.post(panels_url, headers = {"Content-Type": "application/x-www-form-urlencoded", "X-CSRF-TOKEN": session.cookies["CSRF-TOKEN"]}, data={"fieldId": SOLAREDGE_SITE_ID, "timeUnit": TIMEPERIOD}) | |
if (panels.status_code != 200) or (datetime.now() > datetime.fromtimestamp(cookie_expiration)): # Update cookie if expired | |
session.post(login_url, headers = {"Content-Type": "application/x-www-form-urlencoded"}, data={"j_username": SOLAREDGE_USER, "j_password": SOLAREDGE_PASS}) | |
panels = session.post(panels_url, headers = {"Content-Type": "application/x-www-form-urlencoded", "X-CSRF-TOKEN": session.cookies["CSRF-TOKEN"]}, data={"fieldId": SOLAREDGE_SITE_ID, "timeUnit": TIMEPERIOD}) | |
if (panels.status_code != 200): | |
exit() # Terminate if unable to get panel data | |
f.close() | |
try: | |
with open(COOKIEFILE, 'wb') as f: | |
pickle.dump(session.cookies, f) | |
f.close() | |
except IOError: | |
print('Unable to update expired cookie file [' + str(datetime.fromtimestamp(cookie_expiration)) + '] (readonly filesystem?): ' + COOKIEFILE) | |
response = panels.content.decode("utf-8").replace('\'', '"').replace('Array', '').replace('key', '"key"').replace('value', '"value"') | |
response = response.replace('timeUnit', '"timeUnit"').replace('fieldData', '"fieldData"').replace('reportersData', '"reportersData"') | |
response = json.loads(response) | |
data = {} | |
for date_str in response["reportersData"].keys(): | |
date = datetime.strptime(date_str, '%a %b %d %H:%M:%S GMT %Y') | |
date = pytz.timezone('America/New_York').localize(date).astimezone(pytz.utc) | |
for sid in response["reportersData"][date_str].keys(): | |
for entry in response["reportersData"][date_str][sid]: | |
if entry["key"] not in data.keys(): | |
data[entry["key"]] = {} | |
data[entry["key"]][date] = float(entry["value"].replace(",", "")) | |
df = pd.DataFrame(data) | |
conn = DataFrameClient(INFLUXDB_IP, INFLUXDB_PORT, "", "", INFLUXDB_DATABASE) | |
conn.write_points(df, INFLUXDB_SERIES, retention_policy=INFLUXDB_RETENTION_POLICY) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello @cooldil, thanks for sharing your snippet and insights - very helpful and useful! I just added some code to automatically extract the logical IDs (in my case 1.1.1. – 1.1.29.) and serial numbers of optimizers from SE. Hope that might helpful for somebody else.
Here is the code: