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.

Power price API -> grp. adresses on Wiser
#6
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.
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
Reply


Messages In This Thread
RE: Power price API -> grp. adresses on Wiser - by admin - 10.11.2022, 12:42

Forum Jump: