Skip to content

Instantly share code, notes, and snippets.

@peterhellberg
Created January 5, 2022 19:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterhellberg/f0a66c6c8937674cfe09b37359eb7723 to your computer and use it in GitHub Desktop.
Save peterhellberg/f0a66c6c8937674cfe09b37359eb7723 to your computer and use it in GitHub Desktop.
Tibber GraphQL Subscription from Go client
package main
import (
"encoding/json"
"flag"
"log"
"os"
"github.com/hasura/go-graphql-client"
)
func run(token, endpoint, homeID string) error {
params := map[string]interface{}{
"token": token,
}
variables := map[string]interface{}{
"homeId": homeID,
}
logger := log.New(os.Stdout, "", 0)
errLogger := log.New(os.Stderr, "", 0)
onErrorFunc := func(sc *graphql.SubscriptionClient, err error) error {
errLogger.Print("ERR", err)
return err
}
client := graphql.NewSubscriptionClient(endpoint).
WithConnectionParams(params).
WithoutLogTypes(graphql.GQL_DATA, graphql.GQL_CONNECTION_KEEP_ALIVE).
WithLog(logger.Println).
OnError(onErrorFunc)
defer client.Close()
var sub struct {
LiveMeasurement struct {
Timestamp graphql.String
Power graphql.Float
LastMeterConsumption graphql.Float
AccumulatedConsumption graphql.Float
AccumulatedProduction graphql.Float
AccumulatedConsumptionLastHour graphql.Float
AccumulatedProductionLastHour graphql.Float
AccumulatedCost graphql.Float
AccumulatedReward graphql.Float
currency graphql.String
MinPower graphql.Float
AveragePower graphql.Float
MaxPower graphql.Float
PowerProduction graphql.Float
PowerReactive graphql.Float
PowerProductionReactive graphql.Float
MinPowerProduction graphql.Float
MaxPowerProduction graphql.Float
LastMeterProduction graphql.Float
PowerFactor graphql.Float
VoltagePhase1 graphql.Float
VoltagePhase2 graphql.Float
VoltagePhase3 graphql.Float
CurrentL1 graphql.Float
CurrentL2 graphql.Float
CurrentL3 graphql.Float
SignalStrength graphql.Int
} `graphql:"liveMeasurement(homeId: $homeId)"`
}
_, err := client.Subscribe(sub, variables, func(data *json.RawMessage, err error) error {
if err != nil {
return err
}
// Do something with the data, here I'm just logging it
logger.Println(string(*data))
return nil
})
if err != nil {
return err
}
return client.Run()
}
func main() {
var token, endpoint, homeID string
flag.StringVar(&token, "token", "476c477d8a039529478ebd690d35ddd80e3308ffc49b59c65b142321aee963a4", "API token")
flag.StringVar(&endpoint, "endpoint", "wss://api.tibber.com/v1-beta/gql/subscriptions", "GraphQL Subscriptions endpoint")
flag.StringVar(&homeID, "homeID", "cc83e83e-8cbf-4595-9bf7-c3cf192f7d9c", "Home ID")
flag.Parse()
if err := run(token, endpoint, homeID); err != nil {
log.Fatal(err)
}
}
@peterhellberg
Copy link
Author

{"id":"64b0dd2d-6add-47d2-b8f0-71ef38fec821","type":"start","payload":{"query":"subscription ($homeId:ID!){liveMeasurement(homeId: $homeId){timestamp,power,lastMeterConsumption,accumulatedConsumption,accumulatedProduction,accumulatedConsumptionLastHour,accumulatedProductionLastHour,accumulatedCost,accumulatedReward,currency,minPower,averagePower,maxPower,powerProduction,powerReactive,powerProductionReactive,minPowerProduction,maxPowerProduction,lastMeterProduction,powerFactor,voltagePhase1,voltagePhase2,voltagePhase3,currentL1,currentL2,currentL3,signalStrength}}","variables":{"homeId":"11995374-1a29-0000-0000-e512cb1607a6"}}}
{"type":"connection_ack"}
{"liveMeasurement":{"timestamp":"2022-01-05T20:40:47.000+01:00","power":379,"lastMeterConsumption":2153.761,"accumulatedConsumption":7.232,"accumulatedProduction":0,"accumulatedConsumptionLastHour":0.266,"accumulatedProductionLastHour":0,"accumulatedCost":11.301084,"accumulatedReward":null,"currency":"SEK","minPower":127,"averagePower":349.7,"maxPower":2463,"powerProduction":0,"powerReactive":0,"powerProductionReactive":197,"minPowerProduction":0,"maxPowerProduction":0,"lastMeterProduction":0.001,"powerFactor":0.887,"voltagePhase1":232.9,"voltagePhase2":234.4,"voltagePhase3":235.6,"currentL1":1.6,"currentL2":0.4,"currentL3":0,"signalStrength":-75}}
{"liveMeasurement":{"timestamp":"2022-01-05T20:40:57.000+01:00","power":385,"lastMeterConsumption":2153.762,"accumulatedConsumption":7.233,"accumulatedProduction":0,"accumulatedConsumptionLastHour":0.267,"accumulatedProductionLastHour":0,"accumulatedCost":11.302656,"accumulatedReward":null,"currency":"SEK","minPower":127,"averagePower":349.7,"maxPower":2463,"powerProduction":0,"powerReactive":0,"powerProductionReactive":197,"minPowerProduction":0,"maxPowerProduction":0,"lastMeterProduction":0.001,"powerFactor":0.89,"voltagePhase1":233.1,"voltagePhase2":234.5,"voltagePhase3":235.7,"currentL1":1.6,"currentL2":0.4,"currentL3":0,"signalStrength":null}}

@peterhellberg
Copy link
Author

Default flags are demo values copied from https://developer.tibber.com/explorer

You can find your own token at https://developer.tibber.com/settings/accesstoken

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment