Groan, not another iPhone presence detection tutorial.
OK, here is the deal for why I wrote this.
-
I run OH in a docker container and I do not want to run it as root. Therefore I cannot use the dhcplisten nor the new arpping features of the Network binding.
-
I run in an unmodified docker container so access to commands like hping3 are not available to the Exec binding.
-
I’d rather use a solution that doesn’t depend on a script running on the router at this time.
-
This was a quicker and easier way to use than interacting with the icloud interface discussed here. Plus I only care about whether the device is home, my wife would find it creepy if I were to track her location everywhere.
Because of 1 and 2, I need a way to execute things I would normally run using the Exec binding some other way. I already have my sensorReporter script running on three Raspberry Pis and counting and this script already communicates using MQTT and I’ve already written a module for it to execute command line scripts. So the following approach was a no-brainer.
This posting does not go into detail about how to install and use sensorReporter. Ask questions below if you run into trouble.
This posting also assumes your router or phone has been configured with a static IP.
Approach
Configure sensorReporter with an execActuator config that executes the script by @rtvb in this posting. I made one modification to include ${IP} in the echo of the result (i.e. replace echo "OFF"
with echo "${IP} OFF"
and the same for echo ON). So, for example, if the IP passed in a 192.168.1.100 then the script will output 192.168.1.100 ON
. More on why in a moment.
Then configure a String Item in OH to send the IP address and MAC you want to ping as an MQTT message. Configure another Switch Item to subscribe for the results for that IP. You need only the one outgoing Item but a separate incoming Switch for each device you care about.
sensorReporter
To configure the exeActuator use a config like below.
[Actuator1]
Class: execActuator.execActuator
Type: Exec
Connection: MQTT
Poll: 0
Command: ./iphoned.sh
CMDTopic: scripts/presence/iphone/cmd
ResultTopic: scripts/presence/iphone/results
Use your own CMD and Result topics and the above assumes the iphoned.sh script discussed above is in the same folder sensorReporter is run from. Provide a full path if it is located somewhere else.
Note that sensorReporter needs to either run as root or you need to add a sudo to the Command and add what ever user sensorReporter is running as to sudoers so it doesn’t need a password to run this script.
openHAB
Below uses the presence detection example documented here.
Items:
String aHping3 "Ask sensorReporter to hping3 and arp ping device [%s]"
<present> (gPresent)
{ mqtt=">[mosquitto:scripts/presence/iphone/cmd:command:*:default]" }
Switch viPhone_Net "iPhone Presence"
<present> (gPresent)
{ mqtt="<[mosquitto:scripts/presence/iphone/results:command:REGEX(192.168.1.100 (.*)):192.168.1.100 .*]" }
NOTE: the aHping3 Item could be done away with and replaced with a call using the MQTT Action. I had problems with the Action a long time ago and never had a chance to reinstall it.
The magic happens in the transform and matching regex of the second Item. The transform will extract the ON or OFF from the message that starts with the IP address we care about. The filter regex causes the binding to only try to transform those messages that start with the IP address we care about which avoids errors in the logs and the Switch from going crazy with messages for other IP addresses.
If you have more than one device, create more Switch Items and change the two regular expressions to match those other device’s IPs.
Rules:
rule "Ask sensorReporter to hping Jenn's iPhone"
when
Time cron "0 * * * * ? *"
then
aHping3.sendCommand("192.168.1.103 68:db:ca:a4:4c:98")
end
In this case, we are sending the IP address and MAC which gets passed to iphoned.sh. If you have more than one iPhone or other device type you want to hping3 detect you need to send that IP/MAC pair as separate commands.
There are tons of other ways to issue the command to poll for the device but this way was convenient.
I hope this provides some inspiration for those running OH in a Docker or are otherwise constrained to have OH do network operations or use the Exec binding.