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
#6
Hello,

We have prepared script for electricity prices for Denmark, Norway, Sweden and Germany

Code:
price_area = 'DK1' -- west Denmark DK1 grid region
--price_area = 'DK2' -- east Denmark DK2 grid region
--price_area = 'DE' -- Germany
--price_area = 'NO2' -- southern and southwestern Norway
--price_area = 'SE3' -- north Sweden
--price_area = 'SE4' -- south Sweden

current_price_addr = '40/1/18' -- 14. 4 byte floating point
cheapest_hour_addr = '40/1/16' -- 10. 3 byte time / day
cheapest_hour_price_addr = '40/1/17' -- 14. 4 byte floating point
costliest_hour_addr = '40/1/19' -- 10. 3 byte time / day
costliest_hour_price_addr = '40/1/20' -- 14. 4 byte floating point

start_date = os.date('%Y-%m-%d')
end_date = os.date('%Y-%m-%d', os.time() + 48 * 60 * 60)

url = 'https://api.energidataservice.dk/dataset/DayAheadPrices?start=' ..
      start_date .. '&end=' .. end_date ..
      '&filter={"PriceArea":["' .. price_area .. '"]}'
resp, code = require('socket.http').request(url)

if resp and code == 200 then
  data = require('json').pdecode(resp)
else
  log('request error', resp, code)
  return
end

if not data or not data.records then
  log('No data for price area: ' .. price_area)
  return
end

curr_day = tonumber(os.date('%u'))
next_day = curr_day == 7 and 1 or (curr_day + 1)
curr_date = os.date('%Y%m%d')
curr_hour_str = curr_date .. os.date('%H')
curr_hour = tonumber(curr_hour_str)
curr_min = math.floor(tonumber(os.date('%M')) / 15) * 15
curr_ts = curr_hour_str .. string.format('%02d', curr_min)

prices = {}

for _, record in ipairs(data.records) do
  ts = record.TimeDK:gsub('%-', ''):gsub('T', ''):gsub(':', '')
  hour = tonumber(ts:sub(1, 10))
  price = record.DayAheadPriceEUR

  if hour >= curr_hour then
    store = prices[ hour ] or { sum = 0, count = 0 }
    store.sum = store.sum + price
    store.count = store.count + 1
    prices[ hour ] = store
  end

  if ts:sub(1, 12) == curr_ts then
    curr_price = price
  end
end

hourly = {}

for hour, store in pairs(prices) do
  price = store.sum / store.count
  hourly[ #hourly + 1 ] = {
    hour = hour,
    price = price
  }
end

table.sort(hourly, function(a, b)
  return a.price > b.price
end)

function update(store, hour_addr, price_addr)
  local hour = tostring(store.hour)
  local date = hour:sub(1, 8)
  local day = date == curr_date and curr_day or next_day

  grp.checkupdate(hour_addr, { day = day, hour = hour:sub(9, 10) })
  grp.checkupdate(price_addr, store.price)
end

grp.checkupdate(current_price_addr, curr_price)
update(hourly[1], costliest_hour_addr, costliest_hour_price_addr)
update(hourly[#hourly], cheapest_hour_addr, cheapest_hour_price_addr)

Attached Files
.lua   electricity_price_dk_20251013.lua (Size: 2.49 KB / Downloads: 1)
Reply


Messages In This Thread
RE: NordPool prices in LogicMachine - by RomansP - Yesterday, 08:53

Forum Jump: