Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #
- # Extract data from cgi-day call to myenergi server
- # Chris Horne 04/12/2022
- # Modified by Brad McGill 7/4/2023 to collect hourly data and export a slightly differnt format
- # Also added Timezone adjustment code
- # I did not complete the Eddi section as I do not have an Eddi to test with, removed section
- # New requirement for pytz package, run "pip install pytz" to install this.
- import requests
- import json
- import datetime
- from requests.auth import HTTPDigestAuth
- import pytz
- # Setup
- server="s18"
- username = "xxxxxxxx" #Hub/gateway device serial number
- password = "xxxxxxxxxxxxxxxxxxxxxxxx" #api key generated from devices tab in myenergi, see https://support.myenergi.com/hc/en-gb/articles/5069627351185
- ID="Zxxxxxxxx" #Z for Zappi, E for Eddi, plus your serial number e.g. "Z11100022"
- year=2023 #These must be provided as UTC values
- month=3 #These must be provided as UTC values
- day=1 #These must be provided as UTC values
- hour=0 #These must be provided as UTC values
- num_hours=745 # this cannot be more than 745 which is roughly 31 days of data
- timezone = 'Australia/Sydney' #Full list of timezones here : https://gist.github.com/heyalexej/8bf688fd67d7199be4a1682b3eec7568
- # Echo Setup
- print("Username/Hub Serial Number: " + username)
- print("Password:" + str(len(password)) + " chars")
- print("Device serial number: " + ID)
- print("Start Date:" + str(day) + "/" + str(month) + "/" + str(year) + " from hour " + str(hour))
- print("Collecting " + str(num_hours) + " hours")
- print("Timezone:" + timezone)
- filename="Energy_Data_"+ID+"_"+str(year)+"-"+str(month).zfill(2)+".csv"
- print("Saving to: " + filename)
- fo = open(filename,"w")
- fo.write("Date Time,Import (kWh),Export (kWh), Generation (kWh), Zappi Energy (kWh), Self Consumption (kWh), Total Property Usage (kWh), Green Percentage\n")
- url = 'https://' + server + '.myenergi.net/cgi-jdayhour-' + ID + '-' + str(year) + '-' + str(month) + '-' + str(day) + '-' + str(hour) + '-' + str(num_hours)
- print(url)
- headers = {'Content-Type': 'application/json', 'Accept': 'application/json'}
- r = requests.get(url, auth=HTTPDigestAuth(username,password), headers=headers, timeout=20)
- daily_import=0;
- daily_export=0;
- daily_generation=0;
- daily_EV=0;
- if r.status_code == 200:
- if ID[0] == 'Z':
- print ('*** Success - Zappi ***')
- data = json.loads(r.content)
- rec='U' + ID[1:] #No idea why my response is with a U and not a Z, this may be the case for everyone, or may need altering?
- for i in range(num_hours):
- hr=int(data[rec][i].get('hr') or 0)
- yr=int(data[rec][i].get('yr') or 0)
- mon=int(data[rec][i].get('mon') or 0)
- dom=int(data[rec][i].get('dom') or 0)
- y_import = float(data[rec][i].get('imp') or 0)/(60*1000)
- y_gep = float(data[rec][i].get('gep') or 0)/(60*1000)
- y_exp = float(data[rec][i].get('exp') or 0)/(60*1000)
- y_z1 = float(data[rec][i].get('h1d') or 0)/(60*1000)
- y_z2 = float(data[rec][i].get('h2d') or 0)/(60*1000)
- y_z3 = float(data[rec][i].get('h3d') or 0)/(60*1000)
- y_z1b = float(data[rec][i].get('h1b') or 0)/(60*1000)
- y_z2b = float(data[rec][i].get('h2b') or 0)/(60*1000)
- y_z3b = float(data[rec][i].get('h3b') or 0)/(60*1000)
- y_zappi = y_z1 + y_z2 + y_z3 + y_z1b + y_z2b + y_z3b
- daily_generation=y_gep/60
- daily_import=y_import/60
- daily_export=y_exp/60
- daily_EV=y_zappi/60
- daily_self_consumption = daily_generation-daily_export
- daily_property_usage=daily_import+daily_self_consumption
- daily_green_percentage = (daily_self_consumption / daily_property_usage)*100
- #Convert from UTC
- dt = datetime.datetime(yr,mon,dom,hr)
- dtutc = dt.replace(tzinfo=pytz.utc)
- localdt = dtutc.astimezone(pytz.timezone(timezone))
- print(f'{localdt.day}/{localdt.month}/{localdt.year} {localdt.hour}:00,{daily_import:.2f},{daily_export:.2f},{daily_generation:.2f},{daily_EV:.2f},{daily_self_consumption:.2f},{daily_property_usage:.2f},{daily_green_percentage:.1f}')
- fo.write(f'{localdt.day}/{localdt.month}/{localdt.year} {localdt.hour}:00,{daily_import:.2f},{daily_export:.2f},{daily_generation:.2f},{daily_EV:.2f},{daily_self_consumption:.2f},{daily_property_usage:.2f},{daily_green_percentage:.1f}\n')
- if ID[0] == 'E':
- print ('*** Sorry, not implemented yet ***')
- else:
- print ("Failed to read ticket, errors are displayed below,")
- response = json.loads(r.content)
- print(response["errors"])
- print("x-request-id : " + r.headers['x-request-id'])
- print("Status Code : " + r.status_code)
- fo.close()
- #fo.close()
Advertisement
Add Comment
Please, Sign In to add comment