Unable to get iphone presence detection to work! Pls help!

Hello, I’ve been working around with OH1.8 and I am having trouble with a fine presence detection system.
Here are my configurations so far:

String Test_Presence "At Home[%s]"  { exec="<[/opt/openhab/iphonedetect.sh:60000:REGEX((.*?))]" }


Text item=Test_Presence valuecolor=[YES="green",NO="blue"]


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

where xxx.xxx.xxx.xxx is my Iphone’s IP address and yy:yy:yy:yy:yy:yy is my Iphone’s MAC address.
However, it can not run properly, the status of the string “At Home” is always NO, so i check the command:

sudo hping3 -2 -c 10 -p 5353 -i u1 xxx.xxx.xxx.xxx 

I received a message told me that the connection was 100% lost, did I do something wrong? Please show me how to fix it, I want whenever I log in my home wifi network, the string At Home must change accordingly.

As far as I know, there is another way of presence detection system using owntracks, I’ll dig into it, but first, can anyone please could show me what did I do wrong?

I´ve made the experience that detecting the iPhone is not really working well for me, as it often drops out from the WLAN network when going to sleep. Maybe trying with Bluetooth would help.
OwnTracks may be worth a try, depending on GPS accuracy this works well and has the advantage that if you are working e.g. in the garage or garden it does not show you as not at home.

I am not at home at the moment, so I could not check my own configuration, but I have a working iPhone detection with OH1.8.3.

I would assume this is some kind of file permission problem. At least some thoughts:

  • Is hping3 installed (I think so, otherwise the error message would be another one…btw, what is the correct error message?)
  • Does the user openhab has the right to execute iphonedetect.sh? (I assume you installed OH using apt-get? Or is everything executed as root?)
  • If the user is openhab, does openhab has the right to execute arp and hping3? If not, you have to add this in the sudoers file
  • is the mac address entered in lower case letters? (you wrote yy:yy and not YY:YY, so I assume it is. But just to be sure…)
1 Like

Thanks both jaydee and Oggerschummer,
First, I did install hping3. the error message when I tried to ping my IPhone was:

HPING (eth0 udp mode set, 28 headers + 0 data bytes

--- hping statistic ---
10 packets transmitted, 0 packets received, 100% packet loss
round-trip min/avg/max = 0.0/0.0/0.0 ms

Second, I did install OH by using apt-get, however I used sudo chmod 777 iphonedetect.sh so that this file is executable

Third, all of the letters representing my phone’s MAC are uppercase, so I think it must be YY in my script file.

Lastly, can you show me how to add the right to execute arp and hphing3?

Regarding the mac address: It HAS TO BE lower case. Like aa:bb:cc:dd:ee:ff, not AA:BB:CC:DD:EE:FF.

I will also take a look what to add to the sudoers file this evening for allowing openhab to execute arp and hping3.

If I run the hping3 command directly, I do also get the same result as you (100% packet loss). So you shouldn’t pay too much attention to this.

Regarding the sudoers file, do the following. Enter the following command on the command prompt:

sudo visudo

Then, add the following lines at the end of the file:

openhab ALL = NOPASSWD: /usr/sbin/hping3
openhab ALL = NOPASSWD: /usr/sbin/arp

Then save the file and exit. I don’t know if a reboot is necessary, but just to be sure…

Then I would suggest to run the script directly from the shell and see if anything changes.

I add the line you showed me:

And when I go to /opt/openhab (where I located iphonedetect.sh) and used command :


the answer is always NO regardless I did connect to my Wifi home network :frowning:
Of course, the item on my openhab webpage also displays as “NO”

What happens when you ping your iPhone IP from the command line?

when I use the normal ping (sudo ping xxx.xxx.xxx.xxx) , it was totally ok! I sended and received all the packets, however if I use sudo hping3, 100% lost packet,

Oh, this is my router problem, I figured out that my router can not recognize my Iphone’s MAC address, so I try another router, everything works now, thanks jaydee very much