Generic Presence Detection

Hi Rich,

I implemented the code exactly as on your Github page (java code), got the timer_mgr and time_utils installed and created items exactly as with your Github example (did change the names though).

However, my mainPresence item, witch is the proxy, does never switch to the OFF state (I set 5 mins as a start). Nothing shows up in the logs. Any ideas?

I have installed the Items 3 times now, just in case I messed up the metadata part of things but to no avail.

Best regards

So none of your rules are working, if they’re not producing logs?

Bear in mind this example was for OH2. Which version of rules are you running? What version of openHAB are you running? There are differences.

I figured, if the rule is broken or incorrect copied or an item is missing metadata, it will through an error. However, there is nothing in the log so I wandered where else the problem might lie.

The Java-version I got from @rlkoshak s Github page is, I think, the newest. I just got it a few days ago. The TimeOfDay rule works fine, also from his Github page.

I am running OH3.02 release build.

You mean JavaScript? JavaScript is most decidedly not Java.

Show what you’ve done. If you did things exactly right things would be working. Without seeing what you’ve actually done…:man_shrugging:

It’s confusing and I really need time to rework all of these tutorials. The Python versions and Rules DSL versions in all my tutorials and DPs are OH 2.5 only. The JavaScript versions are all OH 3 only.

That’s useful information. It shows you have timeUtils.js and timerMgr.js installed correctly. So there is either something wrong with how you set up the debounce rule or the Items. Did you create the Debounce Group Item? Are all the “raw” Items members of the Debounce Group? What is the metadata used on each Item?

I made a group gPresence. It has a members ArjanPresence and PresenceOverride. Group function is Switch, One ON then ON else OFF.

Then there is mainPresence, the one we want to update with the debounce code. It has no group members and is a switch.

I don’t know how to extract metadata from the items list or the rest-API :grimacing: I do know that I created gPresence like this

Group:Switch:OR(ON,OFF) gPresence { debounce="mainPresence"[timeout="5m", state="OFF", command="True"] }

Hope this sort of helps already.

Find and use “API Explorer” in the UI, see if your Group has members.

But did you create a Group called Debounce and add gPresence to that? From the readme for Debounce:


This code does not depend on the openHAB Helper Libraries. All Items that need to be debounce need to be added a to a Group named Debounce. When any member of that Group’s state changes the rule will trigger to debounce the Item based on the metadata.

Emphasis added.

I missed that part somehow. Will add the Debounce group after our holiday :wink:

I guess gPresence must be the member?

Thanks for the hint!

That’s how the rule knows to run. The debounce rule is triggered by changes to members of Debounce. All Items with the debounce metadata need to be a member of the Debounce Group.

Found a little time and created the Debounce item. Working like a charm now. Thanks for the hint :+1:

Thanks for the tutorial! Works like a charm.

Just one small thing:
Script execution of rule with UID ‘PRESENCE-1’ failed: The name ‘logName’ cannot be resolved to an item or type; line xx, column yy, length 7 in PRESENCE

I had a bit different setup: My presence is signaled through contacts, so I had to rewrite group and rules to fit it as OFF and CLOSED are not compatible…


Switch Presence "Presence [%s]" <presence>
Group:Contact:AND(CLOSED,OPEN) GP_Presence_Contacts "Presence Contacts [%s]" <contact>
Switch Presence_Timer "Presence Timer [%s]" <time> { expire="5m,command=OFF" }


rule "Set presence"
    Item GP_Presence_Contacts changed
    if((GP_Presence_Contacts.state == CLOSED && Presence.state == OFF) || (GP_Presence_Contacts.state == OPEN && Presence.state == ON))
        logInfo("presence", "presence and contacts are in sync")
        if(Presence_Timer.state == ON)
            logInfo("presence", "cancel timer as we currently don't need it as items are yet in sync")
            logInfo("presence", "no timer running and items in sync, nothing to do anymore")


    if(GP_Presence_Contacts.state == OPEN)
        logInfo("presence", "Somebody present!")
        logInfo("presence", "Nobody present, setting timer")

rule "Expire presence"
    Item Presence_Timer received command OFF
    logInfo("presence", "Timer expired, still nobody present")

I was dealing with presence detection on my OH 2.5 installation in the past and I’m just setting up OH3 from scratch (3.2.0 - Release Build).

