Octopus Energy API Tariff History #60860
Replies: 1 comment
-
🕒 Discussion Activity Reminder 🕒 This Discussion has been labeled as dormant by an automated system for having no activity in the last 60 days. Please consider one the following actions: 1️⃣ Close as Out of Date: If the topic is no longer relevant, close the Discussion as 2️⃣ Provide More Information: Share additional details or context — or let the community know if you've found a solution on your own. 3️⃣ Mark a Reply as Answer: If your question has been answered by a reply, mark the most helpful reply as the solution. Note: This dormant notification will only apply to Discussions with the Thank you for helping bring this Discussion to a resolution! 💬 |
Beta Was this translation helpful? Give feedback.
-
Select Topic Area
Question
Body
Hello, I am new at this. I am using Python to download my smart gas and electricity data a year since it was installed using Octopus Energy API. I have successfully created code to extract this data. I want to take it to the next level and link my day and night rate data to my meter readings. Is this possible? I created this code as I couldn't get any tariff data out at all, but still no luck. I am hopeful that there are some Octopus software ppl here to advise me where I am going wrong. Much appreciated.
import csv
import datetime
import json
import logging
import requests
logger = logging.getLogger(name)
BASE_URL = "https://api.octopus.energy"
def build_url(tariff_code, start_date, end_date):
return f"{BASE_URL}/v1/products/{tariff_code}/standard-unit-rates/?period_from={start_date.isoformat()}&period_to={end_date.isoformat()}"
def fetch_unit_rates(tariff_code, start_date, end_date):
try:
url = build_url(tariff_code, start_date, end_date)
response = requests.get(url)
response.raise_for_status()
return response.json()
except requests.exceptions.RequestException as e:
logger.error(f"Error fetching data: {e}")
return None
def write_to_csv(data, file_name):
with open(file_name, "w") as f:
writer = csv.writer(f)
writer.writerow(["Tariff Code", "Start Date", "End Date", "Unit Rate"])
for row in data:
writer.writerow(row)
def get_unit_rates():
start_date = datetime.date(2017, 1, 1)
end_date = datetime.date.today()
data = []
tariff_codes = TARIFF_CODES = [
"AFFECT-LOYAL-FIX-12M-23-06-21",
"AFFECT-SEG-FIX-12M-20-11-11",
"AFFECT-VAR-23-04-01",
"AGILE-FLEX-22-11-25",
"AGILE-FLEX-BB-23-02-08",
"AGILE-OUTGOING-19-05-13",
"AGILE-OUTGOING-BB-23-02-28",
"COOP-LOYAL-FIX-12M-23-06-21",
"COOP-PP-VAR-20-04-01",
"COOP-SEG-FIX-12M-20-11-11",
"COOP-VAR-23-04-01",
"COSY-22-12-08",
"COSY-BB-23-02-07",
"EB-PP-VAR-21-10-08",
"EP-LOYAL-FIX-12M-23-06-21",
"ES-VAR-23-04-01",
"FLUX-EXPORT-23-02-14",
"FLUX-EXPORT-BB-23-02-14",
"FLUX-IMPORT-23-02-14",
"FLUX-IMPORT-BB-23-02-14",
"GO-GREEN-VAR-22-10-14",
"GO-VAR-22-10-14",
"GO-VAR-BB-23-02-07",
"INTELLI-BB-VAR-23-03-01",
"INTELLI-VAR-22-10-14",
"LOYAL-FIX-12M-23-06-20",
"LOYAL-FIX-12M-BB-23-06-21",
"LP-SEG-FIX-12M-20-11-11",
"LP-VAR-23-04-01",
"OUTGOING-FIX-12M-19-05-13",
"OUTGOING-FIX-12M-BB-23-02-09",
"OUTGOING-SEG-FIX-12M-20-07-07",
"OUTGOING-SEG-FIX-12M-BB-23-02-09",
"PREPAY-VAR-18-09-21",
"PREPAY-VAR-BB-23-04-01",
"VAR-22-11-01",
"VAR-BB-23-04-01"
]
for code in tariff_codes:
rates = fetch_unit_rates(code, start_date, end_date)
if rates:
data.append([code, start_date, end_date, rates["unit_rate"]])
return data
def main():
data = get_unit_rates()
write_to_csv(data, "unit_rates.csv")
if name == "main":
main()
Beta Was this translation helpful? Give feedback.
All reactions