Philips Hue PowerShell
I’ve been quietly working on a little project (or two) of my own on GitHub since I got some Philips Hue lights a while back.
Philips makes accessing the bulbs programmatically very easy with the API that exists on the Bridge device but I wanted a scriptable solution to allow me to exert much more fine grained logic control over the states and colours of my lights.
Being pretty advanced with PowerShell (at least, I think I am), I set about writing a PowerShell interface (not a GUI) to allow me to access the properties and set the state of my Hue lights.
The result is a PowerShell 5 class that simplifies the interaction with Philips Hue bulbs and lights that I’ve dropped on to GitHub for use by any and all. I realise this is focussed purely on Windows users but that’s what I am and I use PowerShell extensively for other things too.
The project is called PoSHue and is located on GitHub.
It allows you to do things like this from PowerShell.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
Add-Type -AssemblyName System.Drawing # Required or you'll get a parser error! Import-Module .\PoSHue.ps1 # Assumes PoSHue.ps1 is in the same folder as your script. $Endpoint = "192.168.1.12" # IP Address of your Hue Bridge. $UserID = "38cbd1cbcac542f9c26ad393739b7" # API "key" / password / username obtained from Hue. # Instantiate the class and assign to the $Office variable $Office = [HueLight]::new("Hue go 2", $Endpoint, $UserID) # If the lamp isn't already on, turn it on. If ($Office.On -ne $true) { $Office.SwitchHueLight("on") } # Royal Blue colour in RGB format $RGB = [System.Drawing.Color]::FromArgb(63,104,224) # Define the RGB colour to convert from # Convert the RGB for a Gamut C lamp. $XYZ = $Office.RGBtoXYZ($RGB) # Convert the colour with gamma correction $XYB = $Office.xybForModel($XYZ, 'GamutC') # Get the X, Y and Brightness for a model with GamutC (Hue Go) # Set the XY value on the light. $Office.SetHueLight($XYB.b,$XYB.x,$XYB.y) # Done! |
Feel free to have a look and see how you can use it. Just 4 lines and you’re off and running.
One example is something I’m using the classes for currently but is logically quite complicated. The script executes on a schedule, that schedule is set from the previous execution and is obtained from an API call to a service providing sunset times. The script turns the lights on just before sunset but only if me and/or my fiancee are home.
I then have a second script which is executed by the “turn lights on if it’s sunset and people are home” script which monitors if we go out. If we go out, the lights are turned off by this script and, so long as it’s before 23:00, the turn lights on only if we’re home script is executed again to wait for us to come home again.
Basically, the scripts work in conjunction and cyclically to ensure the lights don’t turn on before sunset and only when we’re home and they also turn the lights off if we go out but would turn them on again if we came home before 23:00.
Let me know if you’d be interested in seeing the scripts and tasks (yes they’re scheduled tasks that monitor for return events from the scripts!) and I’ll see what I can do about packaging them up somewhere.
-Lewis
Hi Lewis,
Thanks for the work here.
I’ve been testing the PoSHue module. Everything starts well, but I got an issue with the Light class:
PS C:\Users\jsmith\Documents\Hue Project\Tests> $Bridge.GetLightNames()
Exception calling “GetLightNames” with “0” argument(s): “The given key was not present in the dictionary.”
At line:1 char:1
+ $Bridge.GetLightNames()
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : KeyNotFoundException
PS C:\Users\jsmith\Documents\Hue Project\Tests>
Any idea?
Thanks again.
—
J-M