Make sure you fill in your own local IP address of your iPhone (replace 10.1.1.20). Save and close the file. Btw thanks @rlkoshak for the sleep 1 suggestion.

Note that this script is adjusted from a multiple- to a single device script from @Seaside which you can find in post 3 of this thread, however to communicate status of multiple devices, mqtt messaging was added there.

Now continue to change the permissions of the script-file:

sudo chown openhab IphoneD.sh
sudo chmod 777 IphoneD.sh

Check if the script is executable with:

ls -all

Edit sudoers file, this enables the user openhab to run sudo commands without password. Enabling user openhab to run sudo commands can potentially be dangerous, do this at your own risk.

sudo visudo

Make sure the last line looks like this (if your user for openHAB is openhab)

openhab ALL=(ALL) NOPASSWD: /bin/ip, /bin/bash

openHAB

Now we go to openHAB. We make a rule that is triggered by a cron job to check from time to time presence (every minute in this example). For details on cron triggers, see this link. And we add a rule to make sure we don't detect 'false non-presence' if the Iphone does not react. Empirical evidence suggests every 19 minutes at least the Iphone answers once the request.

Some things occurred which I could not explain, maybe you can explain to me:1) I inserted a hyperlink, first one works, second one only shows the text.2) I inserted link to another forum user, first one works, second one only shows the text3) I could not give 'hard enters / line breaks' at some point in the post, which makes it more difficult to read (e.g. step 3)

This is a tricky problem I had before. Might even be the one @rlkoshak and I were talking about the other day: One of your code fences had trailing whitespaces, which seems to bring Discourse out of balance. Example with whitespaces as °:

Thought I would share it in case someone is looking to modify in a similar manner

Some differences- Loop to be able to do presence for multiple devices, I run this on a standalone Linux Server- I only flush the arp table for the device I'm looking for (not all, since my server have a lot of arp entries)- I look the MAC-address up, you only need to know the IP, which could be good and bad, this solution might not be as robust- I only arp the specified device, probably not an issue to do arp -an for all.- Relaying on mosquitto_pub to publish messages, which is a bit hard coded.

My detection with iPhone 6 is not flawless yet either, I also will include a delay to make the feedback more robust. I have been doing quite some research, but did not find yet the solution to solve this satisfactorily.

Edit: since this post the initial post has been updated and the presence detection works well now.

For whatever it is worth, we have a iphone7 and one of the latest nexus android phones and both show drop-outs with this method that I cannot reproduce yet. I have to use this script with the Nexus android phone and iphone7 (both phone are fully updated) as the network binding does not work for my android phone (seems to have same no-reponse-to-ping issue, when sleeping). I've played around with some parameters, but to make the iphone respond reliably takes much longer and more frequent hping3's than in the current script....tried it only manually as not to ruin battery life.....but frequent hping3 pings for about a 1min or 2 seem to be necessary at times. Android seems to be less reliable. At times the presence works rock-solid for both phones at times it does not work at all.....mmmhhh

I have a One Plus 3 Android Phone (Android 6.0.1), for that one I need a timeout of 10 mins, compared to the 19 with the iPhone 6.I have a SetPoint in my sitemap for the timeout, so can easily adjust the timers.Usually I notice timeouts during the night, but for the last couple of days it has been stable using those settings.

@Seaside Your script is great and seems to work flawlessly for users. However, not everyone uses MQTT or should be forced to install a broker. Would you alternatively add a way to communicate with the openHAB REST API? Integration via stdout might also be possible. Unsure how to best handle the multiple devices feature that way. I'd probably move the loop to openHAB but I get that your goal was different.

Would be great to finally have ONE solution for all users

@Maurits28 maybe you could update the first posting with this solution. Setting of the privileges should be less insecure by following this recommendation.

I'm not an expert on this, but is your network device linked to eth0? You can get more info with sudo ifconfig. Did you change the ip address in the IphoneD.sh script to the ip address of your Iphone? You can also try to use the updated solution that you can find at the top of the page.

Let me know how you progress with it.

@Seaside, @ThomDietrich On another note, I updated the initial solution at the top of the page with the great work @Seaside has done, I just need to add the timeout rule of 19 minutes.

@Maurits28: Thanks for your script. I've a comprehensive question: I like the approach of the maxcounter timeout. But when I understand the rule right, this means that after a "real" absence (so no false non-presence) it takes 19 minutes (as the script is only executed once a minute) until the presence switch is finally switched to OFF. So I have a quite big delay after leaving the house (in case I'm further using the on/off presence switch for other things (alarm activation etc.).

And another question:I've just created the script and all necessary settings. Just for testing I did several starts of the script from the shell. I have defined two IPs, one is present at the moment (.37), the other one is abscent (.32). .32 is reported correctly every time, but .37 only gets one correct state in about 10 tries. Is this "normal behavior"?

EDIT: I've done a little more research. Both iPhones (5s and SE) are only detected when they are in active use. But as soon as I lay them down and the screen goes off, the detection doesn't work anymore (or everything but reliable).

Any thoughts? Does this mean that the hping3 isn't working? hping3 is installed and I can run hping3 from bash.

@Maurits28: Thanks for your script. I've a comprehensive question: I like the approach of the maxcounter timeout. But when I understand the rule right, this means that after a "real" absence (so no false non-presence) it takes 19 minutes (as the script is only executed once a minute) until the presence switch is finally switched to OFF. So I have a quite big delay after leaving the house (in case I'm further using the on/off presence switch for other things (alarm activation etc.).

Hi Stefan, your understanding is correct, and to prevent 'false' away detections a time-out of 19 is in use. The issue with the Iphone is that when it is in sleep mode, it is hard to wake it up. For that we use Hping3, to wake it up just before we detect it's presence via arp. Even then it does not always wake up, hence the 'time-out threshold' of 19 tries.

jaydee73:

And another question:I've just created the script and all necessary settings. Just for testing I did several starts of the script from the shell. I have defined two IPs, one is present at the moment (.37), the other one is abscent (.32). .32 is reported correctly every time, but .37 only gets one correct state in about 10 tries. Is this "normal behavior"?

I see that you tried once every few seconds, my experience with such an interval is also that it does not detect the iPhone lots of times. You might want to try an interval of 30 seconds instead of one minute, and see empirically how many times you'll need to have a reliable presence detection. Post your results in this thread so we can learn from your experience!

Thanks for the material. I am installing openHAB2 and found your script educational. My question to you is why don't you use the arp command to obtain the IP address of Apple devices since the MAC addresses of the latter have a well known prefix. (ref: https://nmap.org/book/nmap-mac-prefixes.html.

It is an interesting thought, however for me personally the additional work needed to implement does not weigh against the advantages. And I see a few things that still would need 'manual' work:- Assigning of a specific Apple device to a specific person in your household- You do not want to track all Apple devices like AppleTV, iPad, MacBook as you are interested in the fact if somebody is in the house or not.

But hey, why don't you give it a try and tell us how it works? There are always users that would benefit from your proposed solution.

Hi seaside,I like your shared bash script, I turn to your script as it can check multi iPhone in single run,after few days operate, I think it's not necessary to flush arp in every run

ip neigh flush dev eth1 $i

, I use crontab to flush arp every 10 minutes and run your script every minute, in openhub items time out in 4 minutes, result is pretty accurate, may be 1 time false "away" per day, as my home only got 1 door to leave, so door must open when someone leave, than I set the offline rules turn TRUE if door also opened within 10 minutes which makes the detection 100% right in my home. you may also go for a try.