AirThings notifications in Teams with PowerShell

I recently got an AirThings View Plus environment sensor. When I discovered it had an API I got happy. This meant that I could play around with it in PowerShell. I work from home, and during winter I use a fireplace to keep the house warm. Something I now could do is to send a notification to Teams when the temperature drops and I need to put more logs into the fireplace, or when co2 is too high and I need to open the windows to get fresh air. It’s awesome to see a manufacturer make it this easy for consumers to register access to the API. It’s even well documented with API request examples. I have a washer/dryer, that has an API, but it’s closed for consumers. Way to go AirThings! 🙂

Go to https://dashboard.airthings.com/integrations/api-integration.

Click “Request API Client”.

Give it a name, select the Resource Scope, and put Enable to ON. Click Save.

Copy the ID, display the secret, and copy it.

Go to https://dashboard.airthings.com/devices.
Copy your device’s ID.

In the Teams client go to the Connectors menu for the channel you want the notifications in.

Click Add on the “Incoming Webhook”.

Click Add one more time.

Go to the Connector menu again, and click Configure.

Give it a name and click Create.

Copy the URL and click Done.

Close the Connector window, and go to your Channel.
You now can see an Incoming Webhook has been added.


Copy this script, paste your details and run it in PowerShell.

$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
}

Here is an example of how it will look in Teams.

This is a short proof of concept to show you what’s possible with the API for AirThings. The logic in the script probably could have been better, but it’s only to test out that I’m able to get data from the API and send it somewhere with PowerShell. You could for example also setup Azure Automation and set it to run on a schedule.

3 thoughts on “AirThings notifications in Teams with PowerShell

  1. I tried setting up these Airthings notifications following your guide, but running the powershell script just gives me this error:
    Line |
    95 | Invoke-RestMethod -Method POST -Body $body -Uri $WebHookURL
    | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    | Bad payload received by generic incoming webhook.

    Any idea what I might be doing wrong?
    I have of course filled in clientid, clientsecret, deviceid and webhook url before running the script.

    Like

Leave a comment