Life360 - Distances

Posted on
Wed Aug 23, 2023 9:40 am
whmoorejr offline
User avatar
Posts: 763
Joined: Jan 15, 2013
Location: Houston, TX

Life360 - Distances

This may be helpful to others? Or my coding sucks and someone will point out how bad I am with python. I'm playing with some code to give me distances between devices or between a device and a set point.
I was looking for a way to add data to a daily status email. If member devices are at a named place or within an indigo-geofence, I'll get that. If they are at a location with no name (not a place) nor within a geofence, then how far are they from home or how far are they from me.... This is what I'm using currently that seems to work well and will give me results in kilometers and/or miles.

Code: Select all
from math import sqrt, cos, radians

# Life 360 Member Device
location1 = indigo.devices[1986325525] # "Life360 - Bill"
lat1 = location1.states["member_lat"]
lon1 = location1.states["member_long"]

# Life 360 Geofence Center
location2 = indigo.devices[1052647473] # "GeoFence: HEB - L360"
lat2 = float(location2.ownerProps["geofence_lat"])
lon2 = float(location2.ownerProps["geofence_long"])

R = 6371  # radius of the earth in km
x = (radians(lon2) - radians(lon1)) * cos(0.5 * (radians(lat2) + radians(lat1)))
y = radians(lat2) - radians(lat1)
kilometers = R * sqrt(x*x + y*y)
miles = kilometers * 0.621371

# Round results 2 places past decimal, convert result to string.... make it pretty.
kilometers_string = str(round(kilometers, 2))
miles_string = str(round(miles, 2))
message = "The distance between points is " + kilometers_string + " km (" + miles_string + " miles)"

indigo.server.log(message)


I am using the same math to put together a few triggers to have them trigger based on distance between points. (my device has any location change - > If distance from my device is greater/less/equal to miles or kilometers -> action) Good for things like.... If I'm home and a kid's device is now X miles away from me, send me a notification. Or if I'm now within X miles from home, perform these actions. This is what I have in a condition...
Code: Select all
from math import sqrt, cos, radians
trueDistance = 5   # <-- quantity, select miles or kilometers below
The middle part is the same and then tack this on at the end
Code: Select all
if miles >= trueDistance:  # <-- change to kilometers or leave as miles
   return True

else:
   return False
Bench testing all the above has been successful. I'll know for sure if it's useful after traveling around town for a few days to see what happens in the field.

Disclaimer: From my limited research, this is not the most accurate way to determine distance.... but in my opinion, what is? Too many factors to call anything accurate. Straight line, curved line, travel distance (roads), moving objects, etc. I also wanted something that would return a result quickly without having to ping off an outside source to get road distance, traffic conditions, etc.

Lastly, I am thinking about doing a distance calculation that tallies the results to get distance traveled over time. To see how far I drive per day/week/month/year. Not that I would need it or use it.... just curious if I could do it. My guess.... save current location as a set of variables "previous_lat" and "previous_long". Whenever location on the device updates, get distance using current location of device and the variables, add the result to a "drive_today" variable. Update the "previous_lat" and "previous_long". Have a nightly schedule to add the daily total into a weekly variable and zero out the day. Similar function of a weekly and monthly schedule. Something like that. I think it will work, but again, no use for it, just fun to see if I could make it work without messing up something.

Bill
My Plugin: My People

Posted on
Tue Sep 05, 2023 10:25 pm
mundmc offline
User avatar
Posts: 1064
Joined: Sep 14, 2012

Life360 - Distances

I am on the verge of getting back into Indigo in my typical all or nothing manner, and I need to catch myself up on the current accepted best practices of Geo location while maintaining privacy. This approach is intriguing.

In regards to any information you made to your approach being suboptimal, I give you this: The year was 2012. iCloud had a very permissive API. I was acclimating myself to Indigo 5(?) and trying to learn up on programming languages (which I had not used for the past 8–10 years).

Long story short, I had a PHP script that polled the iCloud server every X minutes (I think I settled at one or five minutes) to get my latitude and longitude from my iphone 4. I forget how, but I got that information somehow into Indigo Variables. Then I did some applescript math that legit involved the radius of the Earth as a constant.

It worked pretty well for my studio apartment in Midtown Manhattan. The amazing part, though, was the furthest place I ever went to was the Galapagos islands and it got the surface distance freaking correctly! That got borked with one of the iCloud security updates, presumptively appropriately.

I need to check this out and catch up with you soon

Posted on
Wed Sep 06, 2023 8:55 am
whmoorejr offline
User avatar
Posts: 763
Joined: Jan 15, 2013
Location: Houston, TX

Re: Life360 - Distances

mundmc wrote:
The year was 2012.
That was a weird time for navigation. Around that time I was using a program called Wifi SLAM.... written by a couple college guys at Stanford. It used the known location of wifi nodes to triangulate your indoor location based on wifi signal strength. It worked pretty well and simple to use. In 2013 Apple bought the company and all versions of the program disappeared from online. I wan't too worried, actually looking forward to the polished, user friendly apple version to show up. 10 years later....

As far as Life 360.... For keeping tabs on my kids, it has been a little more dependable than Apple's "Find My" (not all the time, but on average)

The app on it's own accomplishes about 90% of what I need. The plugin easily makes up the remaining 10% (better control over alerts/notifications and home automation integration). Here forward, it's more of a brain exercise to see what all is possible.

The next things I'm trying to figure out the trigger / variable / python math trickery to make a log of potential points of interest. So if a device stops moving for 't' time, then save the lat/long as a new variable or in an external file or something.

I also want to see if I can figure out is how to incorporate a map. Ideally, a map that could generate automatically in addition to on command with the ability to control what is on it. Like all devices visible, or by device or by device with a reference point. (so if a device map shows my kids location.... if I don't recognize the street, it's pointless without a known reference point. So a map with my kid's location and the home geofence would be good most of the time. If the whole family is out of town, a map with the kid's device with my device as the reference point would be good. The closest I've gotten so far is to build a KML file with whatever data layers you want and open it in Google Earth. If I remember correctly, the points will move if you change and re-save the KML.... so it would be live.... but on a separate platform. I think I figured out how to do it as a link to google maps (which would also be handy so you can see the target location and presumably your phone will know where you are. But if you are doing that, you might as well cut out the middleman and go directly to the source of the data (the life 360 app). So it's more for the control page nerd chub factor, not something that I would actually use.

Bill
My Plugin: My People

Posted on
Sat Sep 09, 2023 8:18 am
mundmc offline
User avatar
Posts: 1064
Joined: Sep 14, 2012

Re: Life360 - Distances

Wow- i need to put Life360 on my list- i am still rebuilding my home automation after my house got gutted

Posted on
Sat Sep 09, 2023 9:47 am
neilk offline
Posts: 729
Joined: Jul 13, 2015
Location: Reading, UK

Re: Life360 - Distances

I also want to see if I can figure out is how to incorporate a map. Ideally, a map that could generate automatically in addition to on command with the ability to control what is on it.


Check out https://developer.mapquest.com/documentation/static-map-api/v5/

I used it to plot my car location in my now defunct (as I no longer have the car) Jaguar I Pace plugin, it does what you are looking for.

Posted on
Sat Sep 09, 2023 1:15 pm
whmoorejr offline
User avatar
Posts: 763
Joined: Jan 15, 2013
Location: Houston, TX

Re: Life360 - Distances

neilk wrote:
Thank You!!

Oh this is fun. I'm only an hour into playing on that site and I'm seeing the potential. I'm not a programmer, but I think I can scrap together something to make this work. The free API key gives you 15,000 map image requests/month.... works out to a once every 3 minutes.

Edit: Started a separate thread on this.... wrote up a 'how-to' for making maps for control pages using L360 data. https://forums.indigodomo.com/viewtopic.php?f=363&t=27430

Bill
My Plugin: My People

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 3 guests