Last active
November 9, 2023 20:24
-
-
Save trackd/7a06b4a3f63e4b8e67696d6dd368a6e4 to your computer and use it in GitHub Desktop.
example code how to format query for retrieving a month from the GraphQL api
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-TibberMonth { | |
<# | |
.DESCRIPTION | |
gets tibber usage for a month, will automatically turn any date into the first day of the month | |
and will calculate the amount of hours for that month | |
.PARAMETER Startdate | |
startdate | |
if no enddate is given it will take day 1 of the month in startdate and +1 month | |
.PARAMETER Enddate | |
enddate | |
.PARAMETER Export | |
exportpath for the json file | |
.PARAMETER Passthru | |
will return the data | |
.EXAMPLE | |
$data = Get-TibberMonth -StartDate 2023-10-10 -Export .\test.json -Passthru | |
.EXAMPLE | |
$tibber = Get-TibberMonth -StartDate 2023-03-01 -EndDate 2023-05-30 | |
#> | |
[CmdletBinding()] | |
param( | |
[Datetime] | |
$StartDate = (Get-Date).AddMonths(-1), | |
[Datetime] | |
$EndDate, | |
[String] | |
$Export, | |
[Switch] | |
$Passthru, | |
[String] | |
$Token, | |
[String] | |
$UserAgent = 'powershell' | |
) | |
Import-Module PSTibber | |
if ($Export -and (Test-Path -LiteralPath $Export)) { | |
throw "File $Export already exists" | |
} | |
$tibber = @{ | |
PersonalAccessToken = $Token | |
UserAgent = $userAgent | |
ErrorAction = 'Stop' | |
} | |
if (-Not $EndDate) { | |
# if no enddate it will just return 1 month from the first day of that month. | |
$date = Get-Date $StartDate -Day 1 -Hour 0 -Minute 0 -Second 0 | |
[int]$hours = ($date.AddMonths(1) - $date).TotalHours | |
} | |
else { | |
$date = $StartDate | |
[int]$hours = ($EndDate - $StartDate).TotalHours | |
} | |
$DateString = Get-Date -Date $date -Format s | |
$DateByte = [System.Text.Encoding]::UTF8.GetBytes($DateString) | |
$base64DateTime = [System.Convert]::ToBase64String($DateByte) | |
Write-Verbose "S: $StartDate, E: $Enddate, D: $date, DS: $datestring, H: $hours" | |
$tibber.Query = @" | |
{ | |
viewer { | |
homes { | |
consumption(resolution: HOURLY, after: `"$base64DateTime`", first: $hours) { | |
nodes { | |
from | |
cost | |
unitPrice | |
unitPriceVAT | |
consumption | |
consumptionUnit | |
currency | |
} | |
} | |
production(resolution: HOURLY, after: `"$base64DateTime`", first: $hours) { | |
nodes { | |
from | |
profit | |
unitPrice | |
unitPriceVAT | |
production | |
productionUnit | |
currency | |
} | |
} | |
} | |
} | |
} | |
"@ | |
$data = Invoke-TibberQuery @tibber | |
$nicerData = [PSCustomObject]@{ | |
Consumption = $data.viewer.homes.consumption.nodes | |
Production = $data.viewer.homes.production.nodes | |
} | |
if ($Export) { | |
$nicerData | ConvertTo-Json -Depth 10 | Set-Content -Path $Export | |
if ($Passthru) { | |
$nicerData | |
} | |
} | |
else { | |
$nicerData | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment