Posts: 76
Threads: 5
Joined: Sep 2016
Reputation:
4
I have been trying to pull some data like power consumption and prices. Limited Lua/json scripting skills have brought it to a stop, can anybody help`?
API Guide https://developer.tibber.com/docs/guides/calling-api
Best regards, Jørn.
Posts: 7769
Threads: 42
Joined: Jun 2015
Reputation:
447
Change token and data query as needed:
Code: https = require('ssl.https')
json = require('json')
ltn12 = require('ltn12')
token = 'd1007ead2dc84a2b82f0de19451c5fb22112f7ae11d19bf2bedb224a003ff74a'
data = json.encode({
query = '{viewer {homes {currentSubscription {priceInfo {current {total energy tax startsAt }}}}}}'
})
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)
log(resp)
else
log(res, code)
end
Posts: 76
Threads: 5
Joined: Sep 2016
Reputation:
4
Superb! But i get "nesting too deep" before all data is retrieved, is this issue on Tibber side or LM `?
Code: * table:
[data]
* table:
[viewer]
* table:
[homes]
* table:
[1]
* table:
[currentSubscription]
* table:
[priceInfo]
* table:
nesting too deep
Best regards, Jørn.
Posts: 7769
Threads: 42
Joined: Jun 2015
Reputation:
447
There's a limit of how deep the table can be for log() function, try this:
Code: log(resp.data.viewer.homes)
Posts: 41
Threads: 9
Joined: Apr 2018
Reputation:
0
Hi,
How de we retrieve a value from table within table here, and write this to a group address?
grp.write('32/1/16', 'tbl[1][1][1][1][4][1]')?
kind regards
thomas
Posts: 7769
Threads: 42
Joined: Jun 2015
Reputation:
447
Depends, can you show log output for this table?
Posts: 41
Threads: 9
Joined: Apr 2018
Reputation:
0
Posts: 7769
Threads: 42
Joined: Jun 2015
Reputation:
447
Code: homes = resp.data.viewer.homes
info = homes[ 1 ].currentSubscription.priceInfo.current
log(info.energy)
log(info.tax)
log(info.total)
Posts: 41
Threads: 9
Joined: Apr 2018
Reputation:
0
That worked :-) Thank you again :-)
Posts: 32
Threads: 12
Joined: Oct 2015
Reputation:
0
Hi
I've been testing this for a while, and get all the readings from viewer, homes, currentSubscriptions and so on.
But the liveMeasurement I can't really figure out.
Have anyone got this to work?
Eirik
Posts: 60
Threads: 12
Joined: Oct 2019
Reputation:
0
Very interesting. Anyone who knows how to also integrate the current Power consumption and data from the TIBBER PULSE ? Would be great With some help.
Schneider Wiser (homeLynk), Power Tags, DALI, Multitouch Pro, Panasonic Heating pump, Flexit balansed ventilation, HUE integration, Lemus Speaker system. Tibber integration.
Posts: 60
Threads: 12
Joined: Oct 2019
Reputation:
0
01.03.2020, 22:26
(This post was last modified: 11.01.2023, 18:13 by stemic01.)
I would really like to get the live consumption "REAL TIME SUBSCRIPTION" data for visualization and making trends.
https://developer.tibber.com/Explorer (Demo token / Real time Consumption)
Would be deeply greatfull if any of you LUA specialists were able to help me retrieve this data and for example Write it to a Group address I could call in my Dashboard Interface.
Here is the "code" from the Tibber API. How to get this into something usefull in LUA?
subscription{
liveMeasurement(homeId:"xxxxxxxxxxxxxxxxxxxxx"){
timestamp
power
accumulatedConsumption
accumulatedCost
currency
minPower
averagePower
maxPower
}
}
Schneider Wiser (homeLynk), Power Tags, DALI, Multitouch Pro, Panasonic Heating pump, Flexit balansed ventilation, HUE integration, Lemus Speaker system. Tibber integration.
Posts: 60
Threads: 12
Joined: Oct 2019
Reputation:
0
I try to boost this thread again. Any takers?
Schneider Wiser (homeLynk), Power Tags, DALI, Multitouch Pro, Panasonic Heating pump, Flexit balansed ventilation, HUE integration, Lemus Speaker system. Tibber integration.
Posts: 7769
Threads: 42
Joined: Jun 2015
Reputation:
447
Use this websocket client library: https://forum.logicmachine.net/showthrea...23#pid7823
Create a user library named websocket and add this line after 'Connection: Upgrade', (line 289):
Code: 'Sec-WebSocket-Protocol: graphql-ws',
Example resident script, sleep time = 0, change token and homeId as needed:
Code: if not client then
ws = require('user.websocket')
json = require('json')
token = 'd1007ead2dc84a2b82f0de19451c5fb22112f7ae11d19bf2bedb224a003ff74a'
query = [[
subscription{
liveMeasurement(homeId:"c70dcbe5-4485-4821-933d-a8a86452737b"){
timestamp
power
accumulatedConsumption
accumulatedCost
currency
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 = 1,
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
data = json.pdecode(data)
if data then
log(data)
end
else
log('receive failed: ' .. tostring(err))
client:close()
client = nil
end
end
Posts: 76
Threads: 5
Joined: Sep 2016
Reputation:
4
03.04.2020, 18:49
(This post was last modified: 03.04.2020, 18:50 by Jørn.)
Works great But, how to pull data from the table to i.e. to a grp.write`?
Best regards, Jørn.
Posts: 1764
Threads: 6
Joined: Jul 2015
Reputation:
117
04.04.2020, 07:23
(This post was last modified: 04.04.2020, 07:23 by Erwin van der Zwart.)
Hi,
At line 51 (log data) your need to add your code, look at the data table and select the value you want to send to KNX, i don't know the content of the data fields but something like:
Code: -- log(data)
grp.checkwrite('1/1/1', data.yoursubfield1)
grp.checkwrite('1/1/2', data.yoursubfield2)
BR,
Erwin
Posts: 76
Threads: 5
Joined: Sep 2016
Reputation:
4
(04.04.2020, 07:23)Erwin van der Zwart Wrote: Hi,
At line 51 (log data) your need to add your code, look at the data table and select the value you want to send to KNX, i don't know the content of the data fields but something like:
Code: -- log(data)
grp.checkwrite('1/1/1', data.yoursubfield1)
grp.checkwrite('1/1/2', data.yoursubfield2)
BR,
Erwin
I've tried different combinations of this with no luck. I.e. data.power is nil.
The "data" table looks like this
Code: Tibber websocket 04.04.2020 15:53:26
* table:
["type"]
* string: data
["payload"]
* table:
["data"]
* table:
["liveMeasurement"]
* table:
["timestamp"]
* string: 2020-04-04T15:53:26+02:00
["accumulatedCost"]
* number: 5.133123
["maxPower"]
* number: 8190
["power"]
* number: 1555
["currency"]
* string: NOK
["averagePower"]
* number: 4301.6
["accumulatedConsumption"]
* number: 68.357767
["minPower"]
* number: 1269
["id"]
* number: 1
Best regards, Jørn.
Posts: 7769
Threads: 42
Joined: Jun 2015
Reputation:
447
Try data.payload.data.liveMeasurement.power
Posts: 76
Threads: 5
Joined: Sep 2016
Reputation:
4
04.04.2020, 14:05
(This post was last modified: 04.04.2020, 14:12 by Jørn.)
That worked ;o) But sometimes report error attempt to index field 'payload' (a nil value) stack traceback:
why would this be`?
Best regards, Jørn.
Posts: 1764
Threads: 6
Joined: Jul 2015
Reputation:
117
05.04.2020, 00:10
(This post was last modified: 05.04.2020, 00:11 by Erwin van der Zwart.)
Hi,
You should check if the table content is valid before drilling down deeper...
Code: if data then
if data.payload then
if data.payload.power then
grp.checkwrite('1/1/1’, data.payload.power)
end
end
end
BR,
Erwin
|