This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

NordPool prices in LogicMachine
#8
Hello,

Here is a new script for a hourly pricing for Latvia, Finland, Lithuania, Estonia

Code:
country = 'lv' -- Latvia
--country = 'fi' -- Finland
--country = 'lt' -- Lithuania
--country = 'ee' -- Estonia

current_hour_price_addr = '43/1/2'
cheapest_hour_addr = '43/1/3'
cheapest_hour_price_addr = '43/1/4'
costliest_hour_addr = '43/1/5'
costliest_hour_price_addr = '43/1/6'

local current_date = os.date('*t')
local day_start_ts = os.time({year = current_date.year, month = current_date.month, day = current_date.day, hour = 0, min = 0, sec = 0})
local day_end_ts = os.time({year = current_date.year, month = current_date.month, day = current_date.day, hour = 23, min = 59, sec = 59})

local previous_date = os.date('%F', os.time() - 24 * 60 * 60)
local next_date = os.date('%F', os.time() + 24 * 60 * 60)
local url = 'https://dashboard.elering.ee/api/nps/price?start=' .. previous_date .. 'T00%3A00%3A00.999Z&end=' .. next_date .. 'T00%3A00%3A00.999Z'

--log(url)

local res, code = require('socket.http').request(url)

if not (res and code == 200) then
  log('request error: ', res, code)
  return
end

local data = require('json').pdecode(res)

if not data or not data.data or not data.data[country] then
  log('No data for country: ' .. country)
  return
end

local hourly_prices = {}
for h = 0, 23 do
  hourly_prices[h] = { sum = 0, count = 0 }
end


for _, record in ipairs(data.data[country]) do
  local timestamp = record.timestamp
  local price = record.price

  if timestamp >= day_start_ts and timestamp <= day_end_ts then
    local record_time = os.date('*t', timestamp)
    local hour = record_time.hour

    if hourly_prices[hour] then
      hourly_prices[hour].sum = hourly_prices[hour].sum + price
      hourly_prices[hour].count = hourly_prices[hour].count + 1
    end
  end
end


local min_avg_price = 99999
local max_avg_price = -1
local cheapest_hour = -1
local costliest_hour = -1
local current_hour_avg_price = nil

for hour, price_data in pairs(hourly_prices) do
  if price_data.count > 0 then
    local avg_price = price_data.sum / price_data.count
    log(string.format("Average price per hour %02d:00 - %.2f EUR/MWh", hour, avg_price))

    if avg_price < min_avg_price then
      min_avg_price = avg_price
      cheapest_hour = hour
    end

    if avg_price > max_avg_price then
      max_avg_price = avg_price
      costliest_hour = hour
    end
   
    if hour == current_date.hour then
      current_hour_avg_price = avg_price
    end
  end
end

if current_hour_avg_price then
  log(string.format('Price for current hour (%02d:00): %.2f EUR/MWh', current_date.hour, current_hour_avg_price))
  grp.write(current_hour_price_addr, current_hour_avg_price)
end

if cheapest_hour ~= -1 then
  log(string.format('Cheapest hour: %02d:00, Price: %.2f EUR/MWh', cheapest_hour, min_avg_price))
  local cheapest_hour_time = { wday = current_date.wday, hour = cheapest_hour }
  grp.write(cheapest_hour_addr, cheapest_hour_time)
  grp.write(cheapest_hour_price_addr, min_avg_price)
else
  log('Could not calculate the cheapest hour.')
end

if costliest_hour ~= -1 then
  log(string.format('Costliest hour: %02d:00, Price: %.2f EUR/MWh', costliest_hour, max_avg_price))
  local costliest_hour_time = { wday = current_date.wday, hour = costliest_hour }
  grp.write(costliest_hour_addr, costliest_hour_time)
  grp.write(costliest_hour_price_addr, max_avg_price)
else
  log('Could not calculate the costliest hour.')
end

Attached Files
.lua   electricity_price_lv_20260202.lua (Size: 3.37 KB / Downloads: 2)
Reply


Messages In This Thread
RE: NordPool prices in LogicMachine - by RomansP - 02.02.2026, 09:24

Forum Jump: