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.

REST API to KNX groups
#6
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)
Reply


Messages In This Thread
REST API to KNX groups - by sx3 - 27.09.2022, 15:39
RE: REST API to KNX groups - by admin - 28.09.2022, 05:32
RE: REST API to KNX groups - by sx3 - 28.09.2022, 07:39
RE: REST API to KNX groups - by admin - 28.09.2022, 07:40
RE: REST API to KNX groups - by sx3 - 29.09.2022, 15:38
RE: REST API to KNX groups - by admin - 30.09.2022, 07:42

Forum Jump: