Created
April 25, 2025 16:26
-
-
Save Mad182/b1824c84b08eff727a14e26e94411a95 to your computer and use it in GitHub Desktop.
A php script that fetches nordpool electricity prices for today and check if the current hour is among the cheapest, I use it for water heater control
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
date_default_timezone_set('Europe/Riga'); // Ensure correct time zone | |
$today = date('Y-m-d'); | |
$area = 'LV'; | |
$numhours = 2; | |
$url = "https://dataportal-api.nordpoolgroup.com/api/DayAheadPrices?date=$today&market=DayAhead&deliveryArea=$area¤cy=EUR"; | |
// Fetch JSON data from the API | |
$response = file_get_contents($url); | |
if ($response === false) { | |
die("Failed to fetch data."); | |
} | |
$data = json_decode($response, true); | |
if (!isset($data['multiAreaEntries'])) { | |
die("Invalid data format."); | |
} | |
$entries = $data['multiAreaEntries']; | |
$hourPrices = []; | |
// Go through each hourly entry and extract the price for the target area | |
foreach ($entries as $entry) { | |
$start = strtotime($entry['deliveryStart']); | |
$hour = (int) date('G', $start); // Extract hour in 0–23 format | |
if (isset($entry['entryPerArea'][$area])) { | |
$price = floatval($entry['entryPerArea'][$area]); | |
$hourPrices[$hour] = $price; | |
} | |
} | |
// Sort prices and get 2 lowest hours | |
asort($hourPrices); | |
$lowestHours = array_slice(array_keys($hourPrices), 0, $numhours); | |
// Check current hour | |
$currentHour = (int) date('G'); // 0–23 hour | |
// Output true if current hour is among the lowest | |
echo in_array($currentHour, $lowestHours) ? 'true' : 'false'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment