Mona Jalal
Mona Jalal

Reputation: 38145

Controlling Philips Hue using OpenHAB in Raspberry Pi

I want to control the Philips Hue Lux color lamps with OpenHAB. So I wonder what are the steps I should do and if I am missing something.

In pi@raspberrypi:/opt/openhab/configurations/rules $ sudo vi demo.rules

rule "Select scene"
    when
        Item scene received command
    then
        switch(receivedCommand) {
            /*alles uit */
            case 0 : {
            sendCommand(Toggle_7, OFF); 
            sendCommand(Toggle_3, OFF);
            sendCommand(Toggle_1, OFF);
            sendCommand(Toggle_10, OFF);
            sendCommand(Toggle_11, OFF);
            sendCommand(Toggle_5, OFF);
            sendCommand(Toggle_9, OFF);
            }
            /*sfeer */
            case 1 : { 
            sendCommand(LWD_3, 90);
            sendCommand(Color_7, "45.584415,86.033516,70.19608");
            sendCommand(Color_1, "45.584415,86.033516,70.19608");
            sendCommand(Color_10, "45.584415,86.033516,70.19608");
            sendCommand(Color_11, "45.584415,86.033516,70.19608");
            sendCommand(Color_5, "45.584415,86.033516,70.19608");
            sendCommand(LWD_9,10);
            }
            /*Alles aan */
            case 2 : {
            sendCommand(Toggle_7, ON); 
            sendCommand(Toggle_3, ON);
            sendCommand(Toggle_1, ON);
            sendCommand(Toggle_10, ON);
            sendCommand(Toggle_11, ON);
            sendCommand(Toggle_5, ON);
            sendCommand(LWD_9,100);
            }
            /*eten */
            case 3 : {
            sendCommand(Toggle_3, OFF);
            sendCommand(Color_7, "42.391304,73.6,100.0");
            sendCommand(Color_1, "42.391304,73.6,100.0");
            sendCommand(Color_10, "42.391304,73.6,100.0");
            sendCommand(Color_11, "60,42,100");
            sendCommand(Color_5, "60,42,100");
            sendCommand(LWD_9,10);        
            }
            case 4 : {
            /*nacht */
            sendCommand(LWD_9, 50);
            sendCommand(Color_7, "46.25,37.64706,100.0");
            sendCommand(Color_10, "46.25,37.64706,100.0");
            sendCommand(Toggle_1, OFF);
            sendCommand(Toggle_3, OFF);
            sendCommand(Toggle_11, OFF);
            sendCommand(Toggle_5, OFF);
            }
            case 5 : {
                /* Sunset */
            sendCommand(Color_7, "42.391304,73.6,100.0");
            sendCommand(Color_1, "42.391304,73.6,100.0");
            sendCommand(Color_10, "42.391304,73.6,100.0");
            sendCommand(Color_11, "42.391304,73.6,100.0");
            sendCommand(Color_5, "42.391304,73.6,100.0");
            sendCommand(Toggle_3, OFF);
            sendCommand(LWD_9,10);        
            }
            case 6 : {
                /* Aqua */
            sendCommand(Color_7, "230.82353,100.0,100.0");
            sendCommand(Color_1, "230.82353,100.0,100.0");
            sendCommand(Color_10, "230.82353,100.0,100.0");
            sendCommand(Color_11, "230.82353,100.0,100.0");
            sendCommand(Color_5, "230.82353,100.0,100.0");
            sendCommand(Toggle_3, OFF);
            sendCommand(LWD_9,10);        
            }
            case 7 : {
            /* Duo */
            sendCommand(Color_7, "272,100.0,100.0");
            sendCommand(Color_5, "272,100.0,100.0");
            sendCommand(Color_1, "330,100.0,100.0");
            sendCommand(Color_10, "330,100.0,100.0");
            sendCommand(Color_11, "330,100.0,100.0");
            sendCommand(Toggle_3, OFF);
            sendCommand(LWD_9,10);        
            }

            case 8 : {
            /* Duo ambilight play */
            sendCommand(Color_7, "272,100.0,30.0");
            sendCommand(Color_5, "272,100.0,30.0");
            sendCommand(Color_1, "330,100.0,30.0");
            sendCommand(Color_10, "330,100.0,30.0");
            sendCommand(Color_11, "330,100.0,30.0");        
            }
            case 9 : {
            /* Duo ambilight pauze*/
            sendCommand(Color_7, "272,100.0,100.0");
            sendCommand(Color_5, "272,100.0,100.0");
            sendCommand(Color_1, "330,100.0,100.0");
            sendCommand(Color_10, "330,100.0,100.0");
            sendCommand(Color_11, "330,100.0,100.0");
            }
        }
