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
Code: Select all
if miles >= trueDistance: # <-- change to kilometers or leave as miles
return True
else:
return False
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.