Working Iphone wifi presence locator

Hi there

I’renamed my sitemap and items file and created two new ones ti keep it simple.

Here the items file:
String MOB001 “Zu Hause [%s]” { exec="<[sudo bash /opt/openhab/iphonedetect.sh:60000:REGEX((.?))]" }
String MOB002 “Zu Hause2 [%s]” { exec="<[bash /opt/openhab/iphonedetect2.sh:60000:REGEX((.
?))]" }
String MOB003 “Zu Hause3 [%s]” { exec="<[/opt/openhab/iphonedetect3.sh:60000:REGEX((.*?))]" }
DateTime CurrentDate “Date [%1$tA, %1$td/%1$tm/%1$tY]” { channel=“ntp:ntp:ossy:dateTime” }

and the sitemap file:
sitemap ossy label=“Menu Presence”
{
Frame label=“Groupe Ossy” {
Text item=CurrentDate
Text item=MOB001
Text item=MOB002
Text item=MOB003
}
}

It still don’t update, but I have new entrys in the logfile:

2016-01-30 20:32:31.617 [INFO ] [.dashboard.internal.DashboardService] - Started dashboard at /start
2016-01-30 20:32:31.883 [INFO ] [b.core.service.AbstractActiveService] - Exec Refresh Service has been started
2016-01-30 20:32:31.894 [INFO ] [b.core.service.AbstractActiveService] - Exec Refresh Service has been started
2016-01-30 20:32:31.895 [INFO ] [b.core.service.AbstractActiveService] - Exec Refresh Service has been shut down
2016-01-30 20:32:31.905 [INFO ] [b.core.service.AbstractActiveService] - Exec Refresh Service has been started
2016-01-30 20:32:31.906 [INFO ] [b.core.service.AbstractActiveService] - Exec Refresh Service has been shut down
2016-01-30 20:32:32.084 [INFO ] [b.core.service.AbstractActiveService] - HTTP Refresh Service has been started
2016-01-30 20:32:32.885 [INFO ] [b.core.service.AbstractActiveService] - Exec Refresh Service has been shut down

EXEC is installed. Do I need the create a cfg file?

++
Ossy

You should enable debbug logging

Hello,

I had this working, but now my iphone is repeatedly switching from ‘home’ to ‘nothome’ at nearly every refresh interval of the item config. Its been connected to wifi the entire time. Any thoughts?

Chris

I agree. Enable debug mode, To do this, go to logback.xml and change

To

Can you post your log file?

Here is the relevant log portion, 1st showing home

2016-01-30 18:53:40.803 [DEBUG] [.o.b.exec.internal.ExecBinding] - item ‘chris_phone’ is about to be refreshed now
2016-01-30 18:53:40.804 [DEBUG] [.o.b.exec.internal.ExecBinding] - executed commandLine 'sudo /home/chris/iphonedetect.sh’
2016-01-30 18:53:41.057 [DEBUG] [.o.b.exec.internal.ExecBinding] - exit code ‘0’, result 'home’
2016-01-30 18:53:41.057 [DEBUG] [i.s.RegExTransformationService] - about to transform ‘home’ by the function '(.*?)'
2016-01-30 18:53:41.057 [DEBUG] [.o.b.exec.internal.ExecBinding] - transformed response is ‘home’

2nd showing away

2016-01-30 18:54:01.219 [DEBUG] [.o.b.exec.internal.ExecBinding] - item ‘chris_phone’ is about to be refreshed now
2016-01-30 18:54:01.220 [DEBUG] [.o.b.exec.internal.ExecBinding] - executed commandLine 'sudo /home/chris/iphonedetect.sh’
2016-01-30 18:54:01.523 [DEBUG] [.o.b.exec.internal.ExecBinding] - exit code ‘0’, result 'nothome’
2016-01-30 18:54:01.524 [DEBUG] [i.s.RegExTransformationService] - about to transform ‘nothome’ by the function '(.*?)'
2016-01-30 18:54:01.524 [DEBUG] [.o.b.exec.internal.ExecBinding] - transformed response is ‘nothome’

It looks like it’s working fine. It just says you’re not home (which I assume you are). Are you sure your ip and MAC address are correct in the script? Are you sure your wifi and Bluetooth are on?

Yeah, its running at the correct interval and everything but is switching back and forth between home and nothome. The wifi has been connected the entire time, bluetooth is on but not really connected to anything (probably shouldn’t matter for this anyway). I have triple checked the mac and ip addresses and they are both correct in the .sh file.

I know that this is a very “IT crowd” answer, but have you tried restarting your pi and your phone?

