openHAB lockable motion detector

In my first article Smart garden irrigation I described a rather complex rules and items configuration in openHAB. This time I will focus on the less complex topic of motion detector based switching of lights and other functions. Nevertheless I would like to introduce a very nice concept of reuse within openHAB rules and provide you some hints and links to this concept.

The motion detector application

A lockable motion detection is very useful when you want to switch a light automatically in 80-90% of the cases but, on the other side, you need to lock the light on for example when you are cleaning the room or doing something that takes more time than usual.

I have many of these cases in my house, let me tell you one as an example:

I have a multi-purpose room in my basement. I use it as a store for spare parts, tools and in general stuff I don’t need so often. The second purpose of the room is that sometimes I like to do some electronics stuff, soldering, etc. So occasionally I also use this room as an electronic workshop. Running in and out of the “store” normally takes a few seconds, which is handled by a motion sensor, while I additionally have provided a switch to intentionally switch the light on and keep it on as long as I need it. The motion sensor doesn’t help here really as normally I am sitting for my soldering work and so the light keeps switching off after a while. Well… I could have used a presence detector for this case but really… this costs three times more than a normal motion sensor for a case that occurs only a few times a year. Of course this would be a smart house but not really a smart house holding decision 🙂

Reusable rules

One of the major flaws of openHAB’s rules engine is the fact, that reuse is not really that evident at the first sight. Looking further into the documentation (and googling around with the right keywords), I discovered  a concept, which I would like to share with you: Reusable rules via functions (or better lambda expressions). In a lambda expression packs a code block into an object in order to pass it as an argument to another function. This concept is not unusual in programming languages in general. You find it for example as “closures” in Javascript or “code blocks”  in Objective C.

What the openHAB article Reusable rules via functions and the referenced Xtend documentation for lambda expressions don’t tell, is that there is a whole collection of function and procedure types with up to six parameters, that you can use to build your reusable expressions. A good starting point is the org.eclipse.xtext.xbase.lib documentation.

Now let’s have a look into the function I’ve written for the lockable motion detection and placed into my corresponding rules file:

The function object “bmVerriegelbar” is based on a three arguments function call type Functions$Function3. The lambda expression is surrounded by square brackets, which is historically inspired by the SmallTalk language. The bar separates the arguments from the actual code block, kind of a strange syntax I know but hey… we’re talking about concepts born in the early 80s 🙂

The function takes three arguments:

  • bm is the motion detector, represented by a switch item in openHAB.
  • taster is the switch, which turns the light on and off manually and locks the motion detection function.
  • licht is the light item to be switched on and off

The code itself is quite simple:

  • The light is switched on as soon as the sensor detects motion or the light is switched on manually
  • When the motion detector changes to OFF, the code checks for the status of the manual switch item and switches off only when the manual switch was switched off as well or not used at all.

Note that I provided an additional check for the uninitialized state of the manual switch if the items are not updated on system start.

The final rule

Building a rule that uses the reusable function “bmVerriegelbar” is now very simple. Of course we have to track status changes of our motion sensor and our manual switch and call the previously defined function as follows:

Conclusion

Even though you may get to get used to the syntax of lambda expressions, they are a powerful tool for reusable functionality in openHAB rules. One thing I am missing personally is the capability to put all these functions into a library for reuse in different rules files. At least I didn’t find a solution for that.

I hope you enjoyed reading, you’re welcome to leave a comment here!

Arno