Last year I bought a new Lawn Mower, a Husqvarna 435x AWD. The best thing, besides he is doing an awesome job in our Garden, there is an API to include the Mower in our Home Automation.
That fact brought me to an Idea, can I control my Lawn Mower with PowerShell? Yes, we can!
So, this Article will describe, how you can controll your Husqvarna Automower, with Automower Connect Modul, with PowerShell.
Create the API and get your API Key
Naviagte to https://developer.husqvarnagroup.cloud/ and Login with your Automower Connect Account
Create a New Application
After you Application is created, connect a new API
Please connect 2 API’s
- Authentication API
- Automower Connect API
The PowerShell Script
So, now it is time for the fun Part.
Let’s declare our Variables and Authenticate with our Secret and Username/Password. Please fill in your APIKey, APISecret, Username and Password
#region Variables
$APIKey = "00000000000000000000000000"
$APISecret = "00000000000000000000000000"
$Username = "your Mail"
$Password = "your Password"
$OAuthURL = "https://api.authentication.husqvarnagroup.dev/v1/oauth2/token"
#endregion Variables
#region OAuthToken
$Body = @{
'username' = $Username
'client_id' = $APIKey
'grant_type' = 'password'
'password' = $Password
}
$params = @{
ContentType = 'application/x-www-form-urlencoded'
Headers = @{'accept' = 'application/json' }
Body = $Body
Method = 'Post'
URI = $OAuthURL
}
$token = Invoke-RestMethod @params
#endregion OAuthToken
Now we received the Token to talk to the API, first let’s get some Information’s from our Lawn Mower.
To talk to the API and to Start or Stop our Husqvarna, we need the Mover ID
#region GetMowerID
$Uri = 'https://api.amc.husqvarna.dev/v1/mowers'
$params2 = @{
Headers = @{
'accept' = "application/vnd.api+json"
'authorization' = "Bearer $($token.access_token)"
'X-Api-Key' = "$Apikey"
'Authorization-Provider' = 'husqvarna'
}
Method = 'Get'
URI = $Uri
}
$Result = Invoke-RestMethod @params2
$MowerID=$Result.data.id
#endregion GetMowerID
I have written a Function and some standard command to stop and start the Mower. Feel free to adopt
Function Send-MowerCommand {
Param(
[ValidateSet("Start", "Pause", "Park", "ResumeSchedule","ParkUntilNextSchedule","ParkUntilFurtherNotice")]
[parameter(Mandatory = $true)]
[String]
$Command,
[parameter(Mandatory = $false)]
[int]
$Duration,
[parameter(Mandatory = $true)]
[string]
$MowerID
)
$Uri = "https://api.amc.husqvarna.dev/v1/mowers/$MowerID/actions"
$params = @{
Headers = @{
'accept' = "*/*"
'authorization' = "Bearer $($token.access_token)"
'X-Api-Key' = "$Apikey"
'Authorization-Provider' = 'husqvarna'
'Content-Type' = 'application/vnd.api+json'
}
Method = 'Post'
URI = $Uri
}
if ($Command -eq "Pause") {
$Body = @{
'data' = @{
'type' = 'Pause'
}
} | ConvertTo-Json
}
if ($Command -eq "ResumeSchedule") {
$Body = @{
'data' = @{
'type' = 'ResumeSchedule'
}
} | ConvertTo-Json
}
if ($Command -eq "Start") {
$Body = @{
'data' = @{
'type' = 'Start'
'attributes' = @{
'duration' = $Duration
}
}
} | ConvertTo-Json
}
if ($Command -eq "Park") {
$Body = @{
'data' = @{
'type' = 'Park'
'attributes' = @{
'duration' = $Duration
}
}
} | ConvertTo-Json
}
if ($Command -eq "ParkUntilNextSchedule") {
$Body = @{
'data' = @{
'type' = 'ParkUntilNextSchedule'
}
} | ConvertTo-Json
}
if ($Command -eq "ParkUntilFurtherNotice") {
$Body = @{
'data' = @{
'type' = 'ParkUntilFurtherNotice'
}
} | ConvertTo-Json
}
Invoke-RestMethod @params -body $Body
}
# Pause Mower
Send-MowerCommand -Command Pause -MowerID $MowerID
# Staert Mower with Schedule
Send-MowerCommand -Command ResumeSchedule -MowerID $MowerID
#Start Mower for 10 Minutes, outside Shedule
Send-MowerCommand -Command Start -MowerID $MowerID -Duration 10
#Park Mower for 10 Minutes
Send-MowerCommand -Command Park -MowerID $MowerID -Duration 10
I have published the Script on my GitHub Repository. Feel Free to enhances and contribute.
Michael Seidl aka Techguy