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
#1
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.
Reply
#2
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
Reply
#3
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.
Reply
#4
There's a limit of how deep the table can be for log() function, try this:
Code:
log(resp.data.viewer.homes)
Reply
#5
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
Reply
#6
Depends, can you show log output for this table?
Reply
#7
   
Reply
#8
Code:
homes = resp.data.viewer.homes
info = homes[ 1 ].currentSubscription.priceInfo.current
log(info.energy)
log(info.tax)
log(info.total)
Reply
#9
That worked :-) Thank you again :-)
Reply
#10
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
Reply
#11
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.
Reply
#12
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.
Reply
#13
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.
Reply
#14
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
Reply
#15
Works great Big Grin But, how to pull data from the table to i.e. to a grp.write`?

Best regards, Jørn.
Reply
#16
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
Reply
#17
(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.
Reply
#18
Try data.payload.data.liveMeasurement.power
Reply
#19
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.
Reply
#20
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
Reply


Forum Jump: