Skip to content

Instantly share code, notes, and snippets.

@mathiasschopmans
Last active February 8, 2020 18:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mathiasschopmans/c0adfcd7eb346af3a053 to your computer and use it in GitHub Desktop.
Save mathiasschopmans/c0adfcd7eb346af3a053 to your computer and use it in GitHub Desktop.
HUE Remote-API summary + buildlamp script

HUE API Instructions

General

Setup a new local API user in the bridge

  • visit https://discovery.meethue.com/

    • write down bridgeid (id)
    • write down internalipaddress
  • visit http://{internalipaddress}/debug/clip.html

  • Insert the following

    Url: /api
    Body: {"devicetype":"Hue#Buildlamp"}
    Method: POST
    

When you press the POST button you should get back an error message letting you know that you have to press the link button. This is a security step so that only apps you want to control your lights can. By pressing the button on the bridge you prove that the user has physical access to the bridge.

Write the content of the JSON's success.username down.

Test new user

You can use the API with your username. Try it with: GET /api/{username} for example /api/27e5d1e28fefc571287bcfe2d481a63

Get Remote-API Access-Token

  • visit http://www.meethue.com/en-US/api/gettoken?devicename=iPhone+5&appid=hueapp&deviceid={bridgeid}
  • allow the app
  • right click on “BACK TO THE APP” and write down ACCESSTOKEN inside the link it redirect to phhueapp://sdk/login/**ACCESSTOKEN**

Use Remote-API

Sending Command Endpoint

URL: https://www.meethue.com/api/sendmessage
Method: POST
Header: Content-Type=application/x-www-form-urlencoded
Parameters: token=**ACCESSTOKEN** (which you obtained earlier)
Body: clipmessage={ bridgeId: "**BRIDGEID**", clipCommand: { url: "/api/0/**APIENDPOINT**", method: "**METHOD**", body: **JSONCOMMAND** } }
  • BRIDGEID is the same one you obtained earlier
  • APIENDPOINT the same as official API /api/<username>/ by removing /api/<usename>/ part
  • METHOD PUT/GET/POST/DELETE the same 4 method as official API.
  • JSONCOMMAND The actual command body for example {"on":true}

Limitations

Current limitation is you cannot immediately know from the response whether your control command succeeded like the official API. All the response you get from calling the Sending Command Endpoint is pretty much always <200> if you are doing it correctly. But you can always pull all the status related to the Hue bridge from the getting status endpoint.

#!/bin/bash
brightness=100
red=65535
green=25650
blue=46920
yellow=15000
if [ "$state" == "running" ]
then
message={"on":true,"hue":$yellow,"alert":\"none\","bri":$brightness}
elif [ "$state" == "failure" ]
then
message={"on":true,"hue":$red,"alert":\"none\","transitiontime":15,"bri":$brightness}
elif [ "$state" == "success" ]
then
message={"on":true,"hue":$green,"alert":\"none\","transitiontime":5,"bri":$brightness}
else
message={"on":false,"transitiontime":10}
fi
printf "$message\n"
printf "Switch state to: $state\n"
printf "\nUpdate Lamp ...\n"
curl \
--silent \
-X POST \
-H "Content-Type: application/x-www-form-urlencoded" \
--data-urlencode 'clipmessage={bridgeId:"###INSERT-BRIDGE-ID###",clipCommand:{url:"/api/0/lights/###INSERT-LIGHT-ID###/state",method:"PUT",body:'"$message"'}}' \
'https://www.meethue.com/api/sendmessage?token=###INSERT-TOKEN###'
printf "\n"
exit 0
@hermanbanken
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment