[SOLVED] Presence with Android after using Iphone

Hi people,

I have changed my phone to an Android (Galazy s9+). I came from Iphones where my presence system was wroking great. I use arping + switch in the main door + push notification through myopenhab.

Every time door was open, I send a “push” notification to my phone through MyOpenhab and inmidietly the arping code to detect traffic and was working pretty fine!!

I THOUGHT that on Android was going to work fine too but not. After send a push notification and the arping code, sometimes give me back presence, sometimes gives me NO presence. :confused: … It seems that the Android is more on deep sleep than the Iphone.

  1. Which presence systems do you use with Android devices?
  2. Is there some specials setup on Android? I have setup ON wifi when screen is locked (on Wifi Advanced settings)
  3. Any other ideas? Is there specefic binding for Android? I do not like Owntrack, I tried but I did not like, I rather arping or similars solution…

More information after some tests:

With the Android phone unlocked, If I release the Arping, sometimes I get ON presence sometimes OFF presence, so clearly, the arping is not working for Android. At least with Iphone if I had unlocked the phone, ALWAY I got presence, the arping worked fine, but now NOT.

My code in rules file

         sendBroadcastNotification("Door Open")
           Thread::sleep(300000) // delay 1s
           var String IphoneExec = executeCommandLine("sudo@@bash@@/opt/IphoneD.sh", 5000)
           Iphone.postUpdate(IphoneExec)
           Thread::sleep(1000) // delay 1s
                if(Iphone.state == "ON")
                {
                Prsence.sendCommand(ON)
                }
                else
                {
                Presence.sendCommand(OFF)
                }

The code of IphoneD.sh


#!/bin/bash

#Enter your ip of your device here
DEVICES="192.168.2.120"

for i in `echo $DEVICES`; do
    # Change dev and eth0 if needed
    ip neigh flush dev eth0 $i
    hping3 -2 -c 100 -p 5353 -i u1 $i -q >/dev/null 2>&1
    sleep 1
    # Only arp specific device, grep for a mac-address
    status=`arp -an $i | awk '{print $4}' | grep "..:..:..:..:..:.."`
    statusMessage="OFF"
    #A mac will be 17 characters including the ":"
    if [ ${#status} -eq 17 ]; then
        echo "ON"
        statusMessage="ON"
    else
        echo "OFF"
        statusMessage="OFF"

    fi
done

This Thread::Sleep for 5 minutes is a bad idea…
See:

Replace with a timer:

var Timer timer = null


rule "xxx"

    sendBroadcastNotification("Door Open")
    timer = createTimer(now,plusMinutes(5), [ |
        var String IphoneExec = executeCommandLine("sudo@@bash@@/opt/IphoneD.sh", 5000)
        Iphone.postUpdate(IphoneExec)
        Presence.sendCommand(IphoneExec)
    ])

BUT for detecting my android phone presence I simply use the network binding
Install it through the paper UI
The binding will scan your network and detect the phones. Make sure they are online!!
Select the thingcorresponding to your phone IP and link the item to the channel:

No need for a script or exec binding!

1 Like

Note, you will want to use arping with network binding to detect iPhones when they go to sleep so pay attention to that part when setting it up. It basically does exactly the same thing as your script. There is also the iCloud binding a lot of people like using for this.

Like Vincent said, a 5 minute six second (plus the 5 seconds for the script to run and one second after the script runs) is a really bad idea.

1 Like

Thank boths. Thank you for the tips. I will change the 5 minutes syntax by the Timer but first I will try the binding. I remember that I tried that binding at the very begining with the iphone but I dont remember but something didn’t work for me because when iphone went to sleep, the binding gave me OFF and that the reason because I tried other way.

I added 5 minutes like this because I didn’t know about Timer, and the idea is that whenI leave the flat, after 5 minutes, the rules does not detect me in the flat.

I will try the binding first how is working with Android.

Thank you again for the answer.

Have you tried IFTTT app. It can send commands to OH when Android events such as ur wifi home network connected / in range / out of range.
Not sure about fees and privacy policy of the app though. Also you need internet, I am not sure IFTTT and OH teams have yet worked out a local-intranet discovery of each other.

Or maybe since presence is such a common requirement, the OH Android app when connected locally to OH server, there should be an event published on the bus and a periodic event published to ensure phone is still there. The OH App is anyway keeping a tcp conn wt OH server.

You mean that I do not need any rule or script to detect the phone, only using the binding is enough?

Something is going wrong because, I have setup the binding, I have added the IP of my phone, then I added a switch on Channel ONLINE. But I have tried to connect and disconnect the phone from the network.

  • When I disconnect the phone from the network, works fine. Binding works fine, switch turns OFF.
  • When I connect to the wifi again, the binding does not detect the phone, and switch does not change.
  • When phone is sleeping, binding detect no phone just 1 minutes after connection, Switch turns OFF.

The problem migth be the age of the used Android Phone. I had the same behaviour as @vzorglub discridescribed, changing to a newer one resulted in your observed behaviour.

Try options in that binding. External tool selection etc. I haven’t tried it myself though.

Pfff, I havent tried that.

My idea is that after door was open, release a presence rule just 3-5 minutes after door was open, and check only ONCE the phone presence, if I am gone is enough that phone was disconnected, if I am at home, I am connected. So I sent a push notification to my iphone with MyOH and that was enough to wake up my phone, and detect presence and changed the switch. I do not why, this is not working on Android Galaxy S9+, when I send push notification on Android, it has not detected as quick as with iphone was. I do not know why.

With the network binding. Works fine when goes OFF or go to sleep, but not when goes ON. :confused:
I do not even know how to “release” the binding only when door is open. Like this I only make work the binding on some events.

Might be an issue with binding. Maintainer @davidgraeff (https://github.com/davidgraeff) might give you a clue.

Thank you.

It is strange, when goes OFF works fine, but it does not detect when goes ON. I have my phone unlocked 2 minutes, so the phone is conected to the wifi and, should be enough with the default binding setup, to detect presence, I guess.

Try enabling allowSystemPings in Thing config. Try setting up arping tool on your OH server, if not already done.

Where I find that?

I have installed arping tools, and nothing change. The binding for me is not working properly.

In PaperUI, you can edit thing configuration. There should be a switch for allowSystemPings.

Actually it binding level parm. I thought its thing level.
For binding level parms you need to create .cfg file as USERDATA/etc/binding.network.cfg. USERDATA is /var/lib/openhab2/etc/ for apt based installation.

You can set binding parms like this in that .cfg file:

allowSystemPings=true
allowDHCPlisten=false
arpPingToolPath=/usr/sbin/arping
cacheDeviceStateTimeInMS=2000

Ok i saw the parm in Bindings and everythings is setup as you quoted. :confused:

I dont know, something is not working properly.

Did you assign a fixed IP to your phone?
When the network binding scans the network, it will find several services on your phone IP. Try another one.

Yes, from my router I have assigned a fixed IP.

It is strange because from INBOX in OH Paper UI, if I click on + in order to see if OH find the phone device IP. It does not show the phone IP.

I will try another one.

Is this right that I do not see my phone IP here in the inbox? I should see it, right?

I have read the doc again, and it seems I have setup everything fine, but OH does not see my device connected. It is not reachable. I have checked that WIFI is enable during standby. So, the problem is over there, OH does not see my device conected to my wifi, that the reason because when binding goes OFF works fine, but not when trying to go ON, because OH does not detect the phone. why?

During the scan, phone is not detected. I can see other devices but not the phone. :confused: