07.01.2023, 15:36
(06.01.2023, 10:00)mjaanes Wrote: Tibber seem to have updated their API (see Tibber Developer).
Has anybody updated the LUA script to read consumption from HAN port via the Tibber API?
My script has stopped working with the message:
string: connection failed: Websocket Handshake failed: Invalid Sec-Websocket-Accept (expected ******* = got nil)
I have the same problem..
My resident script has a os.sleep beacause the CPU-load went too high with real-time subscription
Code:
----------------------------
if not client then
os.sleep(50) -- wait 50 seconds
number_of_measurements = 0
ws = require('user.websocket')
json = require('json')
token = 'mySecretToken'
--log('starting')
query = [[
subscription{
liveMeasurement(homeId:"mySecretHomeId"){
timestamp
power
accumulatedConsumption
accumulatedConsumptionLastHour
minPower
averagePower
maxPower
}
}
]]
url = 'wss://api.tibber.com/v1-beta/gql/subscriptions'
client, err = ws.client('sync', 10)
res, err = client:connect(url)
if res then
client:send(json.encode({
type = 'connection_init',
payload = 'token=' .. token,
}))
client:send(json.encode({
id = 2,
type = 'start',
payload = {
query = query
}
}))
else
log('connection failed: ' .. tostring(err))
client:close()
client = nil
end
else
data, _, _, opcode, err = client:receive()
if data then
--log('data= ' ..data)
data2 = json.pdecode(data)
if data2 and data2.payload then
tibber = data2.payload.data.liveMeasurement
--log(tibber)
timestamp_text = tibber.timestamp
timestamp_text_sub = string.sub(timestamp_text, 12, 19)
grp.write('32/4/1', (tonumber(tibber.power))/1000)
grp.write('32/4/2', (tonumber(tibber.accumulatedConsumption)))
grp.write('32/4/3', (tonumber(tibber.accumulatedConsumptionLastHour)))
grp.write('32/4/4', (tonumber(tibber.minPower))/1000)
grp.write('32/4/5', (tonumber(tibber.averagePower))/1000)
grp.write('32/4/6', (tonumber(tibber.maxPower))/1000)
grp.write('32/4/7', timestamp_text_sub)
--log('Suksess')
client:close()
client = nil
end
else
log('receive failed: ' .. tostring(err))
client:close()
client = nil
end
end