Example of simple anti-flaping mechanism for Presence

Hi,
I would like to share my simple rule for Presence items.

items:

Group:Switch:OR(ON, OFF)     Presence   "Presence"	(All)
Switch Person1 "Person1" (Presence)                                   \\phone1
Switch Person2 "Person2" (Presence)                                   \\phone2
Switch Presence_Timer "Presence_Timer" (Presence)         \\dummy item for anti-flaping of Presence

rules:

import org.openhab.model.script.actions.Timer
var Timer myTimer = null

rule "Presence Timer"
when
    Item Person1 changed or Item Person2 changed
then
if(Person1.state == ON || Person2.state == ON)
Presence_Timer.sendCommand(ON)
else 
if(myTimer!==null) {
   myTimer.cancel    
   myTimer=null }
   myTimer=createTimer(now.plusMinutes(5), [|
 Presence_Timer.sendCommand(if(Person1.state == OFF && Person2.state == OFF) OFF)
])
end

So, very simple, Presence_Timer is a dummy switch which is a member of Presence group switch, and it will not allow for Presence group to go down for 5 minutes after all members went OFF. for coming ON (coming back home) there is no delay.

3 Likes

Thanks for sharing
I believe that there is no need for the import statement

I am not sure what happens if the if stement evaluates to the else branch

what exatly this is doing differently from Swtich group itself?

Presence group is switched on off by those 2 phones anyway so why 5minute timer?

There is a timer on http request which basically means it will stay ON or OFF till next check

Thing network:pingdevice:kriznik   "Mobile iKriznik"   [hostname="xxxxx", refreshInterval=600000, retry=2]

Or?

Hi, the Presence group switch is switched on by one of these devices and switched off by both devices correct.
This timer is a protection for cases like this:
1 person left the apartment. The whole presence group now depends on 1 phone which might be crappy and had showed loss of wifi connection for periods of few minutes during the night.
I dont want my lights to go on off during the night because my Lineage OS google-free phone is not so reliable on keeping the wifi connection stable :slight_smile:
So if your phone reports as OFF for few minutes and then comes back ON (or you went out to take out the trash) your apartment will not go to all systems shutdown mode as the timer will override it and it will not allow the Presence group to go immediately OFF.

Also to mention, I am not using arping network binding, Iā€™m running openwrt on a router, and I have a script on the router that runs every minute, checks wlan0 interface for connected clients with my MAC addresses, if he finds them he updates Person1,2 items with ON or OFF via REST API.

minute update seems to me as a overkill as dhcp lease is usually 24h in home environmentā€¦ so I donā€™t see much added value to the timer tho and as well it seems to be bit tight as phone can be discharged,not on signal etc.

but if it is reliable enough for you, thats fine.
I just wanted to understand why there is need for additional timer, when timer itself can be handled on thing level.

cheers

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.