Tom IT

Openhab hue motion sensor

I got the question how to use your hue motionsensor with openhab, so here are the steps you need to do:

  • get hue user (api key)
    You need a hue username key for the requests to the hub, philips has a great tutorial for that 🙂 https://developers.meethue.com/documentation/getting-started
  • setup openhab item in your items folder:
    Switch Motion_Sensor "Motion sensor" <present> (gSkur) { http="<[192.168.x.xx/apiuserkey/sensors/12:3000:JS(getHueMotionEvent.js)]" }

    Note that the id of my motionsensor is 12, and it will poll the status every 3 seconds (3000ms), you need to change the id to the id of your motion sensor, and don’t forget to replace 192.168.x.x with the ip address of your hue hub.
    If you don’t know the ID of your motionsensor check http://IP_HUE_HUB/USERKEY/sensors to find the id

  • add transform script
    The transformscript needs to be added in the transform directory:

    (function(i) {
    var json = JSON.parse(i);
    return ((json['state']['presence'])) == true ? "ON" : "OFF";
    })(input)
  • setup openhab rules
    It might be nice to have something happen when the state changes (turn on / off light for instance), the make this happen you need to add a file to your rules folder (e.g. motion.rules):

    var Timer delay= null
    rule "Motion_Sensor"
        when
        	Item Motion_Sensor changed
        then
          if(Motion_Sensor.state == ON){
            someLightItemName.sendCommand(100)
            
             //Turn off the light after 60 seconds
             delay = createTimer(now.plusSeconds(60)) [|
    		someLightItemName.sendCommand(0)	
    	]
    }
    end

    If you use openhab cloud connector, you can even send a push notification to your phone!, just add this to your rule inside your if statement:

    sendBroadcastNotification("Motion detected")

    I have created a switch in openhab, which I can switch on when I’m out of the house, if the switch is set to on, and motion is detected I will receive the push notification.
    You can create a switch in your items file by just adding “Switch OutOfHouse”  to your items file, and then you are able to check the state of the switch if motion is detected “if(OutOfHouse.state == ON)… and so on”

Add Comment