Philips HUE Motion sensor in OH2?

Hi All,

I followed everything in this thread and seem to have this working except how do I add my motion sensors to OH2 Things to use them? I’m going to have at least 5 or more motion sensors.

Above code needs 2nd http removed as follows;
Switch Mgang “Gang” (gM) { http="<[hue_ip/api/priv_key/sensors/12:3000:JS(getHueMotionEvent.js)]" }
Number Skur_HUE_Temp “Skur temp [%.2f °C]” (gSkur) { http="<[hue_ip/api/priv_key/sensors/2:300000:JS(getHueTemperature.js)]" }

Also I put HueMotion.items and .js in this location; /srv/openhab2-conf/items

Creating a HueMotion.sitemap doesn’t seem to make a difference.
Contents;
Text item=Mgang
Text item=Skur_HUE_Temp

Lastly after getting this working how can I adjust for F vs C for temp readout?

Thank you in advance for the help with this!

For temp I’ll just do this for now;

return ((((json[‘state’][‘temperature’]))/100)* 9/5)+ 32;

I want to get the time of the last motion in the room. So I stored a new script called getHueMotionLU.js to the transformations:

(function(i) {
    var json = JSON.parse(i);
    return ((json['state']['lastupdated'])) ;
})(input)

It works but unfortunately the time zone (Berlin) is not set in the motion sensor so I have to add one hour. But how to do? Can anybody give me a hint?

Hi Dominik,

How does your linked item look like? Do you use a String item or a DateTime item? Can post an example?

Hi Christoph!

Thanks for your interest. This is the corresponding DateTime item:

DateTime BewegungsmelderFlur_3_LastMotion "Letzte Aktualisierung [%1$tH:%1$tM %1$td.%1$tm.]" { http="<[http://192.168.xxx.yyy/api/secrethuekey/sensors/3:3000:JS(getHueMotionLU.js)]" }

Plain text:
DateTime BewegungsmelderFlur_3_LastMotion “Letzte Aktualisierung [%1$tH:%1$tM %1$td.%1$tm.]” { http="<[http://192.168.xxx.yyy/api/secrethuekey/sensors/3:3000:JS(getHueMotionLU.js)]" }

I did it in the same way and had the same issue with the timezone. Here is my solution.

(function(i) {
    var json = JSON.parse(i);
    if( json["state"]["lastupdated"] !== "none" ) {
        var timestamp = new Date(json["state"]["lastupdated"]);
        var timezoneOffset = new Date().getTimezoneOffset() * 60000;
        return new Date(timestamp.getTime() - timezoneOffset).toISOString();
    } else {
        return "UNDEF";
    }
})(input)

If you want to read multiple values from the same URL (e.g. http://192.168.xxx.yyy/api/secrethuekey/sensors/3) I suggest to use a HTTP cache item (see https://docs.openhab.org/addons/bindings/http1/readme.html#example-of-how-to-configure-an-http-cache-item). This reduces the number of requests.

GREAT!

Thanks so much for your help!

When following the above procedure, I end up with this error… Now what? (sorry for the line breake, I dont know how to fix that).

2018-01-24 23:49:25.799 [ERROR] [ab.binding.http.internal.HttpBinding] - Transformation ‘JS(getHueMotionEvent.js)’ threw an exception. [response=[{“error”:{“type”:4,“address”:"/12",“description”:“method, GET, not available for resource, /12”}}]]
org.openhab.core.transform.TransformationException: An error occurred while executing script.
at org.openhab.core.transform.TransformationHelper$TransformationServiceDelegate.transform(TransformationHelper.java:62)[182:org.openhab.core.compat1x:2.1.0]
at org.openhab.binding.http.internal.HttpBinding.execute(HttpBinding.java:189)[219:org.openhab.binding.http:1.10.0]
at org.openhab.core.binding.AbstractActiveBinding$BindingActiveService.execute(AbstractActiveBinding.java:157)[182:org.openhab.core.compat1x:2.1.0]
at org.openhab.core.service.AbstractActiveService$RefreshThread.run(AbstractActiveService.java:169)[182:org.openhab.core.compat1x:2.1.0]

2018-01-24 23:49:28.921 [ERROR] [ab.binding.http.internal.HttpBinding] - Transformation ‘JS(getHueMotionEvent.js)’ threw an exception. [response=[{“error”:{“type”:4,“address”:"/12",“description”:“method, GET, not available for resource, /12”}}]]
org.openhab.core.transform.TransformationException: An error occurred while executing script.
at org.openhab.core.transform.TransformationHelper$TransformationServiceDelegate.transform(TransformationHelper.java:62)[182:org.openhab.core.compat1x:2.1.0]
at org.openhab.binding.http.internal.HttpBinding.execute(HttpBinding.java:189)[219:org.openhab.binding.http:1.10.0]
at org.openhab.core.binding.AbstractActiveBinding$BindingActiveService.execute(AbstractActiveBinding.java:157)[182:org.openhab.core.compat1x:2.1.0]
at org.openhab.core.service.AbstractActiveService$RefreshThread.run(AbstractActiveService.java:169)[182:org.openhab.core.compat1x:2.1.0]

Did you install http-binding? Seems to be missing or working incorrectly.

Hi Kim,

May I ask you to use code fences for readability? That would be very much appreciated. Thanks.

Please post your item configuration. There seems to be an error with the URL.

I´ll try the code fences… Hope it works.

items file

Switch 	Stue_HUE_Motion	<present>	(gSkur)	{ http="<[http://10.4.28.39/api/my_key/12:3000:JS(getHueMotionEvent.js)]" }
Number	Stue_HUE_Temp 	"Stue temp [%.2f °C]"	<temperature>	(gSkur)	{ http="<[http://10.4.28.39/api/my_key/sensors/11:300000:JS(getHueTemperature.js)]" }

Log:

2018-01-25 23:03:37.758 [INFO ] [b.core.service.AbstractActiveService] - HTTP Refresh Service has been started

2018-01-25 23:03:37.877 [ERROR] [ab.binding.http.internal.HttpBinding] - Transformation 'JS(getHueMotionEvent.js)' threw an exception. [response=[{"error":{"type":4,"address":"/12","description":"method, GET, not available for resource, /12"}}]]

org.openhab.core.transform.TransformationException: An error occurred while executing script.

	at org.openhab.core.transform.TransformationHelper$TransformationServiceDelegate.transform(TransformationHelper.java:62)[182:org.openhab.core.compat1x:2.1.0]

	at org.openhab.binding.http.internal.HttpBinding.execute(HttpBinding.java:189)[219:org.openhab.binding.http:1.10.0]

	at org.openhab.core.binding.AbstractActiveBinding$BindingActiveService.execute(AbstractActiveBinding.java:157)[182:org.openhab.core.compat1x:2.1.0]

	at org.openhab.core.service.AbstractActiveService$RefreshThread.run(AbstractActiveService.java:169)[182:org.openhab.core.compat1x:2.1.0]

Hope it reads better now.

A short notice… Seems like the problem only regards for ID 12, the motion sensor. Temperatur does seem to work, though it doesn´t report often.

And my getHueMotionEvent.js

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

Looks much nicer. As I guessed: your URL is wrong. sensors/ is missing.

Try

Switch 	Stue_HUE_Motion	<present>	(gSkur)	{ http="<[http://10.4.28.39/api/my_key/sensors/12:3000:JS(getHueMotionEvent.js)]" }
1 Like

Ahhh crap…
Such an stupid mistake. I have been looking at this so many times, so I simply overlooked this :joy:

Thanks for the help Christoph!! It work just fine now.

Great, I’m glad you got it working.

Hi Christoph…
How does the above round the output of a temperature in the items file ?
I have several (no Philips Hue) temperature sensors, and all of the reporting needs rouding. But I fail to understand how to do this in the item file using your example.

This is one of my temperature sensors:

Number koekken_Temperature "Temperature [%.1f °C]" <cu_heating> ["TargetTemperature"] {ihc="11011604"}

It results in outputting ie 20.08 degreese. I sure would like 20.9 indsted :slight_smile:

Hi Kim,

The item itself contains the original value. Only the display value is rounded in this example to one decimal. You can adjust that by changing the %.1f to a different value. The 1 indicates the number if decimals. If you need to have rounded values in your item you have to use the first or second way.

Actually I could live with a not rounded value. But only one decimal.
Hmm, if the 1 indicates the number of decimals then something is wrong, as I get two decimals from all my temperature sensors. And the items is defined like showen above, [%.1f °C]

My motion sensor works perfect with OH2.3. But I get only every 5 minutes an update.

That is my item config:
Number HueMotionSensorIlluminance “Helligkeit [%.1f Lux]” { http="<[http://192.168.0.8/api/APIKEY/sensors/7:3000:JS(getHueIlluminance.js)]" }

Thus, I should get Update every 3 seconds.

Has anyone an idea?

Thanks

Hi Jens,

Just a shot in the dark: Do you have other items which use the same sensor URL?