HowTo: Use Philips Hue sensors (Motion Sensor / Dimmer Switch)

hue
philips
Tags: #<Tag:0x00007f015a028340> #<Tag:0x00007f015a0281d8>

(jsaltberg) #41

Got damn it i’m such a noob at this. I tried to follow your tutorial and I think I got it right.
Now, how the heck do I get it in to my UI (habpanel)? Think I first have to make an item?


(jsaltberg) #42

So! Just got all the Items in, had made some bad copying. It now still returns “Null” when i’m asking to display battery or temp. Some ideas of what went wrong?


(jsaltberg) #43

Wow! Just got it working. Saw that the urls in the http.cfg generated some errors, so I replaced the adress, in this case for the temp and battery status to: hueMotionSensorTemperature.url=http://192.168.1.xx/api/username/sensors/6


(Thomas) #44

Great tutorial, thx! I have one question, within my rule I’m sayin “if light level <= 4” then “turn on the light”
With this, the light level will be above “4”, because the light is on :upside_down_face:.

If the light turns “off” because there is longer any activity within the room and I return to it, within the next 2-4 seconds, it happens that the light will stay “off”, because the sensors light level is still above “4”.

What I would like to do is, one the light turns off, measure the light level immediately to that it is up to date.

Hope you know what I mean :slight_smile:


(Christoph Weitkamp) #45

Hi Thomas,

Yes, I understand what you are meaning. You can try to create a rule which triggers when your light state changes to off. In this rule you can use the sendHttpGetRequest() method to requests the light level sensor data immediately and update your item with the new value.

An example - please be aware of typos. I wrote it down in a second.

rule "When light turns off request light level on demand"
when
    Item yourLight changed to OFF
then
    val response = sendHttpGetRequest("http://<bridge ip address>/api/<username>/sensors/<id>")
    val lightLevel = transform("JS", "getHueIlluminance.js", response)
    hueMotionSensorIlluminance.sendCommand(lightLevel)
end

(Thomas) #46

Thx for answering my question, I hope I will find some time during the weekend for testing.
Just wondering that now one else is having this question, is it to irrational what I’m doing,
to switch On/OFF the lights?


(Christoph Weitkamp) #47

Good luck. Give me a hint if it works. :wink: Turning on and off the lights is a very usual scenario but the use cases and circumstances vary quite a lot.


(Jon) #48

Hi guys,

Having a weird issue I don’t understand. I’m just trying to get the presence detection from the hue device. If I browse to the URL, state.presence is true.

However, if in karaf I do smarthome:status hueMotionSensorPresence

It always returns OFF, even when true.

Same thing when I use a JSONPATH instead of the JS transformation, it will always return false instead of true.

My hue bridge and my openhab are on different vlans, however a simple curl from the openhab box to the sensor’s URL works just fine.

Kinda scratching my head on this one :thinking:

Edit: do you need to chmod +x the transformation files?


(Christoph Weitkamp) #49

Hi Jon,

I am afraid we need more information to help you. Can you please post an abstract of your configuration for the presence setup (item, transformation, http.service, etc.)? Additionally you can activate DEBUG logging for the HTTP binding to get an insight if it requests the data like expected.

No, that is not necessary.


(Jon) #50

Hello Christoph,

Thanks for getting back to me. Essentially, I followed the how-to.

  1. I created an additional user on the hue bridge to query the bridge from openhab. This works perfectly fine when I browse to the URL with said user and the sensor ID (ID is 9 for presence in my case). Same when I do a curl from the openhab box to the URL, the json is returned to me.

The URL is: http://192.168.x.x/api/my_user/sensors/9

  1. In services/http.conf I added the same config as mentioned in the how to while of course changing the URL and user to suit my setup. I did not change the names

  2. In items I created a hue_motion.items file holding the items (again, copy/pasted from here, so names are the same as wel)

I was missing the JS transformation addon at first, thought it was the cause, but even after adding it, the behaviour is the same

I don’t think the problem comes from the how-to, because even when I do a basic JSONPATH to get the presence instead of using the js files, the state never changes when motion is detected.

The sensor is in my office at home, haven’t been in it for the day. In karaf, If I now do:

smarthome:status hueMotionSensorPresence

It returns OFF since the sensor is indeed off.

Same goes for my JSONPATH item:

smarthome:status hueMotionTest

Which returns false.

Now I just went into my office with my laptop, the lights turned on because presence was detected, the browser says presence is true, yet both items still return OFF and false.

I have played around with the polling times in http.conf (set 1000 instead of 5000) etc. but to no avail. I will enable http debug and see if this gives more clues.

EDIT:

Hmm, ok so what I did was enable debug for the http binding. I then uninstalled and reinstalled the binding, at which point in the logs it shows it’s parsing other items I have in there (the mystrom ones), but not the hue ones. Strange. Here’s the http.conf:

# timeout in milliseconds for the http requests (optional, defaults to 5000)
#timeout=5000

# the interval in milliseconds when to find new refresh candidates
# (optional, defaults to 1000)
#granularity=

# whether to substitute the current time or state value into the URL
# (optional, defaults to true)
#format=

# configuration of the first cache item
#<id1>.url=
#<id1>.updateInterval=
mytrom1.url=http://192.168.x.x/report
mystrom1.updateInterval=60000
mystrom2.url=http://192.168.x.x/report
mystrom2.updateInterval=60000
mystrom3.url=http://192.168.x.x/report
mystrom3.updateInterval=60000
mystrom4.url=http://192.168.x.x/report
mystrom4.updateInterval=60000
mystrom5.url=http://192.168.x.x/report
mystrom5.updateInterval=60000
mystrom6.url=http://192.168.x.x/report
Mystrom6.updateInterval=60000
# configuration of the second cache item  
#<id2>.url=
#<id2>.updateInterval=
#Philips Hue Motion Sensor
hueDimmerSwitch.url=http://192.168.x.x/api/user/sensors/4
hueDimmerSwitch.updateInterval=5000
hueMotionSensorTemperature.url=http://192.168.x.x/api/user/sensors/8
hueMotionSensorTemperature.updateInterval=60000
hueMotionSensorPresence.url=http://192.168.x.x/api/user/sensors/9
hueMotionSensorPresence.updateInterval=5000
hueMotionSensorIlluminance.url=http://192.168.x.x/api/user/sensors/10
hueMotionSensorIlluminance.updateInterval=15000

EDIT2: getting somewhere:

2018-09-18 19:31:33.800 [DEBUG] [ab.binding.http.internal.HttpBinding] - updating cache for ‘hueMotionSensorPresence’ (‘http://192.168.x.x/api/user/sensors/9’)

2018-09-18 19:31:33.808 [WARN ] [ab.binding.http.internal.HttpBinding] - Couldn’t transform response because transformationService of type ‘JSONPATH’ is unavailable

2018-09-18 19:31:33.810 [DEBUG] [ab.binding.http.internal.HttpBinding] - transformed response is ‘{“state”:{“presence”:false,“lastupdated”:“2018-09-18T17:16:05”},“swupdate”:{“state”:“noupdates”,“lastinstall”:null},“config”:{“on”:true,“battery”:100,“reachable”:true,“alert”:“none”,“ledindication”:false,“usertest”:false,“sensitivity”:2,“sensitivitymax”:2,“pending”:[]},“name”:“marc’s study sensor”,“type”:“ZLLPresence”,“modelid”:“SML001”,“manufacturername”:“Philips”,“productname”:“Hue motion sensor”,“swversion”:“6.1.0.18912”,“uniqueid”:“00:17:88:01:03:28:7f:4e-02-0406”,“capabilities”:{“certified”:true}}’

2018-09-18 19:31:33.812 [ERROR] [b.core.service.AbstractActiveService] - Error while executing background thread HTTP Refresh Service


(Jon) #51

Ok so the good news is that after reinstalling both the http binding and the json transform, I’m seeing entries in the logs like this:

2018-09-18 19:45:28.994 [DEBUG] [ab.binding.http.internal.HttpBinding] - updating cache for 'hueMotionSensorPresence' ('http://192.168.x.x/api/user/sensors/9')

2018-09-18 19:45:29.090 [DEBUG] [ab.binding.http.internal.HttpBinding] - transformed response is 'false'

2018-09-18 19:45:29.091 [DEBUG] [ab.binding.http.internal.HttpBinding] - Couldn't create state for item 'hueMotionSensorPresence' from string 'false'

(Christoph Weitkamp) #52

Please show me your item configuration. Do you use the JS or the JSONPATH transformation right now?


(Jon) #53

Currently, the items file looks like this. All items are commented out except for the motion sensor at the moment:

// Hue Dimmer Switch - switch
//Number hueDimmerSwitchButton “button [%d]” { http="<[hueDimmerSwitch:5000:JS(getHueButton.js)]" }
//DateTime hueDimmerSwitchButtonLastChange “last change [%1$tY-%1$tm-%1$tdT%1$tH:%1$tM:%1$tS]” { http="<[hueDimmerSwitch:5000:JS(getHueLastChange.js)]" }

// Hue Dimmer Switch - device
//Switch hueDimmerSwitchBattery “battery low” { http="<[hueDimmerSwitch:5000:JS(getHueBattery.js)]" }
//Number hueDimmerSwitchBatteryLevel “battery level [%d %%]” { http="<[hueDimmerSwitch:5000:JS(getHueBatteryLevel.js)]" }
//String hueDimmerSwitchThingStatus “connection [%s]” { http="<[hueDimmerSwitch:5000:JS(getHueThingStatus.js)]" }

// Hue Motion Senssor - temperature sensor
//Number hueMotionSensorTemperature “temperature [%.1f °C]” { http="<[hueMotionSensorTemperature:60000:JS(getHueTemperature.js)]" }
//DateTime hueMotionSensorTemperatureLastChange “Last change [%1$tY-%1$tm-%1$tdT%1$tH:%1$tM:%1$tS]” { http="<[hueMotionSensorTemperature:60000:JS(getHueLastChange.js)]" }

// Hue Motion Sensor - motion sensor
//Switch hueMotionSensorPresence “presence [%s]” { http="<[hueMotionSensorPresence:5000:JS(getHuePresence.js)]" }
Switch hueMotionSensorPresence “presence [%s]” { http="<[hueMotionSensorPresence:5000:JS(getHuePresence.js)]" }
//DateTime hueMotionSensorPresentLastChange “last change [%1$tY-%1$tm-%1$tdT%1$tH:%1$tM:%1$tS]” { http="<[hueMotionSensorPresence:5000:JS(getHueLastChange.js)]" }
//Switch hueMotionTest “presence [%s]” { http="<[hueMotionSensorPresence:1000:JSONPATH($.state.presence)]" }
// Hue Motion Sensor - ambient light sensor
//Number hueMotionSensorIlluminance “illuminance [%.1f Lux]” { http="<[hueMotionSensorIlluminance:15000:JS(getHueIlluminance.js)]" }
//DateTime hueMotionSensorIlluminanceLastChange “last change [%1$tY-%1$tm-%1$tdT%1$tH:%1$tM:%1$tS]” { http="<[hueMotionSensorIlluminance:15000:JS(getHueLastChange.js)]" }
//Switch hueMotionSensorDarkness “darkness” { http="<[hueMotionSensorIlluminance:15000:JS(getHueDark.js)]" }
//Switch hueMotionSensorDaylight “daylight” { http="<[hueMotionSensorIlluminance:15000:JS(getHueDaylight.js)]" }

// Hue Motion Sensor - device
//Switch hueMotionSensorBattery “battery low” { http="<[hueMotionSensorTemperature:60000:JS(getHueBattery.js)]" }
//Number hueMotionSensorBatteryLevel “battery level [%d %%]” { http="<[hueMotionSensorTemperature:60000:JS(getHueBatteryLevel.js)]" }
//String hueMotionSensorThingStatus “connection [%s]” { http="<[hueMotionSensorPresence:5000:JS(getHueThingStatus.js)]" }

And I use the getHuePresence.js located in the transform folder which looks like this:

(function(i) {
var json = JSON.parse(i);
return (json[“state”][“presence”]) == true ? “ON” : “OFF”;
return (json[“state”][“presence”]);
})(input)


(Jon) #54

I think I found the issue…the mystrom switches I showed in my previous post were causing http refresh issues because 5 off them are configured, but not plugged in at the moment. I think that the http binding was getting too many timeouts. The logs showed errors like this:

Fatal transport error: java.net.ConnectException: Connection timed out (Connection timed out)

Or

Error while executing background thread HTTP Refresh Service
java.lang.NullPointerException: null

I’ve commented out all 5 that aren’t online and all of a sudden the motion sensor starts working the way it should:

2018-09-18 23:01:18.198 [DEBUG] [ab.binding.http.internal.HttpBinding] - item ‘hueMotionSensorPresence’ is fetched from cache
2018-09-18 23:01:18.198 [DEBUG] [ab.binding.http.internal.HttpBinding] - updating cache for ‘hueMotionSensorPresence’ (‘http://192.168.x.x/api/user/sensors/9’)
2018-09-18 23:01:18.225 [DEBUG] [ab.binding.http.internal.HttpBinding] - transformed response is ‘OFF’
2018-09-18 23:01:23.227 [DEBUG] [ab.binding.http.internal.HttpBinding] - item ‘hueMotionSensorPresence’ is fetched from cache
2018-09-18 23:01:23.228 [DEBUG] [ab.binding.http.internal.HttpBinding] - updating cache for ‘hueMotionSensorPresence’ (‘http://192.168.x.x0/api/user/sensors/9’)
2018-09-18 23:01:23.255 [DEBUG] [ab.binding.http.internal.HttpBinding] - transformed response is ‘OFF’
2018-09-18 23:01:28.257 [DEBUG] [ab.binding.http.internal.HttpBinding] - item ‘hueMotionSensorPresence’ is fetched from cache
2018-09-18 23:01:28.258 [DEBUG] [ab.binding.http.internal.HttpBinding] - updating cache for ‘hueMotionSensorPresence’ (‘http://192.168.x.x/api/user/sensors/9’)
2018-09-18 23:01:28.290 [DEBUG] [ab.binding.http.internal.HttpBinding] - transformed response is ‘ON’

I guess that because the mystrom where defined before the hue items in http.config, this was causing the problem.


(Christoph Weitkamp) #55

Sounds good. I am glad you got it working.


(Bjoern Berg) #56

I’l like to turn of the motion sensor but I can’t get this to work as somehow manipulating the switch does not update the sensor configuration.

I copied your code 1:1, created the items and replaced IP, username and id with the correct values.
In the logs I can see that the item changes from ON to OFF or vice versa but this does not update the HUE configuration.

The configuration itself is working fine - I can see temperature values, battery state and illuminace values but somehow I cannot disable the sensor.

When going to http:///api//sensors//config directly it says the following:
[{“error”:{“type”:3,“address”:"/sensors/5/config",“description”:“resource, /sensors/5/config, not available”}}]
But going to http:///api//sensors/ (without config) I can see the state of the sensor.

What am I doing wrong?


(Christoph Weitkamp) #57

Hi Bjoern,

Lets try to tweak the rule a little bit and have a look at the response of the sendHttpPutRequest:

    ...
    val response = sendHttpPutRequest(url, "application/json", body)
    logInfo("demo", "Response: {}", response)
end

If this does not give you a satisfying result you can navigate to http://<bridge ip address>/debug/clip.html in your browser, enter the sensor url and body into the form and push the “GET” and “PUT” buttons.

Let me know if it works.


(Bjoern Berg) #58

Hello Christoph,

with your help and the openhab logs I was able to find out that it was not a HUE problem but that the rule had to be adjusted a bit.
I had to put the val entry before the rules in the file and now everything works as it should. (I am using the rule multiple times with different switches and conditions)
Awesome! Thank you for your help and your HUE tutorials.


(Christoph Weitkamp) #59

Perfect, you are welcome.


(Nils) #60

Sorry, I ask, but I don’t find it in the posts.

When you define the update interval in the HTTP-config file, do you need the update interval in the item itself?