BEGINNER Guide to iPhone Presence?

Hey guys, interesting discussion you have going on here. :slight_smile:

With the new docs still being partially incomplete, I can understand that not all (OH2) users are going with it. I do not miss to annoy users with a link to them ^^

I’m sadly a bit behind in the ambition to make the forum a bit more guided. As the two of you already discussed, the forum can definitely need some guidance in the individual categories. I’ll very soon start creating pinned posts that contain a description what the category is for (and not!), important links and an FAQ section.

I would also like to establish some kind of competence awareness among the moderators, allowing them to invite the right people to threads they personally can not help with.

The Tutorials and Examples is especially important to me, as they will considerably help new users getting started and understanding concepts and possibilities with OH(2). Judging by questions often asked regarding rules and sitemaps, this is definitely an area we should improve in.

As @rlkoshak pointed out, that’s a problem because (a) openHAB is available for all platforms, (b) at least for the next 3-5 months we are still dealing with openHAB1.8 stable and openHAB2 beta setups and © I wouldn’t say that the manually installed setup is any less functional - it’s just a question of how good the system is managed. (That said, I would recommend an apt installation.)

Earlier you discussed, that it would be nice to give more guidance to unexperienced users, especially in the first forum response. In my personal experience, the more you take people by their hand and show them the way, the less they look to the left and right… It is very important to give helpful and complete answers but in the same moment empower the user to find the solution by himself. Making a few assumptions (like with @Max_G 's XYZ example), giving some advice and posting relevant links is probably a good practice.

Regarding linux, I wanted to point out to the links I included here and here (right beneath TOC). If something like this found its way in the FAQs, we should starting to reference that section instead.

In general, you should start of with a short description, pros and cons of your solution, requirements and a screenshot. Then describe the setup process. Remember links for when things go south. The last thing I wanted to write in the sticky is that it would be great if the creator of the first post would incorporate things discussed in the comments (improvements, alternatives, potential problems)…

Related to the presence topic, I think it would be good to also reference the already discussed alternatives and give comparison as far as you can. That would be the first question I myself would have regarding that topic which seems to be solvable in 10 different ways.

Procedurally just create a new posting in the Tutorials and Examples section. Use Thom’s Influx+Grafana Tutorial and an example to follow. And reference this thread and the original thread. You might also come back to both of these threads and add a posting pointing to the new tutorial.

Once written, this tutorial will be the authoritative source for how to do it which we should all reference when iPhone presence detection comes up.

Yes, I have an RPi3 running the Jessie version of Raspbian.

Yes, I have OpenHAB installed and it starts on boot up. I have the Hue bindings installed and can turn on/off lights as well as set their color. I also have the bindings below installed (all version 1.8.3):

astro, exec, harmonyhub, http, hue, ntp, weather, zwave

The below io:
hamin (v. 1.7.0)
myopenhab

The persistence below:
logging
rrd4j

OK, I understand.

Yep, I have a rule to update the last weather update time.

I have it begin on startup. I have Samba and Designer installed on my Win10 computer and will use putty/designer/Samba to execute commands, modify OpenHAB files and save/move files.

OK, I ran this and now get the error below:

-bash: /etc/openhab/configurations/scripts/iPhoneDetect.script: /bin/bash^M: bad interpreter: No such file or directory

It would be very helpful to have a tutorial! I always compare learning OpenHAB to cooking. You can have a step that says “chop 1 C of onion”, but if you don’t know what an onion is or where to buy one, you’re gonna have a hard time! :slight_smile:

That being said, I’ve found the multiple people on this forum MORE than willing to try to help a girl who is just trying to move from Vera to OpenHAB.

Thanks!

That bash error is an unfortunate but common problem. On Windows newlines are represented with two characters “\r\n”. On Linux/Unix/OSX only one character is used: “\n”. A lot of times this difference doesn’t matter but this is one of the cases where it does matter.

You can run dos2unix on the script and it will strip the “\r” (seen as “^M”) characters out of your file and bash should no longer have any problems with it.

dos2unix iPhoneDetect.script

If you intend on editing files on Windows using notepad this will be a perennial problem. I recommend getting Notepad++ instead of using the built in Notepad program, or edit the files on the Raspberry Pi itself using nano.

This is just a quibble and not really relevant to anything functional, but by convention a shell script such as this typically ends in “.sh”, not “.script”.

Your response is a bit of a non-sequitur. The primary point I want to make sure you understand is that when you putty to your Pi, you are interacting with Raspbian, not openHAB. So any commands you issue must be in a format that Raspbian (really bash) understands. See the following link for a bit on how to work on the command line.

Some important commands you will want to know when working with openHAB include:

  • tail -f /var/log/openhab/openhab.log : lets you see what openHAB is logging as it is running
  • cd : change directory
  • ls : get a list of files
  • ls -a : show even hidden files and directories (i.e. files that start with “.”)
  • man <cmd> : where <cmd> is the name of a command you want to run (e.g. ls), shows the usage/help information for that command. Use man to learn how to use the commands that follow
  • chmod : change the permissions on a file
  • sudo : run the command that follows as root
  • systemctl : start/stop a running service, e.g. sudo systemctl restart openhab
  • cp : copy files
  • nano : command line text editor
  • dos2unix : strips the ^M characters from a text file produced on Windows
1 Like

I’m using the REST API in combination with the MAKER plugin at IFTTT and it works fine for me. Make sure to have some firewall policy on your device :wink: The following screenshot shows an example, which works for me:

No, I use Designer to edit any OpenHAB files since it will show line errors in the file, but I did have to create this one using Notepad. I do have Notepad++, so I’ll use it in the future. I recreated the script in Nano and can view it in Designer with no errors. When I run

/etc/openhab/configurations/scripts/iPhoneDetect.sh

I get:

-bash: /etc/openhab/configurations/scripts/iPhoneDetect.sh: Permission denied

I tried running it as “sudo /etc/openhab/configurations/scripts/iPhoneDetect.sh”

and I get the error “command not found”. I went to the exact directory to verify the script is there and still get the same error:

pi@raspberrypi:/etc/openhab/configurations/scripts $ ls
demo.script  iPhoneDetect.original  iPhoneDetect.sh  README
pi@raspberrypi:/etc/openhab/configurations/scripts $ iphoneDetect.sh
-bash: iphoneDetect.sh: command not found

Glad it works for you! I would try it, but as I said in my original post, I couldn’t get any items to show up in IFTTT and searching through the forums didn’t have a single post with the solution.

Please try a

ls -l

in your script directory and post the result here. We have so see if your script is executable (that was maybe the reason for the “permission denied”). Furthermore you made a second try starting the script with “iphoneDetect.sh”, but the correct name is “iPhoneDetect.sh” (with a capital P). Maybe that’s the reason why you became a “command not found”.

You can try to execute the script (while you are in the correct directory) with: ./iPhoneDetect.sh

I have a working iPhone detection using the exec-Binding and a Bash script, just like you want to do it. It was mainly a matter of permissions and user rights to get this working. But as Rich already mentioned, this is clearly no real beginner stuff. You should at first get a little more comfortable with Linux as an OS.

results of ls -l:

pi@raspberrypi:/etc/openhab/configurations/scripts $ ls -l
total 16
-rw-r--r-- 1 openhab openhab  36 May 22 17:24 demo.script
-rw-r--r-- 1 openhab openhab 245 Sep 17 11:57 iPhoneDetect.original
-rw-r--r-- 1 openhab openhab 231 Sep 19 08:27 iPhoneDetect.sh
-rw-r--r-- 1 openhab openhab 230 May 22 15:44 README

When I try to run ./iPhoneDetect.sh:

pi@raspberrypi:/etc/openhab/configurations/scripts $ ./iPhoneDetect.sh
-bash: ./iPhoneDetect.sh: Permission denied

Ok. We’ll getting there. :wink:

Your scripts aren’t executable. You have to have an “x” before the “rw” and ideally also before the second and third “r”.
Try

chmod 777 iPhoneDetect.sh

It isn’t the perfect solution, but with this command we can rule out some other problems.
Then try again to execute the script. Maybe do another ls - l before to verify that the “x” have been set.

I had to use sudo chmod 777 iPhoneDetect.sh, it looks like it worked?

-rwxrwxrwx 1 openhab openhab 231 Sep 19 08:27 iPhoneDetect.sh

When I ran the script it returned “Absent” - which is progress!!

How do I permanently set the permissions on the script?

They are permanently set. The difference is: When you manually start the script (like you just did), you execute it as User “pi”. When Openhab executes the script with the exec binding, it executes the script with the user “openhab”. We have to make sure that this is allowed (which we did with chmod 777).

Next step: Please quote the current content of your iPhoneDetect.sh script. I want to compare it with my script (but I can’t do this until this evening when I am at home). Try to get this with using nano on your ssh shell (type: nano iPhoneDetect.sh).

Furthermore we have to take care that the included commands in the script (like hping3) also are allowed to be executed by the user openhab. We have to edit the /etc/sudoers file for this. But one step at a time…

You see: No rocket science, but quite a bunch of stuff to consider…

#!/bin/bash
declare -a DEVICES
hping3 -2 -c 10 -p 5353 -i u1 xxx.xx.x.xxx -q >/dev/null 2>&1
DEVICES=`arp -an | awk '{print $4}'`
CHECK="my:mac:addr"
if [[ $DEVIES[*]} =~ $CHECK ]];then
	echo "Present"
else
	echo "Absent"
fi

xxx.xx.x.xxx is my iPhone’s IP address connected to my home wifi
my:mac:addr is my iPhone’s mac address when connected to my home wifi

Next step: Have you already installed hping3 like Max said:

Try it with simply executing the command hping3 on your ssh shell. It makes no sense but you can see if the program is installed. Otherwise install it with sudo apt-get install hping3.

And check if your mac address in the script is written in lower cases (aa:bb:cc instead of AA:BB:CC). For me it didn’t work with upper cases.

Next step (as mentioned before): Edit the /etc/sudoers file. Execute the following command on your shell:

sudo visudo

Now you have started an editor and opened the sudoers file. Scroll down to the very end of the file and add the following lines:

pi ALL=(ALL) NOPASSWD: ALL
openhab ALL = NOPASSWD: /usr/sbin/hping3
openhab ALL = NOPASSWD: /usr/sbin/arp

This allows the programs of the script to be executed by the user openhab. Save your changes and exit the editor.
CAUTION: This is a sensible file. Don’t mess up the file or modify/delete any of the other content.

Checking your script I found one (maybe two) problems:

  1. There is a typo. You wrote “DEVIES” in line 6. It has to be “DEVICES” (like in line 2 or 4). Don’t know where this typo comes from as Rich has written it correctly. Anyway…

  2. This could be unnecessary, but I have an additional “sudo” before hping3 (sudo hping3 -2 -c …).

Other than that, the script seems to be identical to mine. With the above mentioned extra steps this should lead to a correct output now.

OK, done.

Done.

Modified to lower case.

Corrected.

Done.

I rebooted my Pi after this and ran the script again and I got Permissions denied again. I ran the ls -l and saw the x’s were missing from the script so I knew the permissions were incorrect again. I ran the sudo chmod and then ran the script manually and got “present”!

I also checked the app and “To House” now states “Present” as well.

Sorry, my fault. As Rich mentioned, the file permissions in the folder you used for the script are resetted with every boot. This folder is not intended to be used for other files.

So move over the file to another directory (Max suggested one, mine are in /scripts). So make a directory with:

mkdir /scripts

Copy your sh script to this folder (I won’t describe how to, just google and learn some linux commands :wink: ) and check the permissions one more time. Adapt your item configuration to the new path and test again.

And then disable wifi on your phone and check if the state changes to absent. The script only executes once a minute, so the change is not visible immediately.

Awesome, thanks a million for everyone’s help!!

Edit: final tutorial can be found here: Tutorial

Just an overview of what the outcome is of the above posts and some other posts on the forum.

To enable Iphone detection in openHAB 1.8.3 follow these steps (on RPI2):

  1. Make a directory to store scripts (if you don’t have this yet)
sudo mkdir /home/pi/scripts
  1. Create a file IphoneD.sh in this directory
sudo nano IphoneD.sh

with the following code:

#!/bin/bash
declare -a DEVICES
sudo ip neigh flush all
sudo hping3 -2 -c 10 -p 5353 -i u1 192.168.xxx.xxx -q >/dev/null 2>&1
sleep 1
DEVICES=`arp -an | awk '{print $4}'`
CHECK="xx:xx:xx:xx:xx:xx"
if [[ $DEVICES[*]} =~ $CHECK ]];then
        echo "Present"
else
        echo "Away"
fi

Make sure you fill in your own local IP address of your Iphone as well as your MAC address (replace xx.xx.xx… in row 6). Make sure to use all lowercase for your MAC address.
Save and close the file.

  1. Now you need to change the permissions:
sudo chown 777 IphoneD.sh

Now check with:

ls -all

if the script is executable.

  1. Install hping3:
sudo apt-get install hping3
  1. Make a rule to check from time to time presence (every minute in this example):

As an example I made an item that shows the result of my rule:

String Iphone "John Doe @ Home? [%s]" 

This item is updated by this rule, trigger by a cron job (link for details: Cron Trigger tutorial

rule "IphonePresence"
when
    Time cron "0 0/1 * * * ?" 
then 
    var String IphoneExec = executeCommandLine("/home/pi/scripts/IphoneD.sh", 10000)
    if(Iphone.state != IphoneExec) {
        Iphone.postUpdate(IphoneExec)
    }
end
  1. Edit sudoers file with
sudo visudo

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

openhab ALL=(ALL) NOPASSWD: ALL
1 Like

Nice and concise tutorial on how to make hping3 work. Thank you for posting.

I might make one suggestion to add a sleep between the hping3 and the arp call in IphoneD.sh. I’ve found on my machine at least that the script runs faster than arp packets fly across the network causing the script to only intermittently correctly report Present. Adding a sleep 1 in my case (much less would probably be adequate) makes it return the correct state every time.