30.09.2022, 07:42
A for loop makes the code more compact and can't cause copy-paste errors. DPT 11 expects a table with year/month/day, not a string.
Code:
require('json')
https = require 'ssl.https'
date = os.date('%Y-%m-%d')
url = 'https://www.vattenfall.se/api/price/spot/pricearea/' .. date .. '/' .. date .. '/SN3'
status = ssl.https.request(url)
data = json.decode(status)
if type(data) ~= 'table' then
return
end
ts = data[ 1 ].TimeStampDay
if type(ts) == 'string' then
ts = ts:split('-')
grp.checkupdate('5/5/28', {
year = tonumber(ts[1]),
month = tonumber(ts[2]),
day = tonumber(ts[3])
})
end
min = math.huge
max = -math.huge
cnt = 0
sum = 0
for i, item in ipairs(data) do
price = item.Value
min = math.min(min, price)
max = math.max(max, price)
sum = sum + price
cnt = cnt + 1
grp.checkupdate('5/5/' .. i, price)
end
grp.checkupdate('5/5/25', sum / cnt)
grp.checkupdate('5/5/26', max)
grp.checkupdate('5/5/27', min)