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.

Tibber API
#35
Use updated user.websocket library attached to this post.

Change token / homeId as needed. Add object updates at line 78. Use grp.checkupdate or grp.checkwrite so that LM is not overloaded with unnecessary duplicate values.

Code:
ws = require('user.websocket') json = require('json') http = require('socket.http') ltn12 = require('ltn12') token = '12345' payload = json.encode({ query = '{viewer { websocketSubscriptionUrl } }' }) res = http.request({   url = 'https://api.tibber.com/v1-beta/gql',   method = 'POST',   headers = {     ['Authorization'] = 'Bearer ' .. token,     ['Content-Type'] = 'application/json',     ['Content-Length'] = #payload,   },   source = ltn12.source.string(payload), }) res = json.pdecode(res) if type(res) == 'table' and res.data then   url = res.data.viewer.websocketSubscriptionUrl   client, err = ws.client('sync', 30)   client.protocol = 'graphql-transport-ws'   res, err = client:connect(url)   query = [[   subscription {     liveMeasurement(homeId: "a-b-c-d") {       timestamp       power       accumulatedConsumption       accumulatedCost       currency       minPower       averagePower       maxPower     }   }   ]] else   res = nil end if res then   res, err = client:send(json.encode({     type = 'connection_init',     payload = {       token = token     }   }))   client:receive()   ts, tu = os.microtime()   res, err = client:send(json.encode({     id = ts .. '-' .. tu,     type = 'subscribe',     payload = {       query = query     }   }))   while true do     data, opcode, _, _, err = client:receive()     if data then       if opcode == ws.PING then         client:send(data, ws.PONG)       elseif opcode == ws.TEXT then         data = json.pdecode(data)         if type(data) == 'table' then           -- update objects here         end       end     else       log('receive failed: ' .. tostring(err))       break     end   end else   log('connection failed: ' .. tostring(err)) end if client then   client:close()   client = nil end

Attached Files
.lua   websocket.lua (Size: 13.79 KB / Downloads: 43)
Reply


Messages In This Thread
Tibber API - by Jørn - 06.08.2019, 13:02
RE: Tibber API - by admin - 12.08.2019, 07:39
RE: Tibber API - by Jørn - 12.08.2019, 10:15
RE: Tibber API - by admin - 12.08.2019, 10:24
RE: Tibber API - by thomasoppida - 14.08.2019, 16:09
RE: Tibber API - by admin - 14.08.2019, 16:37
RE: Tibber API - by thomasoppida - 14.08.2019, 16:48
RE: Tibber API - by admin - 15.08.2019, 14:07
RE: Tibber API - by thomasoppida - 16.08.2019, 05:14
RE: Tibber API - by eirik - 28.11.2019, 19:53
RE: Tibber API - by stemic01 - 07.02.2020, 23:28
RE: Tibber API - by stemic01 - 01.03.2020, 22:26
RE: Tibber API - by stemic01 - 31.03.2020, 18:02
RE: Tibber API - by admin - 01.04.2020, 06:43
RE: Tibber API - by Jørn - 03.04.2020, 18:49
RE: Tibber API - by Erwin van der Zwart - 04.04.2020, 07:23
RE: Tibber API - by Jørn - 04.04.2020, 13:55
RE: Tibber API - by admin - 04.04.2020, 13:57
RE: Tibber API - by Jørn - 04.04.2020, 14:05
RE: Tibber API - by Erwin van der Zwart - 05.04.2020, 00:10
RE: Tibber API - by Jørn - 05.04.2020, 21:08
RE: Tibber API - by stemic01 - 14.04.2020, 22:02
RE: Tibber API - by Rune - 07.01.2021, 19:12
RE: Tibber API - by stemic01 - 11.01.2021, 21:43
RE: Tibber API - by Rune - 12.01.2021, 20:37
RE: Tibber API - by Jørn - 17.01.2021, 17:22
RE: Tibber API - by Odd Egil Aasheim - 09.08.2021, 21:08
RE: Tibber API - by Erwin van der Zwart - 09.08.2021, 23:01
RE: Tibber API - by Odd Egil Aasheim - 10.08.2021, 06:17
RE: Tibber API - by victor.back - 30.01.2022, 10:07
RE: Tibber API - by admin - 31.01.2022, 08:44
RE: Tibber API - by mjaanes - 06.01.2023, 10:00
RE: Tibber API - by CarlS - 07.01.2023, 15:36
RE: Tibber API - by stemic01 - 07.01.2023, 23:17
RE: Tibber API - by admin - 10.01.2023, 10:52
RE: Tibber API - by eilert - 18.01.2023, 19:28
RE: Tibber API - by CarlS - 11.01.2023, 20:43
RE: Tibber API - by admin - 19.01.2023, 06:39
RE: Tibber API - by CarlS - 22.02.2024, 10:09
RE: Tibber API - by admin - 23.02.2024, 11:35
RE: Tibber API - by CarlS - 25.02.2024, 17:08
RE: Tibber API - by davidkoch29 - 23.04.2024, 06:16
RE: Tibber API - by Jørn - 23.04.2024, 09:45
RE: Tibber API - by davidkoch29 - 23.04.2024, 09:47
RE: Tibber API - by admin - 23.04.2024, 09:58
RE: Tibber API - by Odd Egil Aasheim - 22.01.2025, 21:00
RE: Tibber API - by admin - 23.01.2025, 06:55
RE: Tibber API - by Odd Egil Aasheim - 23.01.2025, 08:28

Forum Jump: