15.03.2023, 21:20
Hello,
I'm trying to create a script to retrieve tomorrow's electricity prices with api: https://www.hvakosterstrommen.no/strompris-api.
I try this:
I'm having a bit of trouble getting the data out.
I'm trying to create a script to retrieve tomorrow's electricity prices with api: https://www.hvakosterstrommen.no/strompris-api.
I try this:
Code:
local http = require('ssl.https')
local json = require('json')
local date = os.date('*t', os.time() + 86400) -- get tomorrow's date
local year = tostring(date.year)
local month = string.format('%02d', date.month)
local day = string.format('%02d', date.day)
local area = 'NO1' -- specify area
local url = string.format('https://www.hvakosterstrommen.no/api/v1/prices/%s/%s-%s_%s.json', year, month, day, area)
print('Fetching data from URL:', url)
local body, code, headers, status = http.request(url)
print('HTTP response code:', code)
if code == 200 then
print('Data retrieved successfully')
print('Response body:', body)
local data = json.decode(body)
local group_address = '32/1/%d' -- starting group address
for i, item in ipairs(data) do
local hour = tonumber(string.sub(item.time_start, 12, 13))
local price = item.NOK_per_kWh
print('Hour:', hour)
print('Price:', price)
if price ~= nil then
local value = string.format('%014s', string.format('%.4f', price * 10000)):gsub(' ', '0')
print('Value:', value)
grp.write(string.format(group_address, hour), value, '14ByteAsc')
end
end
else
print('Error fetching data: ' .. status)
end
I'm having a bit of trouble getting the data out.