end

in pi@raspberrypi:/opt/openhab/configurations/items $ vi demo.items

I have
:

/* Hue */
Switch Toggle_1   "left bulb"   (Switching) {hue="1"}
Switch Toggle_2   "center bulb" (Switching) {hue="2"}
Switch Toggle_3       "right bulb"  (Switching) {hue="3"}

Color Color_1     "left bulb"   (Colorize)  {hue="1"}
Color Color_2         "center bulb" (Colorize)  {hue="2"}
Color Color_3     "right bulb"  (Colorize)  {hue="3"}

Dimmer Dimm_1     "left bulb"   (WhiteDimmer)   {hue="1;brightness;30"}
Dimmer Dimm_2     "center bulb" (WhiteDimmer)   {hue="2;brightness;30"}
Dimmer Dimm_3     "right bulb"  (WhiteDimmer)   {hue="3;brightness;30"}

Dimmer CT_Dimm_1      "left bulb"   (CTDimmer)  {hue="1;colorTemperature;30"}
Dimmer CT_Dimm_2      "center bulb" (CTDimmer)  {hue="2;colorTemperature;30"}
Dimmer CT_Dimm_3      "right bulb"  (CTDimmer)  {hue="3;colorTemperature;30"}

in pi@raspberrypi:/opt/openhab/configurations $ vi openhab.cfg:

I have:

############################### Philips Hue Binding ###################################
#
# IP address of Hue Bridge (optional, default is auto-discovery)
hue:ip=192.168.1.117
hue:secret=openHABRuntime
hue:refresh=10000

I ran it using sudo ./start.sh and then paired the Hue hub with OpenHAB. I am not what is missing because still I can't control the Hue with OpenHAB.

enter image description here

enter image description here

enter image description here

My main struggle is connecting the web UI to the code in openHAB. Any pointer is really appreciated! Also I have stitched the code above from various tutorials so it possibly would make no sense at all!

Upvotes: 0

Views: 7331

Answers (2)

Sam Turner
Sam Turner

Reputation: 51

It is random yes but you also have to pair the bridge. Have you got it do at least one bulb can turn on and off?

Just checking but your switch and scene items are not called switch and scene are they?

The binding is picky and you might have to declare the variable type for each:

var DecimalType hue = new DecimalType(240) // 0-360; 0=red, 120=green, 240=blue, 360=red(again)
var PercentType sat = new PercentType(100) // 0-100
var PercentType bright = new PercentType(100) // 0-100
var HSBType light = new HSBType(hue,sat,bright)
sendCommand(Light_GF_Lounge_C, light)

Though to be frank, the binding is a bit of a pain and you would be better off using the API directly and then setting scenes you have set in an app or something.

Or directly

rule "Set lights"
when
    Item Switch_Lounge received update ON
then
    var String hueApiCall = "curl@@-X@@PUT@@-d@@{\"on\": %s, \"hue\":%d , \"sat\": %d, \"bri\": %d, \"transitiontime\": %d}@@http://192.168.1.147/api/YOURPAIREDHUEUSERID/groups/%d/action"
    executeCommandLine(String::format(hueApiCall, true, 13088, 212, 100, 10, 2 ))
end

Each of the values in the executeCommandLine match the % placeholders in order, in this example on: true, hue: 13088, sat: 212, bri, 100, transition time: 10, group 2

Using that syntax you can set scenes, alerts, all sorts.

Upvotes: 0

LeonG
LeonG

Reputation: 871

Changing how the UI's look is done with the *.sitemap files. This files define how and which items are shown in the UI: https://github.com/openhab/openhab/wiki/Explanation-of-Sitemaps

As for the rest of the example: - Are you sure about the ip and secret of the Hue bridge? - How many light bulbs are connected to the bridge? Because in the *.items file hue=1 means the first bulb connected to your bridge.

Upvotes: 1

Related Questions