Getting the electricity prices in Denmark was easier than I expected. In collaboration with Gemini a free API was identified. We wrote a small python script to get the data. More about the API can be found here https://www.elprisenligenu.dk/elpris-api. This enables us to continue the project with real data.
import requests
import json
from datetime import datetime
def hent_elpriser(prisområde=”DK1″):
“””Fetches electricity prices for a given price area.”””
today = datetime.now()
date_str = today.strftime(“%Y/%m-%d”)
url = f”https://www.elprisenligenu.dk/api/v1/prices/{date_str}_{prisområde}.json”
try:
response = requests.get(url)
response.raise_for_status()
data = response.json()
return data
except requests.exceptions.RequestException as e:
print(f”Error fetching electricity prices: {e}”)
return None
except json.JSONDecodeError as e:
print(f”Error decoding JSON: {e}”)
return None
elpriser = hent_elpriser()
if elpriser:
for time_data in elpriser:
try:
price = time_data.get(‘DKK_per_kWh’)
time_start = time_data.get(‘time_start’)
time_end = time_data.get(‘time_end’)
if price is not None and time_start is not None and time_end is not None:
# Convert ISO time string to a readable format
start_time_obj = datetime.fromisoformat(time_start)
end_time_obj = datetime.fromisoformat(time_end)
# Format the time as “HH:MM”
start_time_str = start_time_obj.strftime(“%H:%M”)
end_time_str = end_time_obj.strftime(“%H:%M”)
print(f”Time: {start_time_str} – {end_time_str}, Price: {price} DKK/kWh”)
else:
print(“Missing data in time entry”)
except Exception as e:
print(f”Error processing data: {e}”)
which gives us this result :
Time: 00:00 – 01:00, Price: 0.73616 DKK/kWh
Time: 01:00 – 02:00, Price: 0.7093 DKK/kWh
Time: 02:00 – 03:00, Price: 0.70796 DKK/kWh
Time: 03:00 – 04:00, Price: 0.72818 DKK/kWh
Time: 04:00 – 05:00, Price: 0.74362 DKK/kWh
Time: 05:00 – 06:00, Price: 0.81964 DKK/kWh
Time: 06:00 – 07:00, Price: 1.07305 DKK/kWh
Time: 07:00 – 08:00, Price: 1.10215 DKK/kWh
Time: 08:00 – 09:00, Price: 0.8074 DKK/kWh
Time: 09:00 – 10:00, Price: 0.59307 DKK/kWh
Time: 10:00 – 11:00, Price: 0.19784 DKK/kWh
Time: 11:00 – 12:00, Price: -0.0003 DKK/kWh
Time: 12:00 – 13:00, Price: -0.00418 DKK/kWh
Time: 13:00 – 14:00, Price: -0.00187 DKK/kWh
Time: 14:00 – 15:00, Price: 0.00739 DKK/kWh
Time: 15:00 – 16:00, Price: 0.49781 DKK/kWh ……………………………………….