[SOLVED] Presence detection with iCloud binding

I have the iCloud binding running, including fetching the location. Next step would be to get presence detection working in a generic way.

I was thinking to compare the location of my iPhone with the location of the openHAB location (which can be set in the system settings).

Is there already a way to determine the distance between any location and the ‘home’ location? Or should I build some kind of rule myself?

Any suggestions are appreciated.

If I get a working solution I will post back the results

1 Like

There is an Item type Location and it has a distanceFrom method. So if you have your home location set in a Location Item and the phone location in a Location Item its as simple as:

if(Phone.distanceFrom(Home) as Number > 100)

I do not know whether the location built into OH is available to be used to populate an Item, but you can use Design Pattern: Encoding and Accessing Values in Rules to create and initialize one.

I’m using a rule to do what @rlkoshak mentioned. Please see below

/***********************************************************************************
 
 Rule - Distance from home
	 
 This rule is triggered by an updated location item (Martin_Location). The location 
 item holds the coordinates to my iPhone and is updated by the Bridge/Thing in an 
 iCloud binding. The rule updates an item (Distance_From_Home) by calculating the 
 differene between two locations (a static HOME location and Martin_Location)
 *************************************************************************************
 
 Pre-requisites
 ***************************************************
 1. An .items file with the following content

 Location Home_Location 
 Number Distance_From_Home

2. A configured Bridge/Thing using the iCloud binding with a location item (in this case Martin_Location)
*****************************************************/

rule "Distance from home"

when
	Item Martin_Location changed
then


// ***************************************************************************
//  Set the location for HOME (tip - use google maps to find the coordinates).
// *************************************************************************** 

 // The Norwegian Royal Palace (Lat, Lon)... 
 Home_Location.state = new PointType(new DecimalType(59.916940), new DecimalType(10.726740))

// ******************************************************************************
// Calculate the distance from HOME (Home_Location) and iCloud (Martin_Location)
// ******************************************************************************

 Distance_From_Home.postUpdate(Home_Location.distanceFrom(Martin_Location))

// Optional logging to /var/log/openhab2/openhab2.log
 logInfo("Distance from home : ", Distance_From_Home.state.toString())
	
end
3 Likes

Note to self:

This thread has some nice examples:

Hi i copy and paste and i think its working

but DistanceFromHome changed from 37.59414801918072 to 49.70985558406823

i was expcting some number value , can you tell me if i did somthing wrong
or i need to take this and calc this some how?
or maybe add some import ? using OH2.2

rule “Distance from home”

when
Item BatmenVsGod_Location changed
then
infoHomeLocation.state = new PointType(new DecimalType(31.274104), new DecimalType(34.809167))
infoDistanceFromHome.postUpdate(infoHomeLocation.distanceFrom(BatmenVsGod_Location))
logInfo("Distance from home : ", infoDistanceFromHome.state.toString())
logInfo("home : ", infoHomeLocation.state.toString())
end

igonire all that its working and its great!!!

I would be interested to know what your battery life is like, I also used this method to determine presence, but I found it killed both my wife’s and my own battery life

when it was configured i will admit it worked like a dream.

now playing with Wifi and bluetooth BLE presence.

sure let me test it :slight_smile:
give me some time

@deadnewbie what method did you try going with?

I tried Wifi but found the UDP port 5353 / ARP presence detection as being too unreliable. Even when setting the ping counts to a higher number. I was thinking about trying bluetooth, but I figured I would rather try iCloud or Owntracks first.

Curious what you decided on.

also i cloud is no champ Presence detection
any luck with BLE? @Deadnewbie

BTLE with iPhones and some Android won’t work because the phones frequently spoof their own Mac addresses. You can know that an iPhone is present but not which specific iPhine is present because the MAC address keeps changing.

thanks Rich…

what are you using if using at all?

i think some linux distro like(pfsense,ipfire) will be best fit?

I’m currently using the hping3/arping script discussed in iPhone Presence Detection with hping3 and ARP and the execSensor plug-in for sensorReporter which works very well. Better than any of the BT detection techniques I’ve tried.

As of OH 2.2 the Network binding added arping as an option which can replace the above except that I’m running OH in Docker and arping is not available. Or at least it was unavailable as of a couple of days ago. It is now present so the next time I upgrade I’ll probably drop the script and just use the Network binding with the arping option.

So far it is the most accurate phone detection I’ve run.

Like I said above, BTLE doesn’t work for iPhones and newer Androids because they spoof their BT MAC address so you can’t detect a specific iPhone is present, just that there is an iPhone present. This takes out reelyActive and other BTLE based scanning approaches. I also used just regular BT scanning using RSSI but I never was able to tune it well enough to become accurate enough to rely upon. I’d have three sensors (RPis) throughout the house and invariably one would get stuck ON and the other would get stuck OFF and I never could figure out why.

If you have something like a Fitbit or a BTLE token or the like that would work because they don’t spoof their MAC addresses. But for iPhone the best I’ve seen is Network binding with arping or the iCloud binding.

I do run pfSense and I see nothing there that would help. Some have managed to get some scripts running on DD-WRT that publishes when devices enter or exit the wifi network but I’m not sure that it works well when the iPhone goes to sleep. But these all operate at the IP level and would provide nothing to aid with BT detection.

1 Like

Is there a way to disable / enable the iCloud THING or BRIDGE, using a rule, in order to save the iPhones’ batteries?

In my Vera controller, I was able to turn on the iCloud plugin only when the home door was opened. I then left the plugin check everyone’s phone locations for about 10 minutes (refreshing every 1 minute).

The assumption was that it won’t take more than 10 minutes to get away from home (when leaving it), and once the controller “knows” for everyone whether he/she is at home or away, no one can change his status without opening the door.

This worked well, and prevented from the iCloud service working non-stop and using the battery.

Is there any way to do a similar thing with the OH iCloud Binding?