Convert Latitude/Longitude to Geofence for Presence

I'm trying to automate the opening of my garage when I am driving up to the house. We walk places fairly often, so it's not quite enough to use mobile presence/arrival as a trigger.

I have a Tesla (without Homelink) and was able to connect the car to HE with the Smartthings device driver. I can get the latitude and longitude that way, and whether the car is "driving".

But, I am at a loss with respect to how to convert the coordinates into a presence sensor. Ideally, I'd like to use some app/driver/rule/combination to create a geofence, and when the car's coordinates pass into that geofence, set off my rules.

I started to look into Google's Maps API's - and that looks like it might be a viable path - but I'm hoping there's an easier way since that is definitely over my head at this stage.

Welcome to the Quagmire.

I believe Hubitat has a GeoFence capability that merges with the Phone app. That may solve part of the problem. If not, you will need to keep searching.

For reasonable distances (within a city/town/state), the Haversine Formula is pretty accurate, certainly good enough for presence:

distance(lat1, lon1, lat2, lon2):
    p = pi/180
    a = 0.5 - cos((lat2-lat1)*p)/2 + cos(lat1*p) * cos(lat2*p) * (1-cos((lon2-lon1)*p))/2
    return 12742 * asin(sqrt(a))

edit: source:
https://stackoverflow.com/questions/27928/calculate-distance-between-two-latitude-longitude-points-haversine-formula

Thanks for the reply! I am using the Hubitat app and its Geofencing - but unfortunately that gets me the location of my phone, not the car. Since we regularly go for walks here in the city, I'd like to link to the car's state and location to avoid my garage opening up when I leave and return home on foot.

Oooh very interesting -- I'm a rookie here, so forgive the stupid question. Are you proposing that I throw that formula in as a global (or maybe local?) variable and then use the result in my triggers (say, when distance < some threshold)?

No, you will have to calculate each time you get a new car location sample, the location of your home will probably not change. Will give the distance between the two. The formula above gives result in km, you would just set a desired radius. Changing the constants would give you miles, etc. See the link for details. You will need to code it in Groovy, let an app or driver (however you get Tesla to pass lat/long) do the Calc, & create a trigger value. Essentially, your β€œdriver” will be a number generator (like a voltage meter) that outputs a distance, to use as a trigger.

Got it. That sounds very doable. Will try it and report back - thanks for your help!

Glad to do it. Welcome to the community!

There is a Tesla driver floating around here. Does it provide presence?

I've thought about doing something similar with my car and others have mentioned getting a separate presence sensor, and just leaving it in the car. This way when the car enters the mesh, it'll open the garage

1 Like

It does, but the presence is just an indication as to whether the key (i.e. my phone) is in the car.

Considered this as well. Hoping to avoid it since I've got a car that provides lat/long data, but I think it's a pretty good fallback if I can't get @672southmain's idea turned into a solution.

OK, I think I got it working, after tinkering with syntax in both the driver and the app for a while. I'm successfully returning a distanceFromHome value based on the formula above and a user-entered home latitude and home longitude value upon instantiating the car via the app.

My only remaining question is - the driver is set to automatically refresh every 15 minutes (and the distanceFromHome calc is embedded in the code that runs on refresh). Even if I reduce that to every minute, I'm still thinking that's not frequently enough to actually open the garage door on approach. Will a subscription from rule manager monitor the distanceFromHome on more of a "push" basis, or is there something else I am missing?

You are going to have to ask a Rule Manager expert about that. It’s over my level of Hubitat experience. Congratulations on getting it working.

Drive slowly, circle the block, or increase the radius.

Perhaps, as the distance gets closer and you begin approaching, you could sample faster.

That seems like a weird thing to me. A car that expensive without it? Even my plain-Jane 2003 Minivan has it. :laughing:

They do seem to make a Homelink retrofit for some of these cars. If it were me, I would want it even if it were just a backup to your Hubitat, it is nice to have a manual override at times. Says it is $300. https://www.tesla.com/support/homelink-faq

Yeah, but where's the fun in that? I have a manual garage door opener as my fallback :smiley:

1 Like

Thanks again for your help!

EDIT: Deleted my post as it was OT and not constructive for this conversation.

1 Like

Is there a function you can call from within the device to check for the distance from home? You could maybe make a rule that triggers when the car is away from home, and to poll it maybe every minute and stop once the car is back within the radius.

The only iffy thing with this is if you drive within your circle but aren't going back home, it'll open your garage door