Use PowerShell to change the color of your Govee lamp

I recently bought a Govee lamp for my desk, which I can control using my Google Home Assistant. But… It also has API support. 🙂 In this blog post, I will show you how to control it using PowerShell in a Function to turn it Red, Orange, or Green so you can use it for a visual outcome of your PowerShell script.

How to enable API support for the Govee lamp

In the Govee app, you can apply for the API key in your Profile settings. When you select Apply for API key, you can fill in this screen and accept the Term of Service:

After filling out the details and selecting Submit, you will receive an email from Govee with your API key and a PDF with details and examples of how to use it. This PDF can be viewed here also.

When you have the API key, you can test the connection by running the following: (Replace xxxx with your API key)

$url = "https://developer-api.govee.com/v1/devices"
$token = "xxxx"
$headers = @{
    "Govee-API-Key" = "$($token)"
}

(Invoke-WebRequest -Uri $url -Method Get -Headers $headers | ConvertFrom-Json).data.devices

This will output all of your Govee devices like this: (I replaced my device address and model with xxxx)

device       : xxxx
model        : xxxx
deviceName   : Aura Table Lamp
controllable : True
properties   : @{colorTem=}
retrievable  : True
supportCmds  : {turn, brightness, color, colorTem}

Note: There is an API rate limit of 10 per minute per device.

What does the script do?

The Function can turn the lamp Red, Orange, or Green. You can use it in scripts to let the outcome determine the lamp’s color. Easy for long-running PowerShell scripts so that you know when it’s ready. But that’s just an example of the API capabilities. Of course, you can use it in other ways, such as Home Automation.

How does it work?

The Function will let you select Red, Orange, or Green and send the RGB color to the Govee lamp using the API key with the device (Mac) and model. It will display whether it succeeded or if it failed based on HTTP Status Code 200.

You can copy/paste the Function in your PowerShell script and use it to display color for Error (Red), Warning (Orange), and Success (Green)

Set-GoveeColor -Color Green

When everything is okay, the output on the screen will be:

Succesfully set color to Green

You can see the light change in the room and the app:

The script

Below are the contents of the script. I removed the API and device key/model and replaced them with xxxx.

function Set-GoveeColor {
    param( 
        [parameter(Mandatory = $true)][ValidateSet("Red", "Orange", "Green")]$Color
    )

    switch ($color) {
        "Red" {
            $rgb = @{
                "r" = 255
                "g" = 0
                "b" = 0
            }
        }
        "Orange" {
            $rgb = @{
                "r" = 255
                "g" = 165
                "b" = 0
            }
        }
        "Green" {
            $rgb = @{
                "r" = 0
                "g" = 255
                "b" = 0
            }
        }
    }

    $url = "https://developer-api.govee.com/v1/devices/control"
    $token = "xxxx"
    $headers = @{
        "Content-Type"  = "application/json"
        "Govee-API-Key" = "$($token)"
    }
    $body = @{
        "device" = "xxxx"
        "model"  = "xxxx"
        "cmd"    = @{
            "name"  = "color"
            "value" = $rgb
        } 
    } | ConvertTo-Json

    $request = Invoke-WebRequest -Uri $url -Method Put -Headers $headers -Body $body
    if ($request.StatusCode -eq 200) {
        Write-Host ("Succesfully set color to {0}" -f $color) -ForegroundColor Green
    }
    else {
        Write-Warning ("Error setting color to {0}" -f $color)
    }
}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.