Exporting data from Enphase APIs

I bought an Enphase solar powered system in early 2017, one of the major appeals of the Enphase brand was that is has developer APIs, so I could track my systems power generation and even household usage.

My aim is to get this data out of the Enphase APIs then try to make sense of it, possibly bringing in weather data and water usage from IoT systems on my ever increasing to-do list.

There doesn’t seem to be a way of bulk exporting data, and with rate limiting on the free tier I figured I could write a script that hits the stats API for each day, grabs a whole days data then persist it, wait a period of time, then hit the next day. By delaying, I don’t break the rate limit, I can just kick the script off and have a coffee!


import pendulum
import time
import requests
import json
import os

userId = os.environ['ENHPASE_USER_ID']
key = os.environ['ENPHASE_KEY']
systemId = os.environ['ENPHASE_SYSTEM_ID']
# set tz code from http://www.localeplanet.com/java/en-AU/index.html
tzCode = os.environ['TIME_ZONE']
# free tier only allows so many requests per minute, space them out with delays
sleepBetweenRequests = int(os.environ['SLEEP_BETWEEN_REQUESTS'])
# Start/end dates to export
startDate = pendulum.parse(os.environ["START_DATE"], tzinfo=tzCode)
endDate = pendulum.parse(os.environ["END_DATE"], tzinfo=tzCode)
# Shouldn't need to modify this
url = 'https://api.enphaseenergy.com/api/v2/systems/%s/stats' % systemId

print('Starting report between %s and %s' % (startDate.to_date_string(), endDate.to_date_string()))

period = pendulum.period(startDate, endDate)

for dt in period.range('days'):
time.sleep(sleepBetweenRequests)

print('date [%s] START [%s] END [%s]' % (dt, dt.start_of('day'), dt.end_of('day')))
# HTTP Params
params = {'user_id': userId,
'key': key,
'datetime_format': 'iso8601',
'start_at': dt.start_of('day').int_timestamp,
'end_at': dt.end_of('day').int_timestamp}

r = requests.get(url=url, params=params)

if r.status_code == 200:
filename = "out/%s.json" % dt.to_date_string()
os.makedirs(os.path.dirname(filename), exist_ok=True)
with open(filename, 'w') as outfile:
json.dump(r.json(), outfile, indent=2)
print('Success %s' % dt.to_date_string())
else:
print('Failed to get data for %s' % dt.to_date_string())

Run that using python stats.py and it’ll dump out a json file in /out/ per day. I’ve only got the stats API setup so far, but feel free to pull request in the others!

Also keen an eye on my github project, I’ll be adding more to it, the plan is to get it running in AWS with scheduled lambdas to pull in data on an hourly basis, mash in data from weather APIs, and any IoT systems I build.

Leave a Reply

Your email address will not be published. Required fields are marked *