Logic Machine Forum
Tibber API - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Tibber API (/showthread.php?tid=2185)

Pages: 1 2 3


Tibber API - Jørn - 06.08.2019

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


RE: Tibber API - admin - 12.08.2019

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



RE: Tibber API - Jørn - 12.08.2019

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



RE: Tibber API - admin - 12.08.2019

There's a limit of how deep the table can be for log() function, try this:
Code:
log(resp.data.viewer.homes)



RE: Tibber API - thomasoppida - 14.08.2019

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


RE: Tibber API - admin - 14.08.2019

Depends, can you show log output for this table?


RE: Tibber API - thomasoppida - 14.08.2019

   


RE: Tibber API - admin - 15.08.2019

Code:
homes = resp.data.viewer.homes
info = homes[ 1 ].currentSubscription.priceInfo.current
log(info.energy)
log(info.tax)
log(info.total)



RE: Tibber API - thomasoppida - 16.08.2019

That worked :-) Thank you again :-)


RE: Tibber API - eirik - 28.11.2019

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


RE: Tibber API - stemic01 - 07.02.2020

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.


RE: Tibber API - stemic01 - 01.03.2020

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
}
}


RE: Tibber API - stemic01 - 31.03.2020

I try to boost this thread again. Any takers?


RE: Tibber API - admin - 01.04.2020

Use this websocket client library: https://forum.logicmachine.net/showthread.php?tid=1294&pid=7823#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



RE: Tibber API - Jørn - 03.04.2020

Works great Big Grin But, how to pull data from the table to i.e. to a grp.write`?


RE: Tibber API - Erwin van der Zwart - 04.04.2020

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


RE: Tibber API - Jørn - 04.04.2020

(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



RE: Tibber API - admin - 04.04.2020

Try data.payload.data.liveMeasurement.power


RE: Tibber API - Jørn - 04.04.2020

That worked ;o) But sometimes report error attempt to index field 'payload' (a nil value) stack traceback:
why would this be`?


RE: Tibber API - Erwin van der Zwart - 05.04.2020

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