Koen van Zeijl recently posted a fantastic guide Toggle Philips Hue Depending on Your PC State for accessing the API of your Philips Hue Bridge in order to control lights from PowerShell and Windows task scheduler.

I thought it would be fun to create a guide for using curl and bash for basic Hue automation in Linux. We’ll start with making sure the Huge Bridge can be accessed from a static IP address. DHCP reservation or static IP can be used for this. Let’s go over setting a static IP address for this task.

Open the Philips Hue app on your mobile device, and follow the screenshots below. The images were taken from an Android but iOS should be similar.

Power cycle the Hue Bridge and verify that it now has the IP address specified by browsing to https://discovery.meethue.com/ and looking at the JSON output. Verify that the "internalipaddress" shows the IP address that you specified in the above steps.

Open a terminal and run the following curl command:

curl --silent -H  "Content-Type: application/json"  -d  \
'{"devicetype":"<descriptive_name>"}' http://<hue_bridge_ip_address>/api &&  echo 

The response should be "link button not pressed". Press the center button on the Hue Bridge and then run the same command again.

Now the response should be "success" followed by a long string of characters for a username. Copy that long string and keep it handy because we will need it for the remaining steps.

Let’s see if everything works by getting a list of all the Hue lights.

curl --silent  -k http://<hue_bridge_ip_address>/api/<api_key>/lights | jq '.[] | .name'

If all goes well, a list of your Hue lights will be displayed. However, for each light, there is a light ID that will be needed to control the light. This next command will pull all the lights with their corresponding ID and save it to a text file named “lights.txt”.

for i in {1..30}; do light=$(curl --silent http://<hue_bridge_ip_address>/api/<api_key>/lights/$i | \
jq -r '.name'); if [ $? -eq 0 ]; then echo $i is $light; fi; done > lights.txt

To turn a light on or off, the {"on":true} variable will need to be sent to the bridge with the appropriate ID of the target light. If the target light "Floor Lamp" has the ID of "19" then the command to turn the light on would be…

curl --silent -X PUT -H "Content-Type: application/json" -d '{"on":true}' \
"http://<hue_bridge_ip_address>/api/<api_key>/lights/19/state" > /dev/null

There’s the basics to build some powerful Bash scripts for automating your Philips Hue lights. The rest is up to you.