Skip to content

Instantly share code, notes, and snippets.

@PaulW
Created March 14, 2018 11:52
Show Gist options
  • Save PaulW/7f3ce29c3469757c26504b572597a45c to your computer and use it in GitHub Desktop.
Save PaulW/7f3ce29c3469757c26504b572597a45c to your computer and use it in GitHub Desktop.
Python script to authenticate and pull data from ovo energy to track smart meter usage. Uses ovo's RESTful API interface. At present, the script will error on the initial authentication if no valid token already exists, as takes a short while for ovo to catch up it seems, so I need to add in a catch for this.
#!/usr/bin/env python
# This requires Python 2.7. This will not work with Python 3
USERNAME = 'Your@email.address'
PASSWORD = 'Your ovo Password'
from requests import Request, Session
import json
import time
from time import gmtime, strftime
epoch_time = int(time.time()//60 * 60)
def main():
url = 'https://my.ovoenergy.com/api/auth/login'
postdata = {'username':USERNAME,'password':PASSWORD,'rememberMe':'true'}
headers = {
'content-type':'application/json;charset=UTF-8',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36'
}
s = Session()
response = s.post(url,data=json.dumps(postdata),headers=headers)
url = 'https://paym.ovoenergy.com/api/paym/accounts'
response = s.get(url,headers=headers)
fullData = json.loads(response.content)[0]
for device in fullData['consumers']:
print "Getting readings for %s meter with mpan %s:" % (device['utilityType'], device['mpan'])
print " Estimated Annual Usage is %s %s" % (device['meters'][0]['estimatedAnnualConsumption'], device['meters'][0]['unitOfMeasure'])
url = 'https://live.ovoenergy.com/api/live/meters/%s/consumptions/instant' % device['mpan']
response = s.get(url,headers=headers)
realtimeData = json.loads(response.content)
print " Current Demand : %s %s" % (realtimeData['consumption']['demand'], realtimeData['measurementUnit'])
print " Current Cost/Hour : %s" % realtimeData['consumption']['consumptionPrice']['amount']
print " Past 10 Minutes of Data:"
url = 'https://live.ovoenergy.com/api/live/meters/%s/instant-power?from=%s&to=%s' % (device['mpan'], time.strftime('%Y-%m-%dT%H:%M:%SZ', time.localtime(int(epoch_time - 600))), time.strftime('%Y-%m-%dT%H:%M:%SZ', time.localtime(epoch_time)))
response = s.get(url,headers=headers)
historyData = json.loads(response.content)
for historyItem in historyData['power']:
tdepoch = int(historyItem['startTime']) / 1000.0
if 'dataError' not in historyItem:
print " [%s] Demand %s %s\t(Cost/Hour %s)" % (time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(tdepoch)), historyItem['instantPower'], realtimeData['measurementUnit'], historyItem['price'])
else:
print " [%s] No Data Available" % time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(tdepoch))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment