Fair warning The Hue binding now supports sensor integration. This tutorial is deprecated.
In the last couple of days a lot of questions about the usage and handling of Philips Hue sensors in OH2 popped up in the community. I started a separate thread for collecting every information in one place as I need to reference them later.
Problem statement
Currently it’s not possible to retrieve data from Hue Motion Sensors or Hue Dimmer Switches from the Philips Hue binding (see https://github.com/eclipse/smarthome/issues/2603). We have to do it by creating our own solution. That is possible with the help of the HTTP binding and the JavaScript transformation (JsonPath transformation is possible too but imho not flexible enough because it is not possible to modify the data immediately if we have/want to).
Preparation
You need an authorized username for the requests to the Hue Bridge. Philips has a great tutorial for that https://developers.meethue.com/documentation/getting-started.
Each sensor has its own id. If you don’t know them navigate to http://<bridge ip address>/api/<username>/sensors
in your browser to find them. Look for occurrence of the following types:
-
ZLLSwitch
- dimmer switch -
ZLLTemperature
- temperature sensor (included in the Hue Motion Sensor) -
ZLLPresence
- motion sensor -
ZLLLightLevel
- ambient light sensor (included in the Hue Motion Sensor)
General setup
I recommend to use HTTP cache items for each sensor to reduce the number of requests for polling the Hue API.
services/http.cfg
hueDimmerSwitch.url=http://<bridge ip address>/api/<username>/sensors/<id>
hueDimmerSwitch.updateInterval=5000
hueMotionSensorTemperature.url=http://<bridge ip address>/api/<username>/sensors/<id>
hueMotionSensorTemperature.updateInterval=60000
hueMotionSensorPresence.url=http://<bridge ip address>/api/<username>/sensors/<id>
hueMotionSensorPresence.updateInterval=5000
hueMotionSensorIlluminance.url=http://<bridge ip address>/api/<username>/sensors/<id>
hueMotionSensorIlluminance.updateInterval=15000
items/http.items
// Hue Dimmer Switch - switch
Number hueDimmerSwitchButton "button [%d]" <wallswitch> { http="<[hueDimmerSwitch:5000:JS(getHueButton.js)]" }
DateTime hueDimmerSwitchButtonLastChange "last change [%1$tY-%1$tm-%1$tdT%1$tH:%1$tM:%1$tS]" <time> { http="<[hueDimmerSwitch:5000:JS(getHueLastChange.js)]" }
// Hue Dimmer Switch - device
Switch hueDimmerSwitchBattery "battery low" <lowbattery> { http="<[hueDimmerSwitch:5000:JS(getHueBattery.js)]" }
Number hueDimmerSwitchBatteryLevel "battery level [%d %%]" <battery> { http="<[hueDimmerSwitch:5000:JS(getHueBatteryLevel.js)]" }
String hueDimmerSwitchThingStatus "connection [%s]" <link> { http="<[hueDimmerSwitch:5000:JS(getHueThingStatus.js)]" }
// Hue Motion Senssor - temperature sensor
Number hueMotionSensorTemperature "temperature [%.1f °C]" <temperature> { http="<[hueMotionSensorTemperature:60000:JS(getHueTemperature.js)]" }
DateTime hueMotionSensorTemperatureLastChange "Letzte Änderung [%1$tY-%1$tm-%1$tdT%1$tH:%1$tM:%1$tS]" <time> { http="<[hueMotionSensorTemperature:60000:JS(getHueLastChange.js)]" }
// Hue Motion Sensor - motion sensor
Switch hueMotionSensorPresence "presence [%s]" <motion> { http="<[hueMotionSensorPresence:5000:JS(getHuePresence.js)]" }
DateTime hueMotionSensorPresentLastChange "last change [%1$tY-%1$tm-%1$tdT%1$tH:%1$tM:%1$tS]" <time> { http="<[hueMotionSensorPresence:5000:JS(getHueLastChange.js)]" }
// Hue Motion Sensor - ambient light sensor
Number hueMotionSensorIlluminance "illuminance [%.1f Lux]" <light> { http="<[hueMotionSensorIlluminance:15000:JS(getHueIlluminance.js)]" }
DateTime hueMotionSensorIlluminanceLastChange "last change [%1$tY-%1$tm-%1$tdT%1$tH:%1$tM:%1$tS]" <time> { http="<[hueMotionSensorIlluminance:15000:JS(getHueLastChange.js)]" }
Switch hueMotionSensorDarkness "darkness" <moon> { http="<[hueMotionSensorIlluminance:15000:JS(getHueDark.js)]" }
Switch hueMotionSensorDaylight "daylight" <sun> { http="<[hueMotionSensorIlluminance:15000:JS(getHueDaylight.js)]" }
// Hue Motion Sensor - device
Switch hueMotionSensorBattery "battery low" <lowbattery> { http="<[hueMotionSensorTemperature:60000:JS(getHueBattery.js)]" }
Number hueMotionSensorBatteryLevel "battery level [%d %%]" <battery> { http="<[hueMotionSensorTemperature:60000:JS(getHueBatteryLevel.js)]" }
String hueMotionSensorThingStatus "connection [%s]" <link> { http="<[hueMotionSensorPresence:5000:JS(getHueThingStatus.js)]" }
transform/
getHueButton.js
(function(i) {
var json = JSON.parse(i);
return parseInt(json["state"]["buttonevent"]);
})(input)
buttonevent
returns up to 16 different values for the buttons from top to down:
BUTTON | STATE | CODE |
---|---|---|
Button 1 (ON) | INITIAL_PRESSED | 1000 |
HOLD | 1001 | |
SHORT RELEASED | 1002 | |
LONG RELEASED | 1003 | |
Button 2 (DIM UP) | INITIAL_PRESSED | 2000 |
HOLD | 2001 | |
SHORT RELEASED | 2002 | |
LONG RELEASED | 2003 | |
Button 3 (DIM DOWN) | INITIAL_PRESSED | 3000 |
HOLD | 3001 | |
SHORT RELEASED | 3002 | |
LONG RELEASED | 3003 | |
Button 4 (OFF) | INITIAL_PRESSED | 4000 |
HOLD | 4001 | |
SHORT RELEASED | 4002 | |
LONG RELEASED | 4003 |
getHueTemperature.js
(function(i) {
var json = JSON.parse(i);
return parseFloat(json["state"]["temperature"]) / 100;
})(input)
getHuePresence.js
(function(i) {
var json = JSON.parse(i);
return (json["state"]["presence"]) == true ? "ON" : "OFF";
})(input)
getHueIlluminance.js
(function(i) {
var json = JSON.parse(i);
return Math.pow(10, (parseInt(json["state"]["lightlevel"]) - 1) / 10000);
})(input)
According to the Hue API: lightlevel = 10000 * log10(lux) + 1
.
getHueDark.js
(function(i) {
var json = JSON.parse(i);
return (json["state"]["dark"]) == true ? "ON" : "OFF";
})(input)
getHueDaylight.js
(function(i) {
var json = JSON.parse(i);
return (json["state"]["daylight"]) == true ? "ON" : "OFF";
})(input)
getHueLastChange.js
(function(i) {
var json = JSON.parse(i);
return (json["state"]["lastupdated"]);
})(input)
lastupdated
returns timestamps in UTC.
getHueBattery.js
(function(i) {
var json = JSON.parse(i);
return parseInt(json["config"]["battery"]) <= 10 ? "ON" : "OFF";
})(input)
Define your own threshold when the switch item should indicate a low battery.
getHueBatteryLevel.js
(function(i) {
var json = JSON.parse(i);
return parseInt(json["config"]["battery"]);
})(input)
battey
returns a percentage value (0 - 100).
getHueThingStatus.js
(function(i) {
var json = JSON.parse(i);
return (json["config"]["reachable"]) == true ? "ONLINE" : "OFFLINE";
})(input)
Known issues
- Some of the transformation fails when you disable the sensor in the Philips Hue App