Sometimes thats the best answer…In this case it was partly correct. A reboot stopped the phone from going back and forth as away and home but about 12 hours later it started doing it again after actually returning from being gone. Any other thoughts?

Hi,
I had the same problem with my iphone 4s. It only changed when I enabled the phone to synchronise via wifi using i-tunes.
Maybe that helps?
However, in the end I changed to Geofency via vpn-on-demand installed on my iphone. See post

Cheers
Björn

Hi installed openHab 2.0 beta 2 and now everthing works ithout changing a thing.

Funny.
Thanks for all your help

Best regards

Ossy

Extra,

Thanks, I have tried the geofency app with some custom php scripts on one of my public web servers and I am pretty reliably getting home/nothome notifications. Actually works better than using the arp and ip scan on wifi. Will continue to see how things go.

Since some weeks i cant detect the iphone anymore. I cant resolve the Mac adreas anymore. It says incomplete… Anyone having sane issue?

Slightly improved the solution from @Extrabannies so i can use one script for all my devices by passing the dnsname and MAC of each device from the items configuration.
I also used ON/OFF switches so i can just hang them into the group of devices from the networkhealth sample -> https://github.com/openhab/openhab/wiki/Samples-Tricks#check-presence-by-detecting-wifi-phonestablets

items:
Switch MOB001_WLAN_N “Zu Hause [%s]” (Presence) { exec="<[/opt/openhab/iphonedetect.sh@@fon.fritz.box@@FF-FF-FF-FF-FF-FF:60000:REGEX((.?))]" }
Switch MOB002_WLAN_N “Zu Hause [%s]” (Presence) { exec="<[/opt/openhab/iphonedetect.sh@@otherfon.fritz.box@@EE-EE-EE-EE-EE-EE:60000:REGEX((.
?))]" }

sitemap:
Switch item=MOB001_WLAN_N icon=“present” valuecolor=[ON=“green”,OFF=“blue”]
Switch item=MOB002_WLAN_N icon=“present” valuecolor=[ON=“green”,OFF=“blue”]

script (/opt/openhab/iphonedetect.sh)
#!/bin/bash MAC=${2//-/:} declare -a DEVICES hping3 -2 -c 10 -p 5353 -i u1 $1 -q >/dev/null 2>&1 DEVICES=arp -an | awk '{print $4}' CHECK=$MAC if [[ ${DEVICES[*]} =~ $CHECK ]] then echo "ON" else echo "OFF" fi

Don’t forget the French commas :wink:

1 Like

hi sven,

how did you made it to use the “Switch” Items? So if you run your script the return is a “string”. Do you transform this?

best regards

I’ve tried to integrate this to my OH1 Installation (@Extrabannies solution). In principal it works, but I still have one question.

For testing, I executed the script via bash. Result was correct with “anwesend” / “on” (or whatever echo command will be defined) when my iPhone has Wifi turned on. So far, so good. When I turn Wifi off though, the result of the script doesn’t switch to abwesend/off immediately. When I execute the script a dozen times, the first results are always “anwesend” (wifi is off!). It seems to take some time until it switches to “abwesend”. Then it stays correctly at “abwesend”.

When I test the other direction (wifi off -> wifi on), the script immediately changes to “anwesend”, without any delay.

Do you guys also have this behavior?

Regards,
Stefan

Hi Stefan

i´ve the same behavior. In my case it´s the arp cache.
(wifi off -> wifi on), immediately changes to “anwesend”, no delay.
(wifi on -> wifi off), takes about 1-2 minutes to realise that it´s “abwesend”

If i clear the arp cache directly after (wifi on -> wifi off) itimmediately changes to “abwesend”, no delay
best regards

Ok, thanks! I am everything but a Linux expert, but isn’t it possible to integrate the arp cache cleaning command into the rule?

Hi,

not in the rule…but in the script :wink:. Here is mine

#!/bin/bash
declare -a DEVICES
sudo ip neigh flush 192.168.x.x
sudo /usr/sbin/hping3 -2 -c 10 -p 5353 -i u1 192.168.x.x -q >/dev/null 2>&1
DEVICES=/usr/sbin/arp -an | awk '{print $4}'
CHECK=“78:xx:xx:xx:xx:xx”
if [[ ${DEVICES[*]} =~ $CHECK ]]
then
echo “Anwesend”
else
echo “Abwesend”
fi

Its the “sudo ip neigh flush 192.168.x.x”, you can also use “sudo ip neigh flush all” or “sudo ip neigh flush eth0”
I prefer the first one, because it flushes only the entry with the defined IP Address.