iPhone Presence Detection with hping3 and ARP

Tags: #<Tag:0x00007fadfcaf6a20> #<Tag:0x00007fadfcaf6750> #<Tag:0x00007fadfcaf6318>

(Rich Koshak) #133

I don’t know how one would even approach iPhone detection using the http binding.

This uses a special command line too called hping3 to send a special type of pocket to wake up the phone then looks for the phone’s response to see is it is still on the network despite being asleep and not responding to pings.

The newest Network binding does this with arping so a separate script like this is no longer needed. There is also an iCloud binding that can get the phone’s location through Apple’s servers.

(Stefan Zieren) #134

Thanks for the good explanation - even for beginners
I implemented as you described and the script runs quite nice when executed from the command line.
But after setting up in openhab2 i always get the following error
2017-11-16 15:35:13.142 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule Find My iPhone: The argument ‘state’ must not be null or empty.

Hopefully any ideas?

(Rich Koshak) #135

Post your rule. Please use code fences:

code goes here

Also, be aware that the newest version of the Network Binding supports this through arping and if you have an iPhone there is a iCloud Binding, also available in the SNAPSHOT that works as well for iPhone presence detection.

(Stefan Zieren) #136

Maybe i should give it a try. Can you point me in the right direction how to use network binding or icloud binding instead?

(Stefan Zieren) #137

OK - I tried the example from the network binding page.
i manually added a thing, item and a sitemap. installed arping. everything fine - no errors.
But the outcome for all channels (online/lastseen/latency) is always “-”.
What am i doing wrong?


network:pingdevice:iphoneS [ hostname="" ]


Switch PhoneS { channel="network:pingdevice:iphoneS:online" }
Number PhoneSResponseTime { channel="network:pingdevice:iphoneS:latency" }
DateTime PhoneSlastseen { channel="network:pingdevice:iphoneS:lastseen" }


sitemap iphone label="Main Menu"
        Frame {
                Text item=PhoneS label="iPhone Stefan [%s]"
                Text item=PhoneSlastseen label="lastSeen [%s]"
                Text item=PhoneSResponseTime label="Response Time [%s]"

(Rich Koshak) #138

What version of OH are you running? I think you need to be on a SNAPSHOT past around 1050 or so to pick up those changes that support arping.

See the following for iCloud integration:

(Runar) #139

I have been working for hours now on my presence detection rule following the tutorial by @Maurits28 in the first post.
Ive got the script working and i even verified that it updates the correct item.
This is my modified version of the rule:

var Number SmartphoneCounter = 0
val Number MaxCounter = 18

rule "Execute script SmartphonePresence"
    Time cron "0 * * * * ?"
    var String Polling = executeCommandLine("sudo@@bash@@/opt/RunarMobil.sh", 5000)
	logInfo("Test", Polling)

rule "Determine presence of Runarmobil"
	Item RunarMobil received update
then {
	if(RunarMobil.state == "OFF") {
		SmartphoneCounter = SmartphoneCounter + 1
		if(SmartphoneCounter > MaxCounter) {
			SmartphoneCounter = 0	
			logInfo("RULE","Smartphone Counter reached threshold and sent command OFF. Counter set to 0")
		else {
			logInfo("RULE","Counter Smartphone threshold not yet reached. Counter set to " + SmartphoneCounter)
	else {
		SmartphoneCounter = 0
		logInfo("RULE","RunarMobil_Sw received command ON. Counter reset to 0")

And here is what the log shows:

2018-01-06 14:56:01.359 [INFO ] [.eclipse.smarthome.model.script.RULE] - RunarMobil_Sw received command ON. Counter reset to 0

2018-01-06 14:57:01.397 [INFO ] [.eclipse.smarthome.model.script.Test] - Smartphone is not present

==> /var/log/openhab2/events.log <==

2018-01-06 14:57:01.416 [ome.event.ItemCommandEvent] - Item 'RunarMobil_Sw' received command ON

==> /var/log/openhab2/openhab.log <==

2018-01-06 14:57:01.416 [INFO ] [.eclipse.smarthome.model.script.RULE] - RunarMobil_Sw received command ON. Counter reset to 0

2018-01-06 14:58:01.334 [INFO ] [.eclipse.smarthome.model.script.Test] - Smartphone is not present

==> /var/log/openhab2/events.log <==

2018-01-06 14:58:01.351 [ome.event.ItemCommandEvent] - Item 'RunarMobil_Sw' received command ON

==> /var/log/openhab2/openhab.log <==

2018-01-06 14:58:01.363 [INFO ] [.eclipse.smarthome.model.script.RULE] - RunarMobil_Sw received command ON. Counter reset to 0

2018-01-06 14:59:01.341 [INFO ] [.eclipse.smarthome.model.script.Test] - Smartphone is not present

==> /var/log/openhab2/events.log <==

2018-01-06 14:59:01.364 [ome.event.ItemCommandEvent] - Item 'RunarMobil_Sw' received command ON

==> /var/log/openhab2/openhab.log <==

2018-01-06 14:59:01.377 [INFO ] [.eclipse.smarthome.model.script.RULE] - RunarMobil_Sw received command ON. Counter reset to 0

2018-01-06 15:00:01.342 [INFO ] [.eclipse.smarthome.model.script.Test] - Smartphone is not present

==> /var/log/openhab2/events.log <==

2018-01-06 15:00:01.373 [ome.event.ItemCommandEvent] - Item 'RunarMobil_Sw' received command ON

==> /var/log/openhab2/openhab.log <==

2018-01-06 15:00:01.372 [INFO ] [.eclipse.smarthome.model.script.RULE] - RunarMobil_Sw received command ON. Counter reset to 0

2018-01-06 15:01:01.362 [INFO ] [.eclipse.smarthome.model.script.Test] - Smartphone is not present

==> /var/log/openhab2/events.log <==

2018-01-06 15:01:01.385 [ome.event.ItemCommandEvent] - Item 'RunarMobil_Sw' received command ON

==> /var/log/openhab2/openhab.log <==

2018-01-06 15:01:01.386 [INFO ] [.eclipse.smarthome.model.script.RULE] - RunarMobil_Sw received command ON. Counter reset to 0

2018-01-06 15:02:01.387 [INFO ] [.eclipse.smarthome.model.script.Test] - Smartphone is not present

2018-01-06 15:02:01.407 [INFO ] [.eclipse.smarthome.model.script.RULE] - RunarMobil_Sw received command ON. Counter reset to 0

==> /var/log/openhab2/events.log <==

2018-01-06 15:02:01.407 [ome.event.ItemCommandEvent] - Item 'RunarMobil_Sw' received command ON

To me it looks like the rule “Execute script SmartphonePresence” in combination with the script does what its supposed to do. Im wondering if someone with the right expertise can spot the problem. I’ve spent hours upon hours now looking for the problem but cannot figure this out. My prime suspect is those curly brackets hanging around inside the rule and make no sense to me.

(Runar) #140

Looked up the item receiving the status update from the script using Openhab Rest API - http://openhabianpi:8080/doc/index.html#!/items/getPlainItemState
Seems like we have found a clue on what’s going on we my failing presence detection. :slight_smile:
This is the response:

Response Body
Smartphone is not present
Response Code
Response Headers
  "content-length": "36",
  "server": "Jetty(9.3.22.v20171030)",
  "content-type": "text/plain"

wtf? I was expecting a ON or OFF value here… If this is the string output i clearly cannot use this item in the if-statement in my rule:

if(RunarMobil.state == "OFF")

I’m wondering if im supposed to use a second item (the device item) in this part of the rule?

(Rich Koshak) #141

If you are using Seaside’s script it clearly echos

echo "A's iPhone is ${statusMessage}"

You need to change the echos as appropriate for what you want.

Are you aware that this script is basically built into the Network binding now?

(Runar) #142

Thank you for trying to help me.
No. As i stated i was following the tutorial in the very first post by @Maurits28. If you think you see the problem i need a guiding hand here.

Edit: Last Q - No i was not aware of that. please provide a link where i can read me up on the topic? :slight_smile:

(Rich Koshak) #143

You need to be on 2.2 release. See the readme for the Network binding. All the stuff talking about arping is basically talking about the binding doing what this script does.


(Runar) #144

Ah!!! Totally missed this. I know i’ve read this before and have actually been taking advantage of this without even realizing that i was. I’m on Openhabian 1.4 and i assume this “arping service” was included in the bundle. As a novice on Raspberry Pi and its operating system i find myself lost all the time in the jungle of different solutions and software-versions. This was one of these moments. Spent several hours trying to get this to work…

If i understand this correct i need to write me a rule that take into consideration “flapping” to avoid unintended action triggered and maybee use the DHCP request from the Android phone as a “reentry trigger” in my presence rule.
Thanx for helping me out Rich !

(Runar) #145

One last quick question. Im planning to follow this example: Generic Presence Detection
I’m not barking up the wrong tree here again? :smile:
It seems clean and simple so i think i’ll be able to work that solution out on my own.

(Rich Koshak) #146

I’ve used that for years now and it still works fine. It is what I would recommend for flapping and in case you ever decide to add more sensors for each person.

(ufa) #147

I use Thomas arping
Where do i config the arping parameters in network binding ??

(Rich Koshak) #148


(ufa) #149

did it work for you ?
my presecence detection is not satisfiying

(Rich Koshak) #150

I run in Docker and don’t want to run OH as root so I don’t use the arping feature. I have an external python script that executes a version of the above script for me. Others have reported great success with the arping feature of the Network binding.

Cell Phone State keeps going to OFF
(Victor Clausson) #151

I’m new to openHAB2 and just want to know if this method in the first post is active or has been replaced with something else since?

I’m currently having problem with my iPhones wifi connection dropping in and out.

(Rich Koshak) #152

The Network binding now will do arping so you can just use that. Look at the readme for the binding for details.

There is also an iCloud binding people have been successful using.