Elgato Stream Deck & Philips Hue Integration

Elgato Stream Deck & Philips Hue Integration

Elgato Stream Deck So you recently picked up (or are considering picking up) the Elgato Stream Deck and you now want to tie in your Philips Hue smart lighting system so you can turn some lights on or off, change colors or brightness, and maybe even setup a goofy Red Alert button for when shit really hits the fan. The Stream Deck, targeted primarily at YouTubers, Twitchers, and other streamers, doesn't have a released SDK (yet?) and doesn't offer any out-of-the-box integration with Hue or SmartThings or any other intermediaries that might help us out. Our only option is to hook onto one of the "System" button types and chain that to something that can talk to Hue. Specifically we're going to use "Open" button type to launch a .bat (Batch) file. This will allow us to write a simple script that is fired when we press a Stream Deck button. To accomplish this we will install a command-line interface (cli) for Hue built on node.js.

UPDATED 6/21: After latest update, the Stream Deck is launching batch files in a large command window. Fortunately nircmd (www.nirsoft.net/utils/nircmd.html) can be used to launch batch files silently (as well as play sounds and a bunch of other handy things). The commands are updated to reflect this change.

Step 1.) Install node.js & hue-cli

The cli we will use is written in javascript and runs on node.js, so to start we'll need to install node.js, if you haven't installed it already for another endeavor. Download the latest "recommended for most users" release of node.js here: https://nodejs.org/ and follow their instructions to install. The installation will include the Node.js runtime, the npm package manager and will add the node executable to your PATH, all of which we will make use of.

Once node.js is installed, we'll install the hue-cli. We'll use npm, the package manager that installed alongside node.js, which makes this task quite simple. To install hue-cli open a command window and type: npm install -g hue-cli
That's it, npm will download the hue-cli package and install it. You can validate this once it completes by typing hue help at the command line, if hue-cli is successfully installed you should be presented w/ a Usage guide for hue-cli along w/ a number of examples, commands and options.

To pair your hue-cli install w/ your Hue Bridge, we'll need to first search for the bridge from the command line, to do this enter: hue search at the command line. This should return the IP address of your Hue Bridge on your local network, an address like something like:
1 stations found 1: 10.0.1.218

Note the address returned (in this example it is 10.0.1.218, yours will be different). Enter the command to begin the pairing process: hue -H 10.0.1.218 register (substitute 10.0.1.218 with your IP from the search step). You will then have press the button on your Hue Bridge to complete the pairing process. If this is successful you should then see: Hue Base Station paired!

Step 2.) Command Line interactions and Scripting

Now that we have hue-cli installed, running, and paired we are ready to interact with some lights and create some batch files to script out our lighting commands. hue-cli uses simple numbers to identify each of your Hue lights, so first we need to determine the id numbers of the lights we want to control. To see a list of lights and id's available to hue-cli enter: hue lights
Using the id's returned you can enter simple commands to any of your hue lights

Turn light 1 on: hue lights 1 on
Turn light 1 off: hue lights 1 off
Set light 1 to full brightness: hue lights 1 =255
Set light 1 to 50% brightness: hue lights 1 =127
Turn lights 1, 3 & 4 on: hue lights 1,3,4 on

The full list of commands available can be seen with the hue help command or at the hue-cli github page (https://github.com/bahamas10/hue-cli).

Now we need to make an executable batch files for the commands we want to execute. Create a folder somewhere on your PC for your Hue batch files. Create a new text file in that location w/ the name "HueLightOn.bat". Right-click this file and open it with Notepad (or any other plain-text editor, like atom or notepad++). Enter a simple hue command for your chosen light, but add the word call first: call hue lights <your number here> on Save the file, close it and double click it, it should execute the command, just as if you had entered it in the command line yourself. Make a copy of the file named "HueLightOff.bat" and edit it to turn the same light off.

Adding the word call to the command line allows us to chain multiple hue commands together in a single batch file, which allows us to have our On script set the brightness as well, just add an additional line with: call hue lights <your number here> =255

Step 3.) Making it work w/ Stream Deck

Now that we have scripted batch files that we can execute to control our Hue lights and can string as many hue commands together as we like in these files, it's now time to make it all work properly with the Stream Deck. From the Stream Deck UI, add a new "Open" button from the System button collection. Point the App / File: field at one of your batch files and voila watch it fail to work spectacularly. BLERG! ARG! WTF! Why is this script that is working fine failing for the Stream Deck? The answer lies in the way the PATH environment variables works in windows and the nature of the npm package manager works with those variables. Windows has 2 scopes for environment variables, user and system. The Stream Deck launches commands via the Open button in a way that only sees the System PATH, not the User PATH. Fortunately, Node is on the System PATH, however, npm installs packages to the User PATH. The net result is, we need to make a slight change to our batch files to fully qualify the path to the hue-cli so that the Stream Deck can run it properly. First we need to find where npm has installed the hue-cli cmd file. This should be under your user folder in windows, typically:
C:\Users\YOUR USER NAME HERE\AppData\Roaming\npm Navigate to that directory and you should see hue.cmd in the folder. Once you've got the right folder identified, edit your batch files and replace the "hue" command with the fully qualified path version: C:\Users\YOUR USER NAME HERE\AppData\Roaming\npm\hue, so that a line to turn on light #1 would look like:
call C:\Users\YOUR USER NAME HERE\AppData\Roaming\npm\hue lights 1 on Save your batch files and give the button on the Stream Deck another try, your script should now function properly and you should now be controlling your Hue lights with your Stream Deck.

EDIT 6/21 -- The same update that broke silent execution (as worked around in Step 4 below, also fixed issues w/ environment variables the strikethrough above was working around. Some good, some bad, c'est la vie in software.

Step 4.) Fixing the Command Window PopUp - Added 6/21

The latest update to Stream Deck broke things a bit. Prior to 1.1.2, batch files were executing silently with no command window displayed, making these batch files an acceptable way to fire off quick scripts. With 1.1.2 however, they are now popping a large command window up with focus while executing which is pretty shitty user experience. Fortunately we can use a handy utility Nircmd to provide us w/ the command line wizardry to fix this up. So download NirCmd from http://www.nirsoft.net/utils/nircmd.html (the links are at the bottom of the page in text, there's a 64bit and 32bit version available). Extract the zip file that downloads to a directory on your PC (I used c:\nircmd for simplicity). To use nircmd to run our batch file silently, change the "App / File" of the Stream Deck button that is executing your batch script for hue and change it from your batch script to
c:\<path to your nircmd install>\nircmd.exe exec hide <path and name of your batch file> Your button should now open silently and fire. You can also use nircmd in your batch files to do cool things like play a sound. Chaining hue lights on, hue lights red, hue lights =255, hue lights alert and nircmd mediaplay 10000 redalert.mp3 makes a pretty handy Red Alert button.

Related Article