Calculating the daily cost of my electricity with the Octopus Energy API using Python ๐Ÿ™

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.

Comments

5 responses to “Calculating the daily cost of my electricity with the Octopus Energy API using Python ๐Ÿ™”

  1. Sam Avatar
    Sam

    Hi,

    This is very helpful, how did you map indexs? I am on Intelligent Octopus go, and my off peak times are 23:30 to 5:30, so what indexs should I choose, bear in mind, I am not a programmer and using google for checking it all

    Thanks.

    S

    Like

    1. Brendan Griffin Avatar

      Sorry for the slow reply, for some reason your comment was marked as spam. As your off-peak times span more than one day it makes things a little more complex as you’d have to pull back two separate days and then query the appropriate indexes in each to grab the times. If this is still something that you need, I’d be happy to put something together for you when I get chance?

      Like

  2. Chris Avatar
    Chris

    id be interested in this for intelligent tariff please as per previous comment

    Like

  3. Andrew Hayman Avatar
    Andrew Hayman

    Brendan, did you ever go further with this to get the Intelligent Octopus Go data (with cheap rate going across midnight) ?

    I just had a chat with Octopus – you’d think they could help with this… but no. They make a big deal out of the half-hourly raw data, but that’s just a fraction of the story.

    In fact, they don’t even seem to be able to give a complete annual synopsis of electricty and gas usage and costs – it seems none are provided routinely to their customers.

    Thanks

    Andrew

    Like

    1. Brendan Griffin Avatar

      I didn’t – it’s something that I’d like to look into though, just don’t have the time at the moment.

      Brendan

      Like

Leave a comment