Best Presence detection

Silly question @ksiezykmirek, but where in the Tomato interface should I place this code?

You will have to login via ssh to tomato router, use Putty or similar terminal software.

Hmm, I know how to do that, but not the custom scripting. I’ll have to go away and have a look for a guide.

I’m assuming:

cru a " * * * * * **name** 00:00:00:00:00"

is saved is a crontabs file somewhere


function fcomp() {
    awk -v n1=$1 -v n2=$2 'BEGIN{ if (n1<n2) exit 0; exit 1}'

RET=`brctl showmacs br0|grep $1`
if [ "$RET" = "" ]
echo 'FAIL'
curl --header "Content-Type: text/plain" --request PUT --data "OFF" http://$OHIPPORT/rest/items/$2/state
ATIME=`echo $RET | awk '{print $4}'`
echo $ATIME
if fcomp $ATIME 200; then
echo "OK"
curl --header "Content-Type: text/plain" --request PUT --data "ON" http://$OHIPPORT/rest/items/$2/state
echo "EXP"
curl --header "Content-Type: text/plain" --request PUT --data "OFF" http://$OHIPPORT/rest/items/$2/state

is saved as in somewhere like /tmp/home

But how does the cron job know where the .sh file to run is located?

And then i’m still unsure where **name** 00:00:00:00:00 gets called; from the init section of the router?

Will see what I can find online.


In your case it will be:
cru a " * * * * * /tmp/home/ name 00:00:00:00:00"

Be sure that:
Chmod 755 /tmp/hone/

I have found problem in this linę:

Grep is case sensitive, do the -i parameter need to be add before $1. Otherwise macs has to have all small cases.

Next update:
CRU (cron) list is lost on reboot, so in need to be addet to init script like this:

cru a mirek “* * * * " "/jffs/ xx:xx:xx:xx:xx:xx mirek"
cru a ania "
* * * *” “/jffs/ yy:yy:yy:yy:yy:yy ania”

Great idea, it’s working well for me. Just sharing a few notes from issues I ran into getting it set up on my router.

My version of Tomato doesn’t have bash, and /bin/sh is symlinked to busybox ash. This causes an issue with the “function fcomp()” line (syntax error on the function), so just trim it to “fcomp()” and that will work.

Second issue I had was with your execution. Originally you listed [switch name] [MAC address] as the syntax, but it’s actually expecting [MAC address] [switch name]. Not a big deal, just might be confusing if someone is reading along at home.

Thanks again for the great idea!

Very true words about basic security.
Allow me to chip in on the lighter side:
there is a great (dutch) TV commercial of an insurance company that shows a guy giving a lecture on home automation. And just as he enthusiastically is telling how he can control his home from his phone, burglars show up at his house. At that moment the guy is showing the crowd how he can open his garagedoor and put a spotlight on his prized vintage car… anyway, you get the picture.

You are very right saying in the end it is just about how much risc you are willing to accept. Though I am basically in favor of even being able to switch on my toaster via my phone, I still rely on just a traditional lock on my doors. No WiFi, RFID or what have ya there

1 Like

I feel like I should put this script i worked on a while back here. Uses a simple scan to detect and keep your presence but uses bluetooth authentication if you’ve been away. This fixes an issue with spamming authentication as that screws up both the server and the phone and eventually at least one will need a restart.

1 Like

This thread hasn’t seen a lot of activity… but since I think it’s still a resource for new openHAB users looking to set up presence detection, I want to point out a new method of detection for iPhone users. Bear in mind that you’ll need to modify the example rules a bit if you want presence switches, but it shouldn’t be too complicated.


I am working on a JSR223 lib for presence detection that is (when finished) intended to use a varity of technologies to detect Presence. Right now i am in an early state, where I have done a lot of thinking and written som pseudo code.

My intention is to provide a Python library that can use some or all of these technolgies:

  • Wifi (connected, RSSI, Roaming between AP)
  • Bluetooth Proximity
  • Presence service (like OwnTracks)
  • Sensors (Door contacts, PIR Sensors, Cameras etc.)

The idea is that each person can be configured to use a different/individual technology to identify Presence.
My idea with the unpersonalized sensors is to use those as an indicator that there is somebody there. The personalized “sensors” like Wifi, Owntracks will be used to calculate a probability of whom it may be.

I am also considering a mechanishm where a specific series of events can be used; For instance

  1. Pir Sensor in hallway is activated
  2. Frontdoor is opened
  3. Bluetooth in car is seen by Bluetooth scanner
  4. Time is between 6:30 - 7:30 in the morning

At my place this means (>90%): Wife is leaving for work.

That was just some initial thoughts. i have started a POC to see what needs to be done. :slight_smile:


Do you plan your python library to be able to work distributed over multiple nodes?
Does Bluetooth Proximity include BTLE devices?

Yes, For me this is exactly the same as Wifi, it can have multiple nodes (Access Points).
The problem is that it shouldn’t be too complex to setup, then it is useless in my opinion.

You should think of this library as a library that works with data that is already in openHAB (via items and/or things/channels).

It would be great to include the Wasp in the Box algorithm into this library as well. That would round out the features nicely.

1 Like

Thanks for mentioning. I have brifly looked at the article you have a link to earlier in this post. Actually I think that the concepts will cover my thoughts. At least I have thought of the difference in sensors (e.g personal).
I will start of with this algorithm as it seems to cover most of my thoughts.

Hey @mr_eskildsen,

that sounds very promising! I am very excited.


Just to update how I am progressing.
I have now reached my first milestone. That is to get a rough skeleton up and running.
I already had started a project to handle rules for my lightning. When thinking about it I realised that quite a lot of the basic handling would be exactly the same code. So I have created a bae lib to handle these things.
Next I have created python classes so that I can build my model.
I have decided to split the problem into “two dimensions”. One dimension is to identify where (location) events is happening, and the other dimension would then be how accurate can I identify whom is present.

Now each and every sensor should define how accurate it is in each of the two dimensions. Eg. a door contact is very accurate in location dimension, where it doesn’t tell you much about who is opening the door. I Wifi connected device would be more accurate (moderate to high), about identifying a person (it could be a device shared amongst a couple of persons).
I have created a sensor called WifiClient and I have setup a framework to get notifed whenever one the the following events occurs:

  1. Connect/Disconnect
  2. RSSI changed
  3. Roaming between different Accesspoints

For each sensor I calculate i it has detected presence, when it was detected and also when last “sign of life” was detected. I also plan to add a value defining for how long time a “sign of life” would mean that somebody is there. For a door/window sensor this would mean 30-60 seconds, where a play command on a Chromecast would mean everything between a few minutes (watching a Youtube clip) and a couple of hours (Netflix movie).

Each sensor is mapped to at least one of the two dimensions (depending on the definition of the sensor). A Wifi Client would be mapped to the house location (and not a room) because that would corresppond to the accuracy. It would also be mapped to one or more specific persons, thus i covers both dimensions great and therefore I have started with this.

Right now I have just pushed the code onto my production Raspberry Pi and I will use the comming days to improve the WifiClients ability to determine presence.
One problem I would have to deal with is that I always turns off my phone during night (so openHAB would believe I am out whenever I goes to bed).
That one have to be solved, I am thinking of a “Keep A live” mechanishm, Without having implemented it, i think it should be some kind of a hysteresis that keeps the presence flag high as long as the phone is off. I hope that knowledge about the RSSi could help in determination of this. i believe RSSI for a phone leaving would get weaker before disconencting, where it would just dissapear in this case. But I need som logs to identifi what to do,

I almost forgot to state that my primarily goal is to define something, that other people can use without recoding everything. It should be adaptable,s o that you just would have to define your objects and link them together in the two dimensions. But that is future. Right now I can hardly model my own world :slight_smile:
I should also be possible to extend the library with your own sensors, so that all kinds of strange stuff could be used to presence detection.

Next step would be to make this simple sample (version 0.1) usefull, and also add validation. I won’t publish anything before I have a simple validation of the model added.I think I have taken quite a big step, since I have a working framework right now.


This is the problem that the wasp-in-the-box algorithm is supposed to solve. Basically if you have sensors that detect entry/exit like a door sensor, if you no longer detect presence events but an entry sensor hasn’t triggered assume they are still present.

I agree that this is part of the answer. I can think of a lot of situations where the lack of a door contact being triggered won’t tell you the full story. As an example: Lets assume that I turn off my phone at 22:00, at 22:30 my wifes comes home (and she has a habbit of turning of Wifi). The presence detection knows that I am at home, it will then detect that front door is opened and since it doesn’t detect anybody comming home, it might end up concluding that I am leaving.
Off course you can argue that I doesn’t have sufficient sensors to detect if somebody is entering or leaving. So it is a bit more complicated than just calculating what the sensors available says. I believe the result is highly dependent on how you choose sensors to represent availability of a person. The algorithm has to be aware if the sensors linked to that person is usefull for determining presence for a specific person. Therefore I see that each sensor has two attributes:

  1. Accuracy of Location
  2. Acuuracy of determining a person

Basically door window contacts has a high accuracy, because they are mounted on a specific location, where they say absolutely nothing of the person.On the other hand other sensors would be much tighter related to a person.

The problem is that the algorithm should be able to identify the quality of the calculation, the quality is really difficult to embed into calculation, because it depends on how different people is acting. It is hard to determine the quality of the calculation, since the same sensor would result in different quality for my wife and my self. So the tricky part is to make this parameter configurable WITHOUT making configuration rocket science.

So there is a need to make relevant parameters configurable, therefore it would be nice to dicuss concrete cases and also ideas to handling this in an simple way.

Yesterday I have enabled a a WifiClient for one personal device for each person in the house. The results is as expected: Not even close to anything reliable :slight_smile:
Tonight I have added a Geofence (OwnTracks) to two persons, so I can start playing around and tweaking :slight_smile:

Offcourse contacts, pir sensors etc.also has to be taken into account.

Again I would like some ideas to concrete implementation.As soon as I have some code that I beleive is just a little usefull, I will publish that.

Martin, have you thought about using Face detection. A face detection in the doorway, and you know for sure who is entering, (known people). Then use WiFi/bluetooth to state when they leave.

I wonder if it would be possible using Netamo Welcome. I havn´t given this lots of thoughts, and have not seen any serious reviews stress testing the Netamo Welcome. Heck I dont even know if it´s possible to use the Netamo Welcome together with Openhab. But I guess a face detection item would cover most presence problems.