Working Iphone wifi presence locator

So I recently got a raspberry pi and loaded openhab for my home automation system. After a few days (weeks?) of trying to figure it out, I finally got all of my lights set up. The only thing missing was good presence detection. I wanted the system to know when my wife and I were in the house. We didn’t want to carry anything extra on us, so I figured the iphone (which is always on us) would be perfect. Openhab has a network health configuration and a bluetooth configuration so I thought this would be great.
…It turns out that its not great. As I found out, the iphone has a “deep sleep mode” which turns off wifi and bluetooth after it is asleep for a few minutes. I looked everywhere on the internet, but nobody had an answer. After a long time, i think i may have figured it out. I made a bash script that wakes the phone with hping3, and then uses the arp command to find the iphone’s MAC. If the iphone is connected to wifi, it sends a command to openhab to turn off a switch. Arp is preloaded with raspian, but you will need to download hping3 (sudo apt-get install hping3). Here is the script.


while true

  sudo hping3 -2 -c 10 -p 5353 -i u1 your-ip
  DEVICES=`arp -an | awk '{print $4}'`
  if [[ $DEVICES = *MAC* ]]
    curl --max-time 2 --connect-timeout 2 --header "Content-Type: text/plain" --request PUT --data "ON" http://Your-ip/rest/items/your-item/state
    curl --max-time 2 --connect-timeout 2 --header "Content-Type: text/plain" --request PUT --data "OFF" http://your-ip/rest/items/your-item/state
sleep 30

