I recently shared a script that I’d written that uses the Octopus Energy API to retrieve my electricity usage (in kWh) for the previous 7 days, this was useful however I wanted to take it a step further and get it to output the actual cost per day too ๐ท.
The one challenge I have is that I’m on the Octopus Go tariff, which provides cheaper electricity between the hours of 0:30-04:30 each day (perfect for charging my EV overnight ๐). This means that it’s not quite as simple as multiplying usage in kWh by the price per kWh to calculate the daily cost as the rate varies depending on the time of the day – here are details of the tariff for additional context:

To add to this, the Octopus Energy app currently doesn’t support providing the daily cost for this tariff (which was the main reason for me writing this script):

I eventually figured out how to do this and include the Python script below (with comments).
Some points to note:
- You will need to add your API key, MPAN and serial number which can be obtained from – https://octopus.energy/dashboard/new/accounts/personal-details/api-access
- You will need to update the peak and off-peak rates (offpeakrate and peakrate variables) as these can vary based on your location.
- I haven’t included the daily standing charge in the calculation.
- You can increase the number of days to report on by changing the numberofdays variable
import requests
from requests.auth import HTTPBasicAuth
import datetime
# Set the peak and off-peak rates for Octopus Go
offpeakrate = 9.50
peakrate = 38.58
# The number of previous days to report on
numberofdays = 7
# Set the API key, meter MPAN and serial
key = "API Key"
MPAN = "MPAN"
serial = "Serial Number"
# Get today's date
today = datetime.date.today()
# Loop through the previous x number of days to report on (set by the "numberofdays" variable)
while numberofdays > 0:
peakusage = 0
offpeakusage = 0
fromdate = (today - datetime.timedelta(days=numberofdays)).isoformat() # Get the from date
todate = (today - datetime.timedelta(days=(numberofdays - 1))).isoformat() # Get the to date
# Call the Octopus API for the date range
baseurl = "https://api.octopus.energy/v1/"
url = baseurl + "electricity-meter-points/" + MPAN + "/meters/" + serial + "/consumption" + "?period_from=" + fromdate + "&period_to=" + todate
request = requests.get(url,auth=HTTPBasicAuth(key,""))
consumption = request.json()
numberofdays -= 1 # Minus 1 from the number of days variable (the loop will stop when this hits 0)
i = 0 # Used to index the results returned (48 results per day, one per 30 minutes)
for result in consumption["results"]: # Loop through the results returned for the specified day, extract the peak and off-peak units consumed and calculate the cost
if i in range(40,47): # These are the indexes of the off-peak hours (00:30-04:30)
offpeakusage = offpeakusage + result["consumption"]
else:
peakusage = peakusage + result["consumption"]
i += 1
# Calculate the peak / off-peak and total cost for the day in ยฃ's (rounded to 2 decimal places)
peakcost = round((peakusage * peakrate / 100), 2)
offpeakcost = round((offpeakusage * offpeakrate / 100), 2)
totalcost = round((peakcost + offpeakcost), 2)
# Print out the cost for the day
print("Usage for " + fromdate)
print("-Peak ยฃ" + (str(peakcost)))
print("-Offpeak ยฃ" + (str(offpeakcost)))
print("-Total cost for day ยฃ" + (str(totalcost)))
Here is an example of the output that the script provides:

The script can also be found on GitHub.
In the future I plan to update this to automagically retrieve the pean and off-peak rates as it should be possible to do this using the API – https://developer.octopus.energy/docs/api/#list-tariff-charges.

Leave a reply to Brendan Griffin Cancel reply