Last active
August 23, 2021 16:58
-
-
Save wizardofzos/aee91a013f9a5375fdba980d02186a9a to your computer and use it in GitHub Desktop.
SolarEdge API calls to Splunk
This file contains 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
# SolarEdge API | |
# Docs: https://www.solaredge.com/sites/default/files/se_monitoring_api.pdf | |
# Just add running this file to a crontab and add "dasplunkfile" to splunk forwarder. | |
# It will take all data for today, minus the last entry (as that one fluctuates until end of perdiod) | |
# and prep a json file for splunk. | |
import requests | |
import datetime | |
import pprint | |
today = datetime.datetime.now().strftime("%Y-%m-%d") | |
# Just get today..... | |
first = today + " 00:00:00" | |
last = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") | |
# This is the file for splunk (splunk add monitor <this> | |
dasplunkfile = '/home/pi/solaredge/splunkdata/solaredge-data.json' | |
print(f"Getting data from {first} to {last}") | |
API_KEY = "<<YOUR API KEY HERE>>" | |
siteid = "<<YOUR SITEID HERE>>" | |
detailsurl = f"https://monitoringapi.solaredge.com/site/{siteid}/details?api_key={API_KEY}" | |
baseurl = f"https://monitoringapi.solaredge.com/site/{siteid}/" | |
powerurl = baseurl + f"powerDetails?api_key={API_KEY}&startTime={first}&endTime={last}&timeUnit=QUARTER_OF_AN_HOUR" | |
energyurl = baseurl + f"energyDetails?api_key={API_KEY}&startTime={first}&endTime={last}&timeUnit=QUARTER_OF_AN_HOUR" | |
datareq = requests.get(url=powerurl) | |
jsonpowerdata = datareq.json() | |
datareq = requests.get(url=energyurl) | |
jsonenergydata = datareq.json() | |
myJson = {} | |
enersum = 0 | |
for val in jsonenergydata['energyDetails']['meters'][0]['values']: | |
try: | |
enersum += val['value'] | |
except: | |
# no value for this interval, lets just pretent nothing happened | |
continue | |
myJson[val['date']] = {} | |
myJson[val['date']]['energy'] = val['value'] | |
powsum1 = 0 | |
powsum2 = 0 | |
for val in jsonpowerdata['powerDetails']['meters'][0]['values']: | |
try: | |
powsum1 += val['value'] | |
powsum2 += (val['value'] * 0.25) | |
except: | |
# no value for this interval, lets just pretent nothing happened | |
continue | |
myJson[val['date']]['power'] = val['value'] | |
# Energy From Power is not true. the power value is max measured in (15min) interval... | |
myJson[val['date']]['energy_from_power'] = val['value'] * 0.25 | |
splunkjson= [] | |
print(f'Produced: {enersum/1000} kWh') | |
print(f'PowerSum = {powsum2/1000} kWh') | |
print(f'') | |
print(f'Diff energy and power : {(powsum2-enersum)/24} W') | |
for dt in myJson: | |
jdata = f'"datetime": "{dt}", "energy": {myJson[dt]["energy"]}, "power": {myJson[dt]["power"]}, "energy_from_power": {myJson[dt]["energy_from_power"]}' | |
splunkjson.append("{" + jdata + "}\n") | |
# Take all but last entry and add to file (last entry adds up during interval) | |
print('\n'.join(splunkjson)) | |
# Write all (valid, so not the last one) records to /tmp | |
with open(f"/tmp/solaredge.json", 'w+') as jsonuit: | |
jsonuit.writelines(splunkjson[:-1]) | |
# check and see if out splunk monitored file is there of not create empty | |
import os | |
if not os.path.isfile(dasplunkfile'): | |
os.system(f'touch {dasplunkfile}') | |
# now simply take all lines from the /tmp that are not in splunkfile and append at end.. | |
with open(dasplunkfile) as splunkfile: | |
splunk = splunkfile.readlines() | |
with open(dasplunkfile, 'a+') as splunkfile: | |
for line in splunkjson[:-1]: | |
if not line in splunk: | |
splunkfile.write(line) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment