tibber-api
Node.js module for querying data and integrating with Tibber Pulse through Tibber API.
General
This Node.js module is used for communication with Tibber API through GraphQL queries and for retrieving data from Tibber Pulse via websocket.
Tibber is a norwegian technology focused power company which is providing tools to get more insight and controll over your home and its power consumption.
Note
Breaking changes!
Version 5 introduces breaking changes in TibberFeed. Due to the fact that Tibber also has introduced breaking changes in their API we had to do some small changes.
Instead of changing the whole implementation, the only noticable change is done to the constructor of TibberFeed
which takes a TibberQuery
instead of and IConfig
object and uses the config from the provided TibberQuery
. See example below.
...
const tibberQuery = new TibberQuery(config);
const tibberFeed = new TibberFeed(tibberQuery, 5000);
...
Prerequisites
Click the link below to sign up, and receive 400 NOK to shop smart home gadgets from Tibber Store:
https://invite.tibber.com/fjafad7b
You will also need an API token from Tibber. Get it here:
https://developer.tibber.com/
Installation
NPM package
https://www.npmjs.com/package/tibber-api
Command line
npm install tibber-api
TibberFeed
Realtime power consuption data from Tibber Pulse. Provide API token, Home ID and select what kind of information you want to retrieve.
Note! There should be only one instance running of TibberFeed per API key. Doing otherwise may return unpredictable result, or even error responses from the API.
JSDoc documentation
Constructor
TibberFeed(tibberQuery: TibberQueryBase, timeout: number = 60000, returnAllFields = false, connectionTimeout: number = 30000)
Created a new instance of TibberFeed with with an instance of TibberQuery and timeout. The timeout is used for reconnection when no data is received within the specified time. The config object is described later in this document.
Methods
Connect
Connect();
Connect to Tibber Pulse realtime data feed. The connect method has a backoff logic with jitter applied to prevent too many reconnection attemts. There is also an idle timeout functionality that ensures that the feed is always connected when it's active.
Close
Close();
Disconnect from Tibber Pulse data feed.
Events
connected
connected(message: string)
Called when the feed is connected to Tibber.
connection_ack
connection_ack(message: string)
Called when the feed is authenticated.
disconnected
disconnected(message: string)
Called when the feed is disconnected from Tibber.
data
data(data: any)
Called when new data is available.
error
error(error: any)
Called when the feed is logging errors.
warn
warn(message: string)
Called when the feed is logging warnings.
log
log(message: string)
Called when the feed is logging.
TibberQuery
Do basic calls to Tibber API using GraphQL queries. To query the Tibber API, simply provide raw GraphQL queries in the payload of the incoming message. See Tibber API documentation and API Explorer for more informations.
JSDoc documentation
Constructor
TibberQuery(config: IConfig, requestTimeout: number = 30000);
Created a new instance of TibberQuery with the desired configuration. The config object is described later in this document.
Methods
query
query(query: string, variables?: object): Promise<any>
Query Tibber API with GraphQL to retrieve data. See Tibber documentation for more information on QraphQL.
getHome
getHome(homeId: string): Promise<IHome>
Get home registered to your Tibber account from home ID. This function will return a IHome object including general information. To retrieve complete IHome object, please use the getHomeComplete(homeId: string) function.
getHomeComplete
getHomeComplete(homeId: string): Promise<IHome>
Get home registered to your Tibber account from home ID. This function will return a home object including all information.
getHomes
getHomes(): Promise<IHome[]>
Get all homes registered to your Tibber account. This function will return a list of homes including general information. To retrieve complete Home objects, please use the getHomesComplete() function.
getHomesComplete
getHomesComplete(): Promise<IHome[]>
Get all homes registered to your Tibber account. This function will return a list of homes including all information.
getCurrentEnergyPrice
getCurrentEnergyPrice(homeId: string): Promise<IPrice>
Get the current energy price for selected home.
getCurrentEnergyPrices
getCurrentEnergyPrices(): Promise<IHome[]>
Get current energy prices for all your homes.
getTodaysEnergyPrices
getTodaysEnergyPrices(homeId: string): Promise<IPrice[]>
Get NorPool energy prices for today for selected home.
getTomorrowsEnergyPrices
getTomorrowsEnergyPrices(homeId: string): Promise<IPrice[]>
Get NorPool energy prices for tomorrow for selected home. This will only return data between 12:00 and 23:59
getConsumption
getConsumption(resolution: EnergyResolution, lastCount: number, homeId?: string): Promise<IConsumption[]>
Get consumption for selected home, or all homes if homeId is not provided. EnergyResolution describes interval for data and lastCount specifies number of records to retrieve, returning the last number of records in the dataset.
sendPushNotification
Send a notification to the tibber app. You can specify which route shoule be opened in the App when opening the message. The notification will be send to all devices registered for that tibber account. If the send is successful the response will show how many devices the message reached.
Examples
TibberFeed: typescript example.
import { TibberFeed, TibberQuery, TibberQueryBase, IConfig } from 'tibber-api';
const config: IConfig = {
apiEndpoint: {
apiKey: '3A77EECF61BD445F47241A5A36202185C35AF3AF58609E19B53F3A8872AD7BE1-1',
queryUrl: 'https://api.tibber.com/v1-beta/gql',
requestTimeout: 5000,
},
homeId: '96a14971-525a-4420-aae9-e5aedaa129ff',
timestamp: true,
power: true,
};
const tibberQuery = new TibberQuery(config);
const tibberFeed = new TibberFeed(tibberQuery, 5000);
tibberFeed.on('data', (data) => {
console.log(data);
});
tibberFeed.connect();
TibberQuery: typescript example.
import { TibberQuery, IConfig } from 'tibber-api';
import http from 'http';
const hostname = '127.0.0.1';
const port = 3000;
const config: IConfig = {
apiEndpoint: {
apiKey: '3A77EECF61BD445F47241A5A36202185C35AF3AF58609E19B53F3A8872AD7BE1-1',
queryUrl: 'https://api.tibber.com/v1-beta/gql',
requestTimeout: 5000,
},
};
const queryHomes = '{viewer{homes{id size appNickname appAvatar address{address1 address2 address3 postalCode city country latitude longitude}}}}';
const tibberQuery = new TibberQuery(config);
const server = http.createServer(async (req, res) => {
const result = await tibberQuery.query(queryHomes);
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(result));
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
Config object
IConfig: typescript example.
const config: IConfig = {
apiEndpoint: {
apiKey: '3A77EECF61BD445F47241A5A36202185C35AF3AF58609E19B53F3A8872AD7BE1-1',
queryUrl: 'https://api.tibber.com/v1-beta/gql',
requestTimeout: 5000,
},
homeId: '96a14971-525a-4420-aae9-e5aedaa129ff',
timestamp: true,
power: true,
lastMeterConsumption: true,
accumulatedConsumption: true,
accumulatedProduction: true,
accumulatedCost: true,
accumulatedReward: true,
currency: true,
minPower: true,
averagePower: true,
maxPower: true,
powerProduction: true,
minPowerProduction: true,
maxPowerProduction: true,
lastMeterProduction: true,
powerFactor: true,
voltagePhase1: true,
voltagePhase2: true,
voltagePhase3: true,
currentL1: true,
currentL2: true,
currentL3: true,
signalStrength: true,
};
License
MIT