05.11.2025, 22:29
(This post was last modified: 05.11.2025, 22:30 by jerryhenke.)
(19.12.2022, 18:08)martins.neibergs@gmail.com Wrote: Hi!
Has anyone had any experience with getting (integrating) NordPool prices into LogicMachine?
Tried different versions but nothing worked.
What about this one?
https://dashboard.elering.ee/en/nps/pric...59:59.999Z
https://dashboard.elering.ee/api/nps/pri...%3A00.999Z
Anyone had any luck or experience? Can you please share scripts for getting NordPool prices into logic machine?
Hi!
I do two scripts
Both "Scheduled"
Script 1 -
Collecting electric price from API. running 4 times/day hour 17,18,21,23 amd minute 5.
Code:
CODE
-- API-skript (spara per datum)
local json = require("json")
local ltn12 = require("ltn12")
local http = require("socket.http")
local DECIMALS = 5
local PRISKLASS = "SE2"
local function fetch_prices(date_timestamp)
local year = os.date("%Y", date_timestamp)
local month_day = os.date("%m-%d", date_timestamp)
local url = string.format("https://www.elprisetjustnu.se/api/v1/prices/%s/%s_%s.json", year, month_day, PRISKLASS)
local resp = {}
local res, code = http.request({url = url, method = "GET", sink = ltn12.sink.table(resp)})
if code == 200 then
local decoded = json.pdecode(table.concat(resp))
if decoded then
local prices = {}
for i,v in ipairs(decoded) do
local time_start = v.time_start -- "2025-09-10T21:15:00+02:00"
local hh = string.sub(time_start, 12, 13)
local mm = string.sub(time_start, 15, 16)
local val = tonumber(string.format("%." .. DECIMALS .. "f", v.SEK_per_kWh))
if mm == "00" then
-- timpris: duplicera till kvart
for _, q in ipairs({"00","15","30","45"}) do
prices[hh .. ":" .. q] = val
end
else
prices[hh .. ":" .. mm] = val
end
end
return prices
end
end
return nil
end
local function save_prices()
local now = os.time()
-- idag och imorgon som timestamps (midnatt)
local t = os.date("*t", now)
t.hour, t.min, t.sec = 0,0,0
local idag_ts = os.time(t)
local imorgon_ts = idag_ts + 86400
-- Spara dagens priser i storage med datumnyckel
local ds_idag = os.date("%Y-%m-%d", idag_ts)
local p_idag = fetch_prices(idag_ts)
if p_idag then
storage.set("elpris_" .. ds_idag, json.encode(p_idag))
log("Sparade elpriser för " .. ds_idag)
else
log("️ Kunde inte hämta priser för " .. ds_idag)
end
-- Spara morgondagens priser (förutsatt kl 18+ eller när du vill hämta)
if tonumber(os.date("%H", now)) >= 18 then
local ds_im = os.date("%Y-%m-%d", imorgon_ts)
local p_im = fetch_prices(imorgon_ts)
if p_im then
storage.set("elpris_" .. ds_im, json.encode(p_im))
log("Sparade elpriser för " .. ds_im)
else
log("️ Kunde inte hämta priser för " .. ds_im)
end
end
-- Rensa gamla nycklar (behåll t.ex. -3..+2 dagar)
for i = 4, 30 do
local old = os.date("%Y-%m-%d", idag_ts - i*86400)
storage.set("elpris_" .. old, nil)
end
end
save_prices()Script 2
Writing current electrical price to KNX address
Running as scheduled minute 0,15,30,45
Code:
local json = require("json")
local GA_ELPRIS = "34/4/99"
local function round_to_quarter()
local now = os.time()
local hh = tonumber(os.date("%H", now))
local min = tonumber(os.date("%M", now))
min = math.floor(min / 15) * 15
return string.format("%02d:%02d", hh, min)
end
-- försök hitta pris i dataset för en specifik datumnyckel
local function find_in_date(date_str, hhmm)
local key = "elpris_" .. date_str
local j = storage.get(key)
if not j then return nil end
local prices = json.decode(j)
if not prices then return nil end
local p = prices[hhmm]
if p then return p, key end
-- fallback: sök närmaste tidigare kvart inom detta dataset
local hh = tonumber(string.sub(hhmm,1,2))
local mm = tonumber(string.sub(hhmm,4,5))
local kvart_list = {0,15,30,45}
for h = hh, 0, -1 do
for i = #kvart_list, 1, -1 do
local q = kvart_list[i]
if (h < hh) or (q <= mm) then
local try = string.format("%02d:%02d", h, q)
if prices[try] then
return prices[try], key, try
end
end
end
end
return nil
end
-- Huvudfunktion som provar datum i prioriterad ordning
local function get_price_for_quarter()
local now = os.time()
local hhmm = round_to_quarter()
local date_today = os.date("%Y-%m-%d", now)
local date_tomorrow = os.date("%Y-%m-%d", now + 86400)
local date_yesterday = os.date("%Y-%m-%d", now - 86400)
local candidates = {date_today, date_tomorrow, date_yesterday}
for _, d in ipairs(candidates) do
local p, used_key, used_q = find_in_date(d, hhmm)
if p then
-- used_q kan vara nil om exakt hhmm träffade; annars är used_q den kvarten vi använde
local used = used_q or hhmm
return p, used, used_key
end
end
return nil, hhmm, nil
end
-- Anropa och skriv till KNX
local pris, used_hhmm, used_key = get_price_for_quarter()
if pris then
local pris_5dec = string.format("%.5f", pris)
log(string.format("Skriver elpris kl %s (från %s): %s kr/kWh", used_hhmm, used_key or "ingen", pris_5dec))
grp.write(GA_ELPRIS, tonumber(pris_5dec))
else
log("️ Ingen elprisinformation hittades för " .. used_hhmm)
end