Philips HUE integration

 

Grenton - Gate Http - Philips HUE


A competitor in smart home lighting for IKEA Tradfri is Philips HUE. This tutorial will show how to control HUE lighting from the Grenton system.

 

The HUE bridge is required for integration, but in this case we don't need Node-Red support. Philips HUE provides a REST Web API using which you can communicate directly using the Gate Http module.


The API for Philips HUE is extensive and allows a lot. You can find it at: https://developers.meethue.com/
free registration as an application developer is required.

 

In this example, you will learn how to turn on/off, control the brightness of a group of lights.


The Groups API from the "Hue API": /api/<username>/groups will be used in this tutorial.

  1. At the beginning, you need to configure Philips HUE lighting installation using the dedicated Hue mobile app.
  2. Next, you need to know the IP address of the HUE bridge (HUE bridge received the IP address from the router). You need to log into your router's admin panel and note down the IP address.
  3. The Philips HUE bridge provides an API testing interface via a web browser. The interface is available at https://<adresip bridge>/debug/clip.html.
  4. With the test interface you can obtain the user name (key to the API) for the integration
    1. Click on the button on the HUE bridge
    2. In the "Message Body" type:
  5. {"devicetype":"my_hue_app#gate http" }
    1. URL "/api"
    2. Send the request by clicking the "POST" button
    3. The user name will appear in the "response" field. Write it down. This information will be needed.

    6. Now it is possible to retrieve the group data using the same interface:

  1. Set the URL to /api/<username>/groups
  2. Leave the Message Body blank
  3. Click GET
  4. In the Command Response window you get a JSON from which you can obtain the group identifiers and names and other attributes describing the current state.


    7. Create user features in GATE_HTTP:

  • hue_api_name - (string) user name generated in previous steps
  • hue_bri_1 - (number) light brightness value
  • hue_on_1 - (boolean) indication if the light is on or off

    8. It is helpful to add group’s ID or group’s name. In this example the ID of 1 group is used.



Create a HttpRequest virtual object which will be used to handling of connection between HUE bridge and Philips HUE. Configure it as follows:

  • Host - IP address of the HUE bridge
  • Path - /api
  • Method - PUT
  • RequestType i ResponseType - JSON

    9. To control the light group, a Smart Panel and its buttons’ events are assumed:

  • OnClick - on/off, increase/decrease brightness
  • OnHold - increase and decrease brightness
OnClickOnHold

    10. The script for on/off light control in 1 group:

local var = GATE_HTTP->hue_on_1
local reqJson = nil

if var == 0 then
    var = 1
    reqJson = { on = true }
else
    reqJson = { on = false }
    var = 0
end

local path1 = "/api/".. GATE_HTTP->hue_api_name .."/groups/1/action"

GATE_HTTP->hue_on_1 = var
GATE_HTTP->hueRequest->SetPath(path1)
GATE_HTTP->hueRequest->SetRequestBody(reqJson)
GATE_HTTP->hueRequest->SendRequest()

    12. The script with upVar parameter for increase brightness the light:

local bright = GATE_HTTP->hue_bri_1

if (bright + upVar) <= 255 then
    bright = bright + upVar
    GATE_HTTP->hue_bri_1 = bright
else
    GATE_HTTP->hue_bri_1 = 255
    bright = 255
end

local hueJSON = {
on = true,
bri = bright
}

local path1 = "/api/".. GATE_HTTP->hue_api_name .."/groups/1/action"

GATE_HTTP->hueRequest->SetPath(path1)
GATE_HTTP->hueRequest->SetRequestBody(hueJSON)
GATE_HTTP->hueRequest->SendRequest()

    13. The script with downVar parameter for decrease brightness the light:

local bright = GATE_HTTP->hue_bri_1

if (bright - downVar) >= 0 then
    bright = bright - downVar
    GATE_HTTP->hue_bri_1 = bright
else
    bright = 0
    GATE_HTTP->hue_bri_1 = 0
end

local hueJSON = nil
if bright == 0 then
    hueJSON = {
        on = false,
        bri = bright
    }
else
    hueJSON = {
    on = true,
    bri = bright
    }
    
end

local path1 = "/api/".. GATE_HTTP->hue_api_name .."/groups/1/action"

GATE_HTTP->hueRequest->SetPath(path1)
GATE_HTTP->hueRequest->SetRequestBody(hueJSON)
GATE_HTTP->hueRequest->SendRequest()

The above scripts take parameters that define the amount by which to brighten/dim the light, and make sure not to exceed the extreme values.