stefanes / PSTibber

PowerShell module for accessing the Tibber GraphQL API
https://www.powershellgallery.com/packages/PSTibber
MIT License
7 stars 0 forks source link
powershell tibber tibberlive tibberpulse

PSTibber

PowerShell module for accessing the Tibber GraphQL API: https://developer.tibber.com/docs/overview

Latest version Download count

_:heavy_checkmark: See CHANGELOG.md for what's new!

Grafana

See here for an example of this module in action...

Installation

Using the latest version of PowerShellGet:

Install-Module -Name PSTibber -Repository PSGallery -Scope CurrentUser -Force -PassThru
Import-Module -Name PSTibber -Force -PassThru

Or if you already have the module installed, update to the latest version:

Update-Module -Name PSTibber
Import-Module -Name PSTibber -Force -PassThru

Authentication

Note

To access the API you first must have a Tibber account (you can sign up for Tibber using the invitation code gqpkcwrn to get a €50/500 kr bonus for use in the Tibber Store).

A Personal Access Token can be generated here: https://developer.tibber.com/settings/access-token

To authenticate, pass the generated access token using the -PersonalAccessToken parameter with each call or set the TIBBER_ACCESS_TOKEN environment variable:

$env:TIBBER_ACCESS_TOKEN = "<your access token>"

User agent

Note

Please set a user agent so Tibber can better track different client implementations.

To set a user agent, pass the string using the -UserAgent parameter with each call or set the TIBBER_USER_AGENT environment variable:

$env:TIBBER_USER_AGENT = 'stefanes.tibber-pulse/0.1.0'

Usage

_:heavy_checkmark: See here for how to use this module with your Tibber Pulse/Watty.

Use Get-Command -Module PSTibber for a list of functions provided by this module. See the help associated with each function using the Get-Help command, e.g. Get-Help Get-TibberUser -Detailed, and the documentation available in docs for more details:

If there is no function available for what you are trying to do, you can always use the Invoke-TibberQuery function with a valid GraphQL query:

$query = @"
{
  viewer {
    homes {
      id
      consumption(resolution: HOURLY, last: 1) {
        nodes {
          to
          consumption
          cost
        }
      }
      currentSubscription {
        priceInfo {
          tomorrow {
            startsAt
            total
            level
          }
        }
      }
    }
  }
}
"@
$response = Invoke-TibberQuery -Query $query
$response.viewer.homes[0]

Note: You can construct your GraphQL queries using the Tibber API explorer.

Examples

Get logged-in user details

$response = Get-TibberUser
Write-Host "$($response.name) <$($response.login)> with user Id $($response.userId)"

Get home Id

$response = Get-TibberHome
($response | Where-Object { $_.appNickname -eq 'Vitahuset' }).id | Tee-Object -Variable homeId

Check if your home has a Tibber Pulse or Watty registered

$response = Get-TibberHome -IncludeFeatures -Id $homeId
Write-Host "Your home, $($response.appNickname), has real-time consumption $(
    if ([bool]::Parse($response.features.realTimeConsumptionEnabled)) {
        'enabled!'
    }
    else {
        'disabled...'
    }
    )"

Get the size of your main fuse

(Get-TibberHome -Id $homeId).mainFuseSize

Get time of maximum energy price

$response = Get-TibberPriceInfo -Last 10
$maxPrice = $response | Sort-Object -Property total -Descending | Select-Object -First 1
Write-Host "Max energy price, $($maxPrice.total) $($maxPrice.currency), starting at $(([DateTime]$maxPrice.startsAt).ToString('yyyy-MM-dd HH:mm')) [$($maxPrice.level)]"

Get today's and tomorrow's energy prices

$response = Get-TibberPriceInfo -IncludeToday -IncludeTomorrow
Write-Host "Today's and tomorrow's energy prices: $($response | Out-String)"

Get time of maximum power consumption

$response = Get-TibberConsumption -Last 10
$maxCons = $response | Sort-Object -Property consumption -Descending | Select-Object -First 1
Write-Host "Max power consumption $($maxCons.cost) $($maxCons.currency) ($($maxCons.consumption) $($maxCons.consumptionUnit) at $($maxCons.unitPrice)): $(([DateTime]$maxCons.from).ToString('HH:mm')) - $(([DateTime]$maxCons.to).ToString('HH:mm on yyyy-MM-dd'))"

Get time of maximum power production

$response = Get-TibberProduction -Last 10
$maxProd = $response | Sort-Object -Property production -Descending | Select-Object -First 1
Write-Host "Max power production $($maxProd.profit) $($maxProd.currency) ($($maxProd.production) $($maxProd.productionUnit) at $($maxProd.unitPrice)): $(([DateTime]$maxProd.from).ToString('HH:mm')) - $(([DateTime]$maxProd.to).ToString('HH:mm on yyyy-MM-dd'))"

Send push notifications

$response = Send-PushNotification -Title 'Hello' -Message 'World!' -ScreenToOpen CONSUMPTION
Write-Host "Sent push notification to $($response.pushedToNumberOfDevices) device(s)"

The response cache

The response from all Get functions are cached by default, this means that the next time you call the same function with the same parameters the result stored in the cache will be returned with no new request sent to the GraphQL endpoint. Use -Force to force a refresh of any cached results:

PS> Get-TibberUser -Verbose
VERBOSE: Invoking web request: POST https://api.tibber.com/v1-beta/gql [User agent = PSTibber/0.6.1 stefanes.tibber-pulse/0.1.0]
VERBOSE: POST with 68-byte payload
VERBOSE: received 178-byte response of content type application/json

User Id                              User
-------                              ----
dcc2355e-6f55-45c2-beb9-274241fe450c Arya Stark <arya@winterfell.com>

PS> Get-TibberUser -Verbose
VERBOSE: From cache: b0c06ad71d17d320d1bbada3a930f6e6174e222b

User Id                              User
-------                              ----
dcc2355e-6f55-45c2-beb9-274241fe450c Arya Stark <arya@winterfell.com>

PS> Get-TibberUser -Force -Verbose
VERBOSE: Invoking web request: POST https://api.tibber.com/v1-beta/gql [User agent = PSTibber/0.6.1 stefanes.tibber-pulse/0.1.0]
VERBOSE: POST with 68-byte payload
VERBOSE: received 178-byte response of content type application/json

User Id                              User
-------                              ----
dcc2355e-6f55-45c2-beb9-274241fe450c Arya Stark <arya@winterfell.com>

Note: The entire cache can be cleared by re-importing the module, Import-Module -Name PSTibber -Force -PassThru.

Debugging

To view the GraphQL query sent in the requests, add the -Debug switch to the command. To also include the response, add the -DebugResponse switch.

Example:

PS> Get-TibberUser -Debug -DebugResponse
DEBUG: GraphQL query: { "query": "{ viewer{ login,userId,name,accountType,websocketSubscriptionUrl,__typename }}" }
DEBUG: Response: 200 OK
DEBUG: Response content: {"data":{"viewer":{"login":"arya@winterfell.com","userId":"dcc2355e-6f55-45c2-beb9-274241fe450c","name":"Arya Stark","accountType":["tibber","customer"],"websocketSubscriptionUrl":"wss://websocket-api.tibber.com/v1-beta/gql/subscriptions","__typename":"Viewer"}}}

DEBUG: Cache entry [b0c06ad71d17d320d1bbada3a930f6e6174e222b]: @{viewer=}

User Id                              User
-------                              ----
dcc2355e-6f55-45c2-beb9-274241fe450c Arya Stark <arya@winterfell.com>

Tibber Pulse/Watty (live measurement data)

The live measurement data, generated by e.g. Tibber Pulse or Watty, is served as a GraphQL subscription. See here for details.