@rlkoshak Thank you for this great tutorial and the updated best practice for OH3. While creating the Debounce rule based on your rule template I think I missed something because when I trigger the rule in Main UI I get this error:

2022-01-07 18:13:52.646 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘fccb09a5c1’ failed: ReferenceError: “event” is not defined in at line number 198

The items I created in my .item file according to your very good description.
May you have an idea what I did wrong?

How are you triggering it? Just clicking the “play” button? There is nothing to debounce when you do that. There is no Item that has changed state. You need to have the rule triggered by an Item event.

Thank you for your quick response – yes, I triggered via “play” button. I thought with that I could test my configuration – my mistake. Now I tested via setting all presence sensors to OFF. Unfortunately now I get the following error:

2022-01-08 13:02:52.593 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘fccb09a5c1’ failed: PresenceAndi does not have a timeout parameter defined! Debounce metadata should follow debounce=ProxyItem[command=true, timeout=‘2s’, state=‘ON,OFF’]. in at line number 155 at column number 4

But timeout parameter is defined - just copied from your example:

Group Debounce                                            // Rule template Debounce needed
Group:Switch:OR(ON, OFF) Presence "Presence"	          <presence>
Switch PresenceOverride "Überschreibe Abwesenheit"        (Presence) 

Switch AndiPresence "Andi"                                (Presence)   //proxy item
Group:Switch:OR(ON, OFF) PresenceAndi	"Andi" <presence> (Debounce)   ["Presence"]   {debounce="AndiPresence[timeout='5m', states=OFF, command=true]"}
// Sensor definitions
Switch Sensor1 (PresenceAndi)
Switch Sensor2 (PresenceAndi)

What is wrong in my item definition?

Evans debug logging for the rule out manually edit the code to change the debug logs to info. I think that will log out the metadata pulled from the item. If not, add a line to log out exactly what metadata was pulled from the item.

To get the debug logging, in Karaf console I set the logger for org.openhab.model.script.Rules.rules_tools. Triggering the rule once more I only got this output:

2022-01-08 19:44:50.539 [DEBUG] [el.script.Rules.rules_tools.TimerMgr] - Building timerMgr instance.
2022-01-08 19:44:50.555 [DEBUG] [el.script.Rules.rules_tools.TimerMgr] - Timer Mgr is ready to operate

Therefore I added the last line in this code snippet to log metadata:

var checkMetadata = function(itemName, timers) {
   var USAGE = "Debounce metadata should follow debounce=ProxyItem[command=true, timeout='2s', state='ON,OFF']."
   var cfg = MetadataRegistry.get(new MetadataKey("debounce", itemName));
   logger.debug("Debounce " + cfg);


2022-01-08 20:04:09.250 [DEBUG] [el.script.Rules.rules_tools.Debounce] - Debounce Metadata [key=debounce:PresenceAndi, value=AndiPresence[timeout=‘5m’, states=OFF, command=true], configuration=[]]

Does that help?

It seems to be treating the config part of the metadata as part of value.

Frankly, I stopped using. items files a year ago now and I couldn’t say why is not seeing the config part (i.e. the stuff in []) as separate like its supposed to. But that’s the problem. The value is AndiPresence[timeout=‘5m’, states=OFF, command=true] and there config is empty. The value should be AndiPresence and the config should be [timeout=‘5m’, states=OFF, command=true].

Unfortunately I’m out of the business of solving syntax errors in text config files so I’m not going to be much further help. All I can say is it looks right but something is wrong with the metadata.


This is the correct syntax

Group:Switch:OR(ON, OFF) PresenceAndi	"Andi" <presence> (Debounce)   ["Presence"]   {debounce="AndiPresence" [timeout="5m", states="OFF", command=true] }

@JimT Thank you for the right syntax, that did the trick! Now the debounce rule is running properly on my installation.
@rlkoshak Nevertheless thanks for your support. Interesting to read that you seem to move completely away from text config files, if I am not mistaken. Perhaps an option also for me, after I got a better understanding of all the concepts in OH 3. But this is another discussion and off topic.

The tl;Dr is that even though it’s slightly less effecient to point and click or use the API Docs, the time lost is dwarfed by the time lost to solving syntax errors like this and having to keep looking stuff up in reference docs. It’s frustrating fighting problems that simply don’t exist when using the UI and I find it a better user of my time to help solve home automation problems rather than syntax errors.