Skip to content

Instantly share code, notes, and snippets.

@sammachin
Created July 27, 2020 10:01
Show Gist options
  • Save sammachin/8a0a17e0d6b80c155901566104010d26 to your computer and use it in GitHub Desktop.
Save sammachin/8a0a17e0d6b80c155901566104010d26 to your computer and use it in GitHub Desktop.
Octopus Rating
import pickle
import requests
from datetime import datetime, timedelta, time
import csv
# Octopus Fixed Tarrif Rates (check for your region)
go_day = 14.5
go_night = 5
e7_day = 18.04
e7_night = 9.38
flat = 15.76
def get_prices():
#Builds a lookup table of agile pricing data for 365 days from date specified in start (1st June 2019), check URL for your region
url = "https://api.octopus.energy/v1/products/AGILE-18-02-21/electricity-tariffs/E-1R-AGILE-18-02-21-C/standard-unit-rates/"
lookup = {}
start = datetime.strptime("2019-06-01", "%Y-%m-%d")
for d in range(0,365):
date = (start + timedelta(days=d)).strftime("%Y-%m-%d")
payload = {'period_from': date+'T00:00Z', 'period_to': date+'T23:59Z'}
r = requests.get(url, params=payload)
data = r.json()
for x in data['results']:
lookup[x['valid_to']] = x['value_inc_vat']
pickle.dump(lookup, open("lookup.pkl", "wb"))
return lookup
def get_usage(month, year):
#Returns a Dict of 30min values for the month, where end of period timestamp is the key and number of wH is val (1000wh = 1kw) for example {'2020-02-28T22:30:00Z': 1.923}
url = 'http://192.168.1.41/csv_input4.csv'
payload = {'month' : month, 'year' : year}
resp = requests.get(url, params=payload)
data = csv.DictReader(resp.content.decode('utf-8').splitlines())
readings = {}
for r in data:
d = "{}-{}-{}".format(r['Year'], r['Month'].zfill(2), r['Day'].zfill(2))
del(r['Year'])
del(r['Month'])
del(r['Day'])
del(r['Total'])
del(r['Input'])
for i in r.items():
readings[d+'T'+i[0]+':00Z'] = int(i[1])/1000
return readings
def e7(t):
# Gets the rate for a timeperiod on Economy7 set to 00:30 > 07:30
ts = datetime.strptime(t, "%Y-%m-%dT%H:%M:%SZ")
if ts.time() >= time(0,31) and ts.time() <= time(7,30):
return e7_night
else:
return e7_day
def go(t):
# Gets the rate for a timeperiod on Octopus Go set to 00:30 > 04:30
ts = datetime.strptime(t, "%Y-%m-%dT%H:%M:%SZ")
if ts.time() >= time(0,31) and ts.time() <= time(4,30):
return go_night
else:
return go_day
def rate(readings, tarrif):
total = 0
for i in readings.items():
if tarrif == 'flat':
price = i[1] * flat
elif tarrif == 'agile':
price = i[1] * lookup[i[0]]
elif tarrif == 'go':
price = i[1] * go(i[0])
elif tarrif == 'e7':
price = i[1] * e7(i[0])
total += price
return int(total)/100
# Usage: rate(get_prices('[MONTH], [YEAR]'), '[TARIFF]')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment