31.01.2022, 08:44
Change token / group addresses as needed:
Code:
https = require('ssl.https')
json = require('json')
ltn12 = require('ltn12')
token = '476c477d8a039529478ebd690d35ddd80e3308ffc49b59c65b142321aee963a4' -- demo token
query = [[
{
viewer {
homes {
currentSubscription {
priceInfo {
today {
total
startsAt
}
tomorrow {
total
startsAt
}
}
}
}
}
}
]]
data = json.encode({ query = query })
tbl = {}
res, code = https.request({
url = 'https://api.tibber.com/v1-beta/gql',
method = 'POST',
headers = {
['authorization'] = 'Bearer ' .. token,
['content-type'] = 'application/json',
['content-length'] = #data,
},
source = ltn12.source.string(data),
sink = ltn12.sink.table(tbl),
})
if res and code == 200 then
resp = table.concat(tbl)
resp = json.pdecode(resp)
homes = resp.data.viewer.homes
today = homes[ 1 ].currentSubscription.priceInfo.today or {}
tomorrow = homes[ 1 ].currentSubscription.priceInfo.tomorrow or {}
function getprice(hour)
local entry
local index = hour + 1
if index > 24 then
entry = tomorrow[ index - 24 ]
else
entry = today[ index ]
end
if entry then
return entry.total
end
end
hour = os.date('*t').hour
grp.update('1/1/1', getprice(hour))
grp.update('1/1/2', getprice(hour + 1))
grp.update('1/1/3', getprice(hour + 2))
else
log(res, code)
end