Guest User

Untitled

a guest
Apr 6th, 2023
566
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.63 KB | None | 0 0
  1. #
  2. # Extract data from cgi-day call to myenergi server
  3. # Chris Horne 04/12/2022
  4. # Modified by Brad McGill 7/4/2023 to collect hourly data and export a slightly differnt format
  5. #                                  Also added Timezone adjustment code
  6. #                                  I did not complete the Eddi section as I do not have an Eddi to test with, removed section
  7. #                                  New requirement for pytz package, run "pip install pytz" to install this.
  8.  
  9. import requests
  10. import json
  11. import datetime
  12. from requests.auth import HTTPDigestAuth
  13. import pytz
  14.  
  15. # Setup
  16. server="s18"
  17.  
  18. username = "xxxxxxxx"                   #Hub/gateway device serial number
  19. password = "xxxxxxxxxxxxxxxxxxxxxxxx"   #api key generated from devices tab in myenergi, see https://support.myenergi.com/hc/en-gb/articles/5069627351185
  20. ID="Zxxxxxxxx"                          #Z for Zappi, E for Eddi, plus your serial number e.g. "Z11100022"
  21.                                        
  22. year=2023                               #These must be provided as UTC values
  23. month=3                                 #These must be provided as UTC values
  24. day=1                                   #These must be provided as UTC values
  25. hour=0                                  #These must be provided as UTC values  
  26.  
  27. num_hours=745                           # this cannot be more than 745 which is roughly 31 days of data
  28. timezone = 'Australia/Sydney'           #Full list of timezones here : https://gist.github.com/heyalexej/8bf688fd67d7199be4a1682b3eec7568
  29.  
  30. # Echo Setup
  31. print("Username/Hub Serial Number: " + username)
  32. print("Password:" + str(len(password)) + " chars")
  33. print("Device serial number: " + ID)
  34. print("Start Date:" + str(day) + "/" + str(month) + "/" + str(year) + " from hour " + str(hour))
  35. print("Collecting " + str(num_hours) + " hours")
  36. print("Timezone:" + timezone)
  37.  
  38. filename="Energy_Data_"+ID+"_"+str(year)+"-"+str(month).zfill(2)+".csv"
  39. print("Saving to: " + filename)
  40.  
  41. fo = open(filename,"w")
  42. fo.write("Date Time,Import (kWh),Export (kWh), Generation (kWh), Zappi Energy (kWh), Self Consumption (kWh), Total Property Usage (kWh), Green Percentage\n")
  43.  
  44. url = 'https://' + server + '.myenergi.net/cgi-jdayhour-' + ID + '-' + str(year) + '-' + str(month) + '-' + str(day) + '-' + str(hour) + '-' + str(num_hours)
  45.  
  46. print(url)
  47. headers = {'Content-Type': 'application/json', 'Accept': 'application/json'}
  48. r = requests.get(url, auth=HTTPDigestAuth(username,password), headers=headers, timeout=20)
  49. daily_import=0;
  50. daily_export=0;
  51. daily_generation=0;
  52. daily_EV=0;
  53.  
  54. if r.status_code == 200:
  55.     if ID[0] == 'Z':
  56.         print ('*** Success - Zappi ***')
  57.         data = json.loads(r.content)
  58.         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?
  59.         for i in range(num_hours):
  60.             hr=int(data[rec][i].get('hr') or 0)
  61.             yr=int(data[rec][i].get('yr') or 0)
  62.             mon=int(data[rec][i].get('mon') or 0)
  63.             dom=int(data[rec][i].get('dom') or 0)
  64.             y_import = float(data[rec][i].get('imp') or 0)/(60*1000)
  65.             y_gep = float(data[rec][i].get('gep') or 0)/(60*1000)
  66.             y_exp = float(data[rec][i].get('exp') or 0)/(60*1000)
  67.             y_z1 = float(data[rec][i].get('h1d') or 0)/(60*1000)
  68.             y_z2 = float(data[rec][i].get('h2d') or 0)/(60*1000)
  69.             y_z3 = float(data[rec][i].get('h3d') or 0)/(60*1000)
  70.             y_z1b = float(data[rec][i].get('h1b') or 0)/(60*1000)
  71.             y_z2b = float(data[rec][i].get('h2b') or 0)/(60*1000)
  72.             y_z3b = float(data[rec][i].get('h3b') or 0)/(60*1000)
  73.             y_zappi = y_z1 + y_z2 + y_z3 + y_z1b + y_z2b + y_z3b
  74.                
  75.             daily_generation=y_gep/60
  76.             daily_import=y_import/60
  77.             daily_export=y_exp/60
  78.             daily_EV=y_zappi/60
  79.            
  80.             daily_self_consumption = daily_generation-daily_export
  81.             daily_property_usage=daily_import+daily_self_consumption
  82.             daily_green_percentage = (daily_self_consumption / daily_property_usage)*100
  83.            
  84.             #Convert from UTC
  85.             dt = datetime.datetime(yr,mon,dom,hr)
  86.             dtutc = dt.replace(tzinfo=pytz.utc)
  87.             localdt = dtutc.astimezone(pytz.timezone(timezone))
  88.            
  89.             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}')
  90.             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')
  91.     if ID[0] == 'E':
  92.         print ('*** Sorry, not implemented yet ***')
  93.        
  94. else:
  95.     print ("Failed to read ticket, errors are displayed below,")
  96.     response = json.loads(r.content)
  97.     print(response["errors"])
  98.  
  99.     print("x-request-id : " + r.headers['x-request-id'])
  100.     print("Status Code : " + r.status_code)
  101.    
  102. fo.close()
  103.            
  104. #fo.close()
Advertisement
Add Comment
Please, Sign In to add comment