Turn On/Off Lights Based On PC Power State

I bought the Philips Hue Bridge with Light Bars to make a cool lighting effect behind my PC monitors. You could easily control them via the Android App, and they look really cool.

The problem is that when I am on my PC, I must manually turn them on/off via the App. I wanted an automated system that handles these requests automatically. This means that whenever I turn on/wake my PC, they light up automatically. And, whenever I put my computer to sleep/shutdown, they turn off automatically.

Yes, I am a bit lazy….

Fortunately, the Hue system comes with an API which can be controlled easily through web requests. Tutorial can be found here: https://developers.meethue.com/develop/get-started-2/

With this API you can easily turn on/off the lights using code. So first, I created 2 PowerShell scripts (TurnOn/TurnOff). One script was responsible to turn on the lights and the other one to turn off the lights.

Using the Windows Task Scheduler, you can easily call these scripts automatically in various events like startup, user login, system locked, and system unlocked.

Windows Task Scheduler

The problem is that the shutdown event does not exist in the Task Scheduler, but one could call the TurnOff script from the Local Group Policy Editor.

Group Policy

Unfortunately, when I tried this technique, it failed to work for most of my tests. I even configured the group policy to run PowerShell scripts first at user Shutdown but all in vain.

Therefore, I had to come with a better technique.

Fortunately, using the API one could easily create schedules. This means that the Hue Bridge can run automatically commands at a specified time. These schedules can be even deleted which makes it perfect for us. Why? Here is my logic:

  1. On Windows Login turn on the lights
  2. Create an infinite loop:
    1. Create a schedule to turn off the lights automatically in the next 10 seconds
    2. Wait for 5 seconds. If the script is still running, delete the just created schedule and repeat from step a.
    3. If the script is not running (PC is turned off) – the schedule is not cancelled and therefore the schedule turns off both lights.

It works perfectly.

PC Waking Up - Lights Turning On Automatically
PC Turning Off - Lights Turning off Automatically

Here is the script to make this work. This script must be called using the Windows Task Scheduler. I used the below settings:

# Hue Bridge
$hueBridge = "http://192.168.1.139/api"

# Username
$username = ''

function Switch-Lights($TurnOn){
  if($TurnOn -eq 0){
    #Command to Turn Off
    $apicontent= '{"on" : false}'
  }
  else {
    $apicontent= '{"on" : true}'
  }

  #Invoke commands
  Invoke-WebRequest -Method put -Uri "$($hueBridge)/$($username)/groups/1/action" -Body $apicontent
}
function Add-FutureEvent {
  #calculate future localtime
  $future_date = (Get-Date).AddSeconds(10)
  $date_format = $future_date.ToString("yyyy/MM/dd")
  $time_format =  $future_date.ToString("HH:mm:ss")
  $future_localtime = "$($date_format)T$($time_format)"

  #Command to Turn off
  $apicontent= 
  '{  "name": "PC Auto Manager",
      "command": {
          "address": "/api/'+$username+'/groups/1/action",
          "method": "PUT",
          "body": {
              "on": false
          }
      },
      "localtime": "'+$future_localtime+'"                      
  }'

  #Invoke commands
  $response  = Invoke-RestMethod -Method post -Uri "$($hueBridge)/$($username)/schedules" -Body $apicontent
  #get id from json response 
  return  $response.success.id
}

function Remove-Event($event_id){
  #Invoke commands
  Invoke-WebRequest -Method delete -Uri "$($hueBridge)/$($username)/schedules/$($event_id)" 
}


while(1){
  Switch-Lights -TurnOn 1
  $event_id = Add-FutureEvent
  Start-Sleep -s 5 #in seconds
  Remove-Event -event_id $event_id
}


Happy Coding 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s