Skip to content

Instantly share code, notes, and snippets.

@AlexanderHolmeset
Last active May 9, 2022 06:38
Show Gist options
  • Save AlexanderHolmeset/5e53340d9d51332f0c46e39edb115480 to your computer and use it in GitHub Desktop.
Save AlexanderHolmeset/5e53340d9d51332f0c46e39edb115480 to your computer and use it in GitHub Desktop.
$ClientID = "1111111111"
$ClientSecret = "11111111"
$DeviceID = "111111111"
$WebHookURL = "https://contoso.com"
$formbody = "grant_type=client_credentials&scope=read%3Adevice%3Acurrent_values&client_id=$ClientID&client_secret=$ClientSecret"
$uri = "https://accounts-api.airthings.com/v1/token"
$Token = (Invoke-RestMethod -Method Post -Uri $uri -Body $formbody -ContentType "application/x-www-form-urlencoded" -UseBasicParsing).access_token
$header = @{
"Authorization" = "Bearer $token"
}
$data = (Invoke-RestMethod -Method Get -Uri "https://ext-api.airthings.com/v1/devices/$deviceid/latest-samples" -Headers $header -ContentType "application/json" -UseBasicParsing).data
if ($data.battery -lt 10) {
$messages += [PSCustomObject]"Battery is low: $($data.battery)%"
}
if ($data.humidity -gt 30) {
$messages += [PSCustomObject]"Humidity is to high: $($data.humidity)%"
}
if ($data.humidity -lt 20) {
$messages += [PSCustomObject]"Humidity is to low: $($data.humidity)%"
}
if ($data.pm1 -gt 100) {
$messages += [PSCustomObject]"pm1 is to high: $($data.pm1)"
}
if ($data.pm25 -gt 100) {
$messages += [PSCustomObject]"pm25 is to high: $($data.pm25)"
}
if ($data.pressure -gt 1200) {
$messages += [PSCustomObject]"Pressure is to high: $($data.pressure)"
}
if ($data.radonShortTermAvg -gt 100) {
$messages += [PSCustomObject]"Radon Short Term Avg is to high: $($data.radonShortTermAvg)"
}
if ($data.temp -gt 22) {
$messages += [PSCustomObject]"Temperature is to high: $($data.temp)"
}
if ($data.temp -lt 20) {
$messages += [PSCustomObject]"Temperature is to low: $($data.temp)"
}
if ($data.voc -gt 200) {
$messages += [PSCustomObject]"VOC is to high: $($data.voc)"
}
foreach($message in $messages){
$body = @"
{
"type":"message",
"attachments":[
{
"contentType":"application/vnd.microsoft.card.adaptive",
"contentUrl":null,
"content":{
"`$schema":"http://adaptivecards.io/schemas/adaptive-card.json",
"type":"AdaptiveCard",
"version":"1.2",
"body":[
{
"type": "TextBlock",
"text": "$message"
}
]
}
}
]
}
"@
Invoke-RestMethod -Method POST -Body $body -Uri $WebHookURL
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment