Example of simple anti-flaping mechanism for Presence

You can simplify your cancelling of the Timer with

    myTimer?.cancel()
    myTimer = null

But you can further simplify it by just rescheduling the Timer since you recreate it anyway.

    if(myTimer !== null) myTimer.reschedule(now.plusMinutes(5))
    else myTimer = createTimer(now.plusMinutes(5), [ |
                                  Presence_Timer.sendCommand...
                              ])

I like this very simple Rule but if you need to add more sensors it might get unruly.

To expand this same approach to include multiple sensors for each person (often users must include multiple presence sensors for accurate presence detection, e.g. wifi and BT) see Generic Presence Detection. The big difference is it uses Expire timers like Vincent demonstrated, and it relies on Group aggregation functions rather than doing tests on individual Items in the Rule itself.

For those using JSR223, I’ve submitted a generic and reusable implementation of the above approach to the Helper Libraries, see https://github.com/openhab-scripters/openhab-helper-libraries/pull/234. It supports generic presence and can be configured to detect individuals.

1 Like