Just put in your mac address (instead of MAC), your ip (instead of your-ip) and your openhab switch name (instead of your-item) and run the script (sudo bash

It looks similar to this solution…

In that solution, hping3 was used to wake up the phone before the network health binding pinged it.

In the gist of evil hacks for presence/state detection, here’s how to determine whether your Xbox360 is on:

STATUS=`/usr/bin/nmap -p 1025 | grep "1025" | sed -e "s/unknown//" -e "s/1025\/tcp\ //" -e "s/open/ON/" -e "s/closed/OFF/" -e "s/filtered/ON/"`
if [ ! $STATUS ]; then
    echo OFF

This can used with the exec binding…

It is similar to this solution. The problem is that I couldn’t get her solution to work for me, thats why I came up with this one. I also tried making a script that just used hping3, but it didn’t work either.

Hi Jonathan,
Thanks so much for your little script. With that and some modifications, I got finally my iphone detection to work.
I changed it, however, to be used by the http binding as follows:


String MOB001_WLAN_N "Zu Hause [%s]" (Praesenz) { exec="<[/data/OpenHab/*?))]" }


Text item=MOB001_WLAN_N icon="Praesenz" valuecolor=[Anwesend="green",Abwesend="blue"] - placed in /data/OpenHab

declare -a DEVICES
hping3 -2 -c 10 -p 5353 -i u1 -q >/dev/null 2>&1 
DEVICES=`arp -an | awk '{print $4}'`
if [[ ${DEVICES[*]} =~ $CHECK ]]
  echo "Anwesend"
  echo "Abwesend"

with ip of iphone in network
with yy:yy:yy:yy:yy:yy MAC adress in Network

I’m glad you like it! Why do you prefer the http binding?

Actually ignore my question. Your method is much better. Now I don’t have to continuously run two scripts in a terminal window on my pi. Also, it means that I can use a string instead of a switch, Thanks!

Hey, maybe you can help me. I’m actually having a problem running the script your way. The problem is, that I habe to run hping3 -2 -c 10 -p 5353 -i u1 -q >/dev/null 2>&1 as root (so sudo hping3 -2 -c 10 -p 5353 -i u1 -q >/dev/null 2>&1 ), and I cant do that within the exec binding of openhab. Any tips?

Maybe I dont understand you but why dont use sudo hping in the script as in the opening post?

And why can’t u use sudo in exex?

Hi Jonathan,
That was also my intention, just to run one script.
Problem I still encountered is, that hping3 could not wake up in many cases my and my wife’s iphone. I installed OnavoProject VPN app (free) which hinders the wifi to be turned off in deep sleep mode. Seems to work so far.
In regard to your second question:
I installed OpenHab in “root mode”.
However, maybe you can change the rights of the exec binding with chmod via putty or what so ever.
Advise if this helped.
Again thanks for posting your idea. Great job!
Cheers Björn

I have an issue to get the script output into the variable. The script runs perfectly when i run it in bash. My item is

String phone_christin “%s” (gMobiles) { exec="<[/etc/openhab/configurations/*?))]" }

but the value is always “%s” not “Anwesend” or “Abwesend”

Edit: my fault, has to be [%s]


String phone_christin “[%s]” (gMobiles) { exec="<[bash /etc/openhab/configurations/*?))]" }

Jonathan I am encountering the same problem. Did you find any solution ? I tried to add user openhab to sudo but i am still not allowed to use sudo via “sudo visudo” and adding

openhab ALL=(ALL:ALL) ALL

I fixed it. This is what to do


to the bottom of visudo

www-data is the user that the exec program uses to execute commands.

Hope this helps

Which os are you using and which user is openhab running at? I added the www-data user as you mentioned and when I use sudo hping3 in the script it results in a message that I am not allowed to run sudo

Are you sure that it works and exec binding uses www-data user? Did you check if hping3 work by removing the > /dev/null ?

Im using a raspberry pi, running jesse. To be honest, it didn’t work at first. I was so frustrated (by this and a few other things that werent working on my system), I wiped my raspi, reinstalled openhab, re-wrote the script, added www-data to visudo and it started working. I think one problem that I had was that when i installed openhab on my raspi, I used these directions

This calls for me to do

sudo chown -hR openhab:openhab /etc/openhab
sudo chown -hR openhab:openhab /usr/share/openhab

In order for my other computers to have SMB access to openhab (to more easily edit my files). My theory is that this messes up some of the permissions. When I reinstalled openhab, I didn’t do this step and it started working.

If i was better at this stuff, i probably wouldn’t have had to wipe and start over, but sadly I am not…

Hi there

Could someone tell we with binding has to be enabled, because my doesn’t fire up.
I’m using debian Jessie and openhab2.
Thanks in advance

Best regards


It doesn’t need a binding. it works with HTTP PUT requests to a switch. You have to make sure that the item is has the same name as the switch (or presence) in your home.items in openhab. If you post your version of the script and your home.items, ill let you know whats going wrong.

Hallo Jonathan

Thanks for your help.
here is my /opt/openhab/ script.
echo “number one $(date)” >> /opt/openhab/ip1.txt
declare -a DEVICES
/usr/sbin/hping3 -2 -c 10 -p 5353 -i u1 -q >/dev/null 2>&1
DEVICES=/usr/sbin/arp -an | awk '{print $4}'
if [[ ${DEVICES[*]} =~ $CHECK ]]
echo "available"
echo "away"

I created 3 test script hoping to find my error.

my sitemap file:
Frame label=“Groupe Ossy” {
Text item=CurrentDate
//Text item=MOB001_WLAN_N icon=“Praesenz” valuecolor=[Anwesend=“green”,Abwesend=“blue”]
Text item=MOB001_WLAN_N icon="siren"
Text item=MOB002_WLAN_N icon="siren"
Text item=MOB003_WLAN_N icon="siren"
Text label=“Groupement Ossy” icon=“firstfloor” {
Switch item=Lights mappings=[OFF=“All Off”]
Group item=gHeating label="Heating"
Group item=gWindows label="Windows"
Text item=Temperature

and at last my item file:
//Iphone detect
// Working Iphone wifi presence locator
String MOB001_WLAN_N “Zu Hause [%s]” (gPresence) { exec="<[sudo /opt/openhab/]" }
String MOB002_WLAN_N “Zu Hause2 [%s]” (gPresence) { exec="<[bash /opt/openhab/
?))]" }
String MOB003_WLAN_N “Zu Hause3 [%s]” (gPresence) { exec="<[/opt/openhab/*?))]" }

echo is not writing in my /opt/openhab/ip1.txt :frowning:

Thanks a lot
best regards


Short question, you do have the exec binding installed, don’t u