Example of simple anti-flaping mechanism for Presence

Hi,
This has nothing to do with DHCP. router can see the wifi ACTIVE connected clients. as soon as you disconnect the device from the router, in 2 seconds the device disappears from the list of active wifi clients. (I dont know about your router, but this is how routers usually behave when you check for active wifi clients list (nothing to do with DHCP clients).
I am thinking of reducing the script to 30 seconds actually, as router is not doing anything anyway, and its a really small light script.

So, rather then pinging the phone all the time (every second), I opted in for an approach that router tells openhab if the client is here or not here.

When I walk in the apartment, in less then 1 minute music starts playing, AC starts running, and if its after sunset, some lights come on.

I dont want all this to come off when I accidentaly drop wireless connection. I am actually using 10 minute timer as I never lost wifi for more than that. And I will probably replace the crappy phone anyway and reduce the timer as my wife’s samsung had never dropped a connection for a minute.
(we charge phones every night so there is no discharged situations)

I don’t understand what do you mean additional timer when timer can be handled on thing level?
It’s very simple and common, there is other scripts from people online with timers doing the same thing, people dont want Presence group to flop OFF immediately when all phones are disconnected.
Previously I had a timer within “Going out” rule (which looks at Presence group state), but I found it’s better to handle it on the Presence group level then in every separate rule you might have that involves Presence group.

Just to add my grain of salt
This can be done simply with 4 items and a very short rule by installing the expire binding

Group:Switch:OR(ON, OFF)     Presence   "Presence"	(All)
Switch Person1 "Person1"  (Presence)                                \\phone1
Switch Person2 "Person2"  (Presence)                                \\phone2
Switch Presence_Timer (Presence) { expire="5m,command=OFF" }
rule "presence"
when
    Item Person1 changed or Item Person2 changed to ON
then
    Presence_Timer.sendCommand(ON)
end

When either of the Persons change to ON the Presence_Timer Item is updated to ON and the 5 minutes timer reset

5 Likes

Hi,
I dont understand from your rule what happens with your Presence_Timer Item when both phones go to OFF…and how it prevents Presence group to go OFF in that case…

When both phones go to OFF for more than 5 minutes the Presente_Timer goes to OFF and the Group is OFF
Otherwise the Group is ON

1 Like

since I have multiple hotspots around the house, checking connection on each of those would be quite tidious proces, not to mention that phones usually use deepsleep eg they are not actively connected when not needed.

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

well if you had openwrt on all access points it would be a piece of cake, and mobile phones in deep sleep are still on wifi (have you noticed you still receive whatsapp messages)
or if you are not on openwrt you can use arping that forces phones to answer to ping even in deep sleep (this is the method network binding is using).
OK, clearly you are not interested in presence detection via mobile phone devices, so what are you doing about it, motion sensors, other sensors, nothing? :slight_smile:

am using arping for mobile presence detection, but indeed i will never ever use this information to trigger anything inhouse because from real world experience this is very unreliable and not always wanted so there is no place for it it my general ruleset.

I do use it for some weekend additional lake lights as there is no point to have it running when nobody is home, but for home itself am not using it at all.
On top of it, me and my family is not phone-centric based so there are situations which makes phones not really indicators of actual presence as well :wink:

I just pointed out that those 5mins timer is not doing anything different than whole group on/off trigger anyway :wink: no big deal, was just asking

my main in-house / out-house presence is my alarm being armed or not and doors have been opened or not in certain period between arming/disarming zones

no not really, as these apps are using push and they will work on data or wifi as they want, my iphone is disconnecting from wifi when it’s not using for some time as well as my wife’s
Phones are not constantly on wifi for battery saving, am not telling it is general behaviour but some of them are doing that quite often

I don’t understand this statement.

The timer is doing something different. There is a proxy Item, Presence_Timer. This proxy Item is what the Rules use to determine whether someone is present or not.

When Person1 or Person2 change to ON, Presence_Timer is immediately set to ON.

When Person1 and Person2 changed to OFF, the Rule waits 5 minutes before setting Presence_Timer to OFF. Should Person1 or Person2 return within that 5 minutes, Presence_Timer remains ON.

The Group Presence isn’t actually use at all in this example and could be eliminated. But the point is Presence isn’t actually used by the OH Rules to determine if someone is present. That’s what Presence_Timer is for.

This filters out the situations described where the presence sensors erroneously detect all people are away when in fact they were just momentarily out of range or there was a network issue or something.

I think there’s a snag with the expire version; if ‘phones’ stay ON for ten minutes, it will expire OFF.
You could manage ways around that by separating command from state and using another rule to filter/retrigger at OFF time - but then you might as well manage all by Timer.

That is why the group is still needed

You can’t rely on the Group because it’s state will not follow the anti-flapping timer. You either need to use Present_Timer to represent the actual presence in which case rossko57’s right and there is a snag. Or you need to add another proxy Item to represent the anti-flapped presence state.

Yes, that’s the point

So, if the phones stay ON for more than five minutes, the Group will remain ON. The Phones will remain ON. But Presence_Timer will be expired to OFF.

Since Presence_Timer represents whether or not everyone is home the house will go into away mode even though everyone is still home.

To work as written, Presence_Timer will need to be updated or commanded to ON every 5 minutes or less to keep the expire binding from expiring it to OFF while any of the sensor Items or the Group remain ON.

No the group represents if someone is home or if everyone is away.
The timer is just a timer

Right, the magic is making the expire timer a member of the Group as well, so it acts as an Group ON-stretcher

The bit confused me was rule trigger changed to ON - you really want any change (including to OFF)

1 Like

Correct

Why? Explain your thinking?

A change indicates activity. A phone may drop off because it’s been turned off, or the owner has walked from here to there. Leaving the building is only one possible cause.
Just me I suppose; for motion sensing I start a timeout from “off” i.e. motion ceased, because that is the interesting part.

fair point for not being phone centric :slight_smile:
you need to get some smart wifi watches for your family this christmas, wearing is mandatory! :slight_smile:
btw the phone is not suppose to decide to burn your data (you might not have a data plan active at all) if wifi is available. I had mobile data off and never lost push notifications regardless of deep sleep…
disclaimer: I am not iphone fan and never used it :slight_smile:

your real world is different then mine, I am in 1550sq ft apartment on high floor, there is no house or huge back yard involved, people don’t wander off leaving the phone in back yard, front porch etc…
wifi coverage in the entire flat is excellent with single access point, if the phone is offline it is not in the apartment, period. so the timer of few minutes is all I need to override hickups.
samsung S8 with official firmware never lost wifi coverage in the apartment whatsoever, but if you are open source enthusiast with unlocked bootloaders and unofficial LOS firmware without google services, you might experience hickups :slight_smile:

I missed the magic. That is a clever way to handle it indeed! I may have to revisit my tutorial.

It depends on how the motion sensor works really. Some report every motion detect and that’s it. Just a series of ON/OPEN updates. Other report motion and then after a time of no motion report OFF/CLOSED. Most will have a sleep period after motion is detected in which it will not report new motion. In the case where the OFF means that there has been no motion for that time period and doesn’t necessarily line up with the time of last motion. So it depends on what your use case is as to which event is most meaningful.

For example, if you want a shorter time than implemented by the device you can only use the ON/OPEN events. If you want a longer time than you can either use the ON/OPEN events with the amount of time you actually want to delay since the last motion, or use the OFF/CLOSED events with the amount of time you want after the last motion minus the time built into the device.