Generic Presence Detection

Fixed. Alarm sensors never sends OFF command. So i added an expire command into items file

Switch PresentDSC (Presence_Sensors) { expire="1m,command=OFF" }

Thank you @rlkoshak. Beatiful presence detection

1 Like

Glad you got it to work!

Rich

Nice and simple…you are the king of that…how might one go about basing basing an action upon the presence of 2 or members of the group. The reason I ask, I use oh in my office, and only using arp detection, and you know how well that works with new phones…so if I can base a rule on at least 2 members of the presence group, I can be fairly certain someone will actually be reported as on site.

If I understand your question, that is exactly what the DP does.

As written you can have N sensors. Each sensor is a member of Presence_Sensors. If any one of those sensors a ON then Presence_Sensors becomes ON and the Rule sets Presence to ON.

Only if all the sensors are OFF will Presence_Sensors become OFF and a timer will be set. When the timer expires Presence will be set to OFF. If any one sensor returns to ON before the timer expires, the timer is cancelled and Presence remains ON.

So Presence will only go to OFF five minutes after ALL the presence sensors go to OFF.

So if you have two sensors of mixed reliability, as long as one of them is reporting ON then Presence will remain ON.

Is this the behavior you are asking about or am I misunderstanding the question?

If you want to know who is actually on site, not just that someone is on site, then you can create a Group for each person and put the Items representing each person into that Group. Define the Group the same as Presence_Sensors and put the person’s Group in the Presence_Sensors Group.

The Rule will continue to work as is or you can expand it by using Design Pattern: Associated Items, giving each person their own proxy Item (e.g. Branden_Presence), and implementing a flapping timer for each person.

Ok, I mis understood. Thanks Rich

Hi,

Not sure if this is a rule / item issue or an owntracks one but here’s my issue:

I’ve set up some items and rules as per this example, modified them slightly to include a second person, but all still in the gPresent group.

Both my and my wife have owntracks on mobile and both have a region set to home, which when there is movement in or out of a region passes back on or off to my dummy item, (Presence_GP/EP_Home in the image below), so that it triggers the rule…everything perfect here, the logs show the logic working, and vPresent is set to off, (ie no one home), only if both of us are out…SUPERB…

Now for the issue,

Every morning I wake up and check the presence items, (I’m testing it for a while to make sure it’s rock solid before I apply logic to the home / away state), and it seems that sometime every night all the variables are setting themselves back to OFF for no logical reason - that I can see anyway!

Two instances from the logs.

image

image

Different times, but same result…it’s almost like the system is deciding to dump the variables and turn everything off.

The problem is that I now have to spend a day going in and out of region on OneTracks to reset them all properly so it’s up to date…do that and then sometime during the night I’m back to square one…

Any ideas anyone?

In the future please post logs as text using code feces instead of screen shots. Screen shots are impossible to read from a phone, cannot be found in a search, and cannot be copied from.

There is nothing on OH core that will turn off the Switches so the changes must be coming either from your phones or from OwnTracks. Given that’s the case, it would bet better to post this as it’s own thread where users of OwnTracks will see it and hopefully be able to help.

Finally, put the Switches on your sitemap and in the morning just to them manually. You don’t have to drive around to cheer the state of an Item.

1 Like

Hi,

Sorry, was in a hurry this morning so did a couple of cheeky screen shots, will revert to log text later tonight when I get home.

I have switches on the sitemap, which is how I noticed that the settings were OFF, but in owntracks you need to have entered / left a region for it to start the trigger, it was something that puzzled me the first time I set it up as nothing appeared to work, once I’d been out and back it started populating the variables…

I wasn’t sure where to post this to, I thought I’d stat with here in case it was a common thing that variable were ‘reset’ if not used for x amount of time…I’ll wander over to the owntracks thread and try for help there…

Thanks!

Some people create this behavior on purpose using Rules or the Expire binding. But it doesn’t happen by default. Once an Item is set to a state it will keep that state until something else changes it. The something else can be OH restarting, the binding, Rules, or manually through the REST API. Your first step in debugging will be to figure out which of those is responsible for resetting the Switches in your cases.

1 Like

Thanks, I haven’t restarted, the rules seem to be ok, don’t have anything on REST, so I can only assume it’s the binding or owntracks maybe sending a status if it can’t find me anymore.

I’m going to be having a play with a Bluetooth tag for presence shortly so I might see if that is more reliable…

I personally find Network binding with arping or the hping3 external scripts to be reliable enough that I’ve abandoned all other presence detection approaches as unneeded. If you have a BT dongle, you might look into reelyActive Smart Spaces Revisited. If you have Android phones, you might look into (FIND)(Find: High-precision indoor positioning framework for most wifi-enabled devices).

I know there are more up to date tutorials/information threads than that one but I can’t find them. But this should be enough to get you started.

Almost no one I’ve seen on this forum has been happy with GPS based presence detection. It tends not to update fast enough.

Yes, I have the reelyActive thread in my ‘reading list’, (which gets longer every time I expand my knowledge in OH), as a potential. I’ve got guests coming regularly and cleaner etc so I want to steer clear of anything that requires an app on a phone or me having to ‘register’ their phone on the network etc, like the arping / hping3, which is why the BT route seemed best. I’ve also got a completely mixed phone estate in the immediate family, a couple of iphones, an android and even a google phone…

Just give people a tag for their set of keys and / or just leave one in the drawer at home to act as a ‘someone’s home’ presence if needed.

I wanted to try GPS on my / wife phones as a proof of concept and then as an extra presence option to hopefully BT fob as I know we always have our phones on us but don’t always carry the keys / fobs.

I’ll keep experimenting and maybe write up my findings shortly when the set up is more stable.

What would be the impact if this is overruled
“All Switches must be off for a certain amount of time (5 minutes in this case) before turning off presence. If one returns before that amount of time then presence never turns OFF”
I have not followed this and where can I expect the system to misbehave
fake social security number generator try this, i tried out and it was so cool. let me know your experience also

“Misbehave” is only going to happen when the system does something you don’t want it to do. That timer is called an anti-flapping timer. If, for example, you go and stand just at the edge of your WiFi such that your phone is constantly connecting to and then losing connection to the network. The Presence Switch will continue to flip ON and OFF which might cause undesirable behaviors. By waiting for five minutes of OFF before setting the Presence Item to OFF you avoid this flapping. It won’t go OFF until it knows you are really gone.

Notice how we only do this for OFF. For ON we immediately turn ON at the first sign that someone has come home.

It doesn’t have to be five minutes. But it does need to be longer than the longest polling period for your sensors. For example, if the Network binding is pinging your phones every minute for presence, the time should be two minutes.

Another name for this is hysteresis. It’s a buffer that prevents a system from rapidly flipping ON/OFF when it is near the control point.

For Bluetooth presence detection sensor I have been using Monitor running on Raspberry Pi Zero with good results. It’s designed to work with phones, dongles and some watches but may not work directly with some Android devices.

The repository has clear instructions for setting up the sensors. I’m using three in an average size single level house (front door, backdoor, living room), configured to respond as a device_tracker responding ON/OFF via MQTT. The code on this thread has worked seamlessly with this setup. Thank you to all the contributors :slight_smile:

Thanks, I’ll give that a look this weekend when I’m planning on testing my Bluetooth set up.

@rlkoshak whats the benefit of using the Timer instead of :

Switch Presence "Someone is Present" <present> // master presence switch, represents to the rest of OH where someone is home
Group:Switch:AND(OFF,ON) Presence_Sensors <present> // all presence sensors belong to this group


// Optionally one can create additional groups to aggregate all the sensors for
// each person if that matters to you, but the sensors need to all be members of
// Presence_Sensors as well

Switch Sensor1Person1 (Presence_Sensors){ expire="5m,command=OFF" } 
Switch Sensor2Person1 (Presence_Sensors){ expire="5m,command=OFF" } 
...
Switch Sensor1Person2 (Presence_Sensors){ expire="5m,command=OFF" } 
...

then the rule would be:

rule "Reset Presence and sensors to OFF on startup"
when
    System started
then
    Presence.sendCommand(OFF)
    Presence_Sensors.sendCommand(OFF)
end

rule "A presence sensor updated"
when
        Item Presence_Sensors changed
then

    if(Presence_Sensors.state == "ON") {
        logInfo(logName, "Someone is home")
        Presence.sendCommand(ON)
    }
    else  {
          logInfo(logName, "Everyone is still away, setting presence to OFF")
          Presence.sendCommand(OFF)
    }
end

It depends on what behavior you want and how your SensorXPersonX Items are updated. Not all sensors will update those Switches every five minutes and instead only update when there is a change. In that case you will have those Items go to OFF even when you are still present which will break the presence detection.

By creating a Timer just to control when the global Presence Switch goes to OFF, the example works regardless of how the individual sensors work and keeps the example more generic.

If you know all your sensors update at least every five minutes then putting the timers on each sensor Item should work.

Hi,

I’m working on my presence detection a bit more. I’ve had success getting my miniNut Bluetooth device to work with the brilliant Bluetooth binding

and both my wife and I have a nut each on our car fob. Now the issue is that sometimes we go out together in one car, (a chore all married couples have to do from time to time ;-)), so my OH presence doesn’t flip to away.

What I’ve done is add the presence of both our iphones via:

Again, this in isolation works great.

So, my question is on the logic of this…I’ve had a slightly modified version of rlkoshak’s great presence detection script on this thread running but I want to update it so that:

If, and only if, both my miniNut and iPhone are present then I am home
If, and only if, both my wifes miniNut and iPhone are present then she is home
If a guest has a guest miniNut present then guest is present.

In each case if nothing is detected then no one is home…

So, the 3rd part is easy, I’m just wondering how easy it would be to code the if and only if part of the first two???

The very original version of this example handled that case but I dropped it because I wasn’t using it and it added a whole lot of complexity in the Items (but not the Rule luckily.

So here is what you need to do. Create a new Group:Switch:AND(ON,OFF) for each person. The AND(ON,OFF) means that all members of the Group must be ON for the Group’s state to be ON.

Now, instead of putting all the sensors into the Presence_Sensors Group. Instead put all the sensors for person 1 into their Group and all the sensors for person 2 into their separate Group.

Finally, put the new Groups into the Presence_Sensors Group.

Group:Switch:AND(OFF,ON) Presence_Sensors <present>
Group:Switch:AND(ON,OFF) Me_Sensors <present> (Presence_Sensors)
Group:Switch:AND(ON,OFF) Wife_Sensors <present> (Presence_Sensors)

Switch Nut_Me (Me_Sensors)
Switch iPhone_Me (Me_Sensors)
Switch Nut_Wife (Wife_Sensors)
Switch iPhone_Wife (Wife_Sensors)
Switch Nut_Guest (Presence_Sensors)

No changes are required to the Rule.