[solved] Hue Motion Sensor (ON/OFF State)

Hi there

i want to know (and later switch) the status of an Hue Motion Sensor.

as Example the Hue Battery status:

Number KuecheHueBattery "Batterie [%.0f %%]"                    <batterylevel> (Kueche, gMotion) 
                { http="<[http://10.0.0.109/api/xxxx/sensors/26:300000:JS(getHueBattery.js)]" }

getHueBattery.js

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

that works…

now i try the same syntax for my Sensor Status:

Number HueMoetionSensorKueche "Hue Sensor Küche [%s]"                          (Kueche, gMotion)
                { http="<[http://10.0.0.109/api/xxxx/sensors/26:300000:JS(getHueStatusOnOff.js)]"}

getHueStatusOnOff.js

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

thats the Hue API view…

109_api_sensors_25

why is this not working? The Log stay blank.

edit:

i try’d also:

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

didn’t work too

I’m not sure, what you would like to achieve. Would you like to check, if there is motion?

Then you need to check for “state -> presence” not “config -> on”.

So your JS-file should look like this:

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

And I think your item should be a String instead a Number.

String HueMoetionSensorKueche "Hue Sensor Küche [%s]"                          (Kueche, gMotion)
                { http="<[http://10.0.0.109/api/xxxx/sensors/26:300000:JS(getHueStatusOnOff.js)]"}

Norman

edit:

sorry, to get no confuse i delete this post…

-> With String it works for the state.

2018-03-29%2010_12_18-HUE%20Sensor%20K%C3%BCche

but when i use a switch:

2018-03-29%2010_14_18-HUE%20Sensor%20K%C3%BCche

Why the switch does not get the right state?

This could be a way, if the one you are trying to achieve is not working:
You could create a rule that changes the state of the item in the hue bridge via a shell script. Something like that:

Switch HueSensorKueche "Hue Sensor Küche"

and a rule like this:

rule "Switch hue motion sensor on and off"
when
        Item HueSensorKueche changed
then
 if  (HueSensorKueche.state == ON) {
 
    executeCommandLine("sh /path/here/hue-switch-ON.sh")
}

else {
   executeCommandLine("sh /path/here/hue-switch-OFF.sh")
}

end

The shell scripts basically trigger a curl command to make the change like this:

curl -H "Accept: application/json" -X PUT --data '{"config": {"on": true}}' http://192.168.x.x/api/xxxxxxxxxxxxxxxx/sensors/26

and

curl -H "Accept: application/json" -X PUT --data '{"config": {"on": false}}' http://192.168.x.x/api/xxxxxxxxxxxxxxxx/sensors/26

It’s not very elegant and the other way is better, but it does the job (in case nothing else works).

Thank you for you help and work.

The Switch allready runs.

Switch HueMotionSwitch "Hue Sensor Switch"                                     (Kueche, gMotion) 
var Sensor = 25

rule "Switch Hue Motion Sensor ON / OFF"
when
    Item HueMotionSwitch changed
then
    val url = "http://10.0.0.109/api/xxxx/sensors/" + Sensor + "/config"
    var String body = ""
    if( HueMotionSwitch.state != ON ) {
        body = '{"on":false}'
    } else {
        body = '{"on":true}'
    }
    logInfo("HueSensorKueche", "Send command '{}' to {}", body, url)
    sendHttpPutRequest(url, "application/json", body)
end

The Problem is the Switch get not the actual state from the switch.

When i turn the switch, then the state are good, but when i refresh the sitemap then the actual state is not given.

i search the forum, but i can’t get the solution. i think i need a rule to call the state, but i have no idea how to do.

Don’t know if this the right way, but this does update the switch in my case

rule "Hue Sensor Status abfragen"
when
    Item HueMoetionSensorKueche changed
then
    if (previousState == NULL) {
        HueMotionSwitch.setState(ON)
     }
end

thanks for the help