10.11.2022, 12:42
What exactly is not working for min/max/avg? Do you have objects created with correct data type?
This will produce a string with cheapest 4 hours. Set 5/5/28 datatype to 250 byte string.
For general info on Lua read this book: http://www.lua.org/pil/contents.html
This will produce a string with cheapest 4 hours. Set 5/5/28 datatype to 250 byte string.
Code:
url = 'https://api.energidataservice.dk/dataset/Elspotprices?offset=0&filter=%7B%22PriceArea%22:%22dk1%22%7D&sort=HourDK%20DESC&timezone=dk&limit=24'
resp = require('ssl.https').request(url)
data = require('json').decode(resp).records
max = 0
min = math.huge
sum = 0
for i, item in ipairs(data) do
price = item.SpotPriceDKK
max = math.max(max, price)
min = math.min(min, price)
sum = sum + price
grp.checkupdate('5/5/' .. i, price)
item.hour = tonumber(item.HourDK:match('T(%d+)'))
end
avg = sum / #data
grp.checkupdate('5/5/25', avg)
grp.checkupdate('5/5/26', max)
grp.checkupdate('5/5/27', min)
table.sort(data, function(a, b)
return a.SpotPriceDKK < b.SpotPriceDKK
end)
list = {}
for i = 1, 4 do
item = data[ i ]
list[ #list + 1 ] = string.format('%d = %.2f', item.hour, item.SpotPriceDKK)
end
hours = table.concat(list, '; ')
grp.checkupdate('5/5/28', hours)
For general info on Lua read this book: http://www.lua.org/pil/contents.html