Monitoring device location with OpenHAB?

Hi Friends,

I’m running OpenHAB 4.2 and I wanted to ask if it’s possible to monitor when an iPhone is at home or away?

It can be by the phone being connected to the local wifi network, or by the phone’s GPS location, or by any other method that will allow OpenHAB to distinguish when my phone is home or away.

I would like to use this information to disable certain rules when I’m home, or to enable them when I’m out.

Hope someone can help :folded_hands:

Thers’s several topics on this issue. Try to search for presence rules or so. Depending on which wifi yoy have you could setup a netwotkbinding ping item that pings how often you need to sense if your home or not.

This eill require static ip on your wifi device and that could be tricky with some smartphones.

1 Like

Thank you very much for the tip, Marcus :folded_hands::folded_hands:
Your suggestion was very easy to implement :blush:

does it also work if the iPhone is in sleep mode?

alternative you could use the iCloud plugin and check distance.

1 Like

Hi @emiel , thank you for the iCloud binding suggestion. I will try this one also :slight_smile: :folded_hands:
The ping method works when the iPhone is in sleep mode, but I had to disable Settings->Cellular->Wi-Fi Assist on the iPhone for it to work without interruptions.

Hi @emiel ,
Can you please explain how I should define my home_location for the rule they give in the iCloud binding documentation to work?

rule "iPhone Home"
when
    Item iPhone_Location changed
then
    // specify your home location
    val PointType home_location  = new PointType(new DecimalType(51.0), new DecimalType(4.0))
    val PointType phone_location = iPhone_Location.state as PointType
    val int distance = phone_location.distanceFrom(home_location).intValue()
    // specify your preferred radius (in meters)
    if (distance < 200) {
        iPhone_Home.postUpdate(ON)
        logInfo("iPhone Home", "iPhone is at home.")
    } else {
        iPhone_Home.postUpdate(OFF)
        logInfo("iPhone Home", "iPhone is away.")
    }
end

Do I need to include the gps coordinates?
Or maybe OpenHAB uses the home location of the server I defined when I setup the server for the first time?

see Items | openHAB

1 Like

Thank you for the explanation, @Wolfgang_S :folded_hands:

I use Blockly to calculate the distance between my home location (gps coordinates) and the cooridnates from iCloud, I can post the blockly or the code if you want.

I am changing this to Bluetooth BLE, seems to be easier to calculate.

1 Like

Yes, please.

Your Blockly configuration method or the code will be very helpful. Thank you :folded_hands:

And if/when you change your configuration to the Bluetooth BLE solution, I will be very grateful to see that method, too :folded_hands:

ok, here’s the screenshot of the blockly, if not readable the code is below.

code:

var iPhoneLocMiel, afstand;

function geo_distance(position1, position2) {
  var lat1 = position1.latitude;
  var lat2 = position2.latitude;
  var lon1 = position1.longitude;
  var lon2 = position2.longitude;
  var R = 6371e3; // metres
  var o1 = lat1 * Math.PI/180;
  var o2 = lat2 * Math.PI/180;
  var Ao = (lat2-lat1) * Math.PI/180;
  var Ab = (lon2-lon1) * Math.PI/180;
  var a = Math.sin(Ao/2) * Math.sin(Ao/2) +
  Math.cos(o1) * Math.cos(o2) *
  Math.sin(Ab/2) * Math.sin(Ab/2);
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
  return R * c;
}

function geo_position(latitude, longitude) {
  return {'latitude': latitude, 'longitude': longitude};
}


iPhoneLocMiel = items.getItem('iPhone_emiel_XS_Max_Location').state;
afstand = (geo_distance(geo_position(XXXXXXX, YYYYYYYY), geo_position(iPhoneLocMiel.slice(((iPhoneLocMiel.indexOf(',') + 1 + 1) - 1), (iPhoneLocMiel.lastIndexOf(',') + 1) - 1), iPhoneLocMiel.slice(0, (iPhoneLocMiel.indexOf(',') + 1) - 1))));
if (afstand > '150' && !(items.getItem('MielThuis').persistence.changedSince(time.ZonedDateTime.now().minusMinutes(10)))) {
  items.getItem('MielThuis').sendCommand('OFF');
  console.warn('miel = weg!');
}

The problem with the coordinates in iCloud is that the format is not compatible with the blockly rules. So I had to cut & paste the right gps numbers which makes it a bit complex. But basically I check if I’m further away than 150m and than I’m out.

iCloud isn’t that precise and updates also take a lot of time. And my only goal was to know if I’m home or not. Bluetooth BLE can do that much faster and precise. So I’ve installed ESPresence on an ESP32 board and connected a bluetooth beacon so I know the distance (based on RSSI Strength) within seconds. Only downside is that Bluetooth BLE needs a lot of trying/tweaking/tuning to get the best calculated distance you can work with. Connection between ESPresence and OH is MQTT.

And as mentioned already, presence detection is mostly a combination of technologies

1 Like

Thank you so much for the detailed explanation! :folded_hands:

From all the solutions I’ve tried so far, your description of the Bluetooth BLE method sounds like the most responsive.

Do you, by chance, know if OpenHAB can use the built-in Bluetooth radio of a Gigabyte Z490I AORUS ULTRA motherboard to implement the BLE method?
From my understanding, this motherboard has an Intel bluetooth adapter.

I don’t know that. But you have to place the beacons in a spot from where you want to measure the RSSI signal and distance. BLE works fine for a max of 4-6m. So the idea is to place a beacon (ESP32 with ESPresense) in a room to check if you’re in the room.

1 Like

DepI being on your Wi-Fi router brand/model there might be an integration already that infers presence based on your phone being connected to home Wi-Fi. I find that works very fine, especially if paired with another check like iCloud location.

1 Like

Hi Marco :slight_smile: The router method sounds like a very good solution :folded_hands:

I have a Tp-Link Archer C6 wi-fi router.
How can I check if it’s supported?

On the OpenHAB Add-On Store I saw two bindings for TP-Link:

One binding is for TP-Link Smart Home appliances (which, to my understanding, isn’t relevant to what you mentioned).

And the second binding is for TpLinkRouter Binding, but from the binding documentation, I’m not sure if it will give me information about the devices connected to the router. I think the binding just gives info on the router’s state.

Not sure the tplink archer has that kind of integration! Might be you can code something to access the MAC address table via snmp, or flash a third party firmware that has integrations!

Ok, Thanks :folded_hands: I’ll look into it.
Does OpenWRT support this type of integration?
From what I remember OpenWRT supports my router, so I can install this firmware.

Hi Marco,
I flashed OpenWRT on my router, so I can now install an snmp integration on it.
Do you know if there’s a guide explaining how to connect between OpenHAB and OpenWRT to monitor the presence of my devices on the local wireless network?

Hi there,

I have the same usecase and for the longest time used a Python script that would get me my phone location from iCloud.

I would use the Exec binding to trigger the script each hour and check if my phone was within a preset radius around my home. I also added the network binding into the mix to listen DHCP requests from the phone. This is mainly for faster status updates when coming hone.

This basically works. The code looks similar to mine but missing one critical thing.
The location data will also return vertical and horizontal accuracy information. You can pretty much ignore the vertical bit but from my experience it is rather important that you include the horizontal accuracy information in your calculation.

Why is this an issue? Glad you asked.
Your iPhone has several ways to establish its location, with GPS (or alternatives) being the most accurate followed by a lookup in Apples Wifi AP location database.
However it can also get its location data by looking up cellular networks cell ids and their location. This is when your location can suddenly change by hundreds of meters.
In your Maps app this inaccuracy is represented by a blue circle around your location

I found it sufficient to correct the distance you calculated above by subtracting the horizontal accuracy information.

However there is some other issue with using iCloud and that goes by the name of 2FA. At irregular intervals iCloud would require me to input the 2FA code and always at the most inconvenient times. Also i always needed two devices to do this. One to display the code and a second to enter it.

So i abandoned the iCloud approach and now use a few ESP32 (3, with an antenna mod) running ESPresense scattered around the property.
If you pair your phone with it you can configure it such that it will frequently send MQTT updates when the phone is within reach. I then just added a NodeRed flow that acts as a watchdog for the update messages.
My phone is represented by its own MQTT topic. While the flow gets updates for the phone its status is online and if the watchdog times out it will set the status to offline.
Running this for a few months now and it just works.

1 Like

Thank you for the detailed explanation, Tobias.
I really appreciate it :folded_hands:

1 Like