Obtain distance from device tracker - attribute or not?

Okay,
I have set up and used distance for quite a while but I’ve always found the set up to be less than satisfying.
With the template editor I enter : -

{{ states.device_tracker.life360_muttley }}

{{ states.device_tracker.life360_muttley.attributes }}

{{ '%.3f' | format(distance(states.device_tracker.life360_muttley)) }}

And in response I get : -

<template state device_tracker.life360_muttley=home; source_type=gps, latitude=x.xxxxxx, longitude=x.xxxxxx, gps_accuracy=15, battery=58, address=Home, at_loc_since=2020-01-09T13:19:18+00:00, battery_charging=False, driving=False, last_seen=2020-01-10T01:41:09+00:00, moving=False, place=Home, raw_speed=0.87847424, speed=3, wifi_on=True, friendly_name=life360 muttley @ 2020-01-09T13:19:28.437379+00:00>

{'source_type': 'gps', 'latitude': x.xxxxxx, 'longitude': x.xxxxxx, 'gps_accuracy': 15, 'battery': 58, 'address': 'Home', 'at_loc_since': datetime.datetime(2020, 1, 9, 13, 19, 18, tzinfo=<UTC>), 'battery_charging': False, 'driving': False, 'last_seen': datetime.datetime(2020, 1, 10, 1, 41, 9, tzinfo=<UTC>), 'moving': False, 'place': 'Home', 'raw_speed': 0.87847424, 'speed': 3, 'wifi_on': True, 'friendly_name': 'life360 muttley'}

0.006

So given the lack of a clear attribute named ‘distance’, how come the 3rd entry even works ?
And as this is not the best way to call such (as the templating documents state) how should it be called ?

See https://www.home-assistant.io/docs/configuration/templating/#distance

And where does it say that?

I just inferred that from : -

Though it’s not a direct parallel ???

(I must have read that listing as I can’t think where else I’d have gotten the template, though you will have to admit, we still occasionally find references in the docs to ‘states.something’ so maybe assumed it was an old format yet to be updated ??? dunno, I’m guessing)

But why is distance not listed as an attribute ? Just seems strange. :man_shrugging:

Apparently distance() can take a few different types & numbers of arguments. Ultimately it needs two points.

A State object is one type. But an entity_id is another, in which case it will just try to get its State object. So distance(states.device_tracker.me) is just as good as distance('device_tracker.me'). In these cases it uses the latitude & longitude attributes of the State object.

Another type is a float, or something that can be converted to a float. In this last case it wants two for latitude & longitude.

It calculates the distance between two points. If you specify only one point, then the second point is assumed to be the location of Home.

Distance to what?

Good point given your explanation above. :+1:
Thanks

Sorry, given this I assume you could get the distance between two people by just passing the device_tracker ids? Interesting, I’ll give that a go
Cheers

Yep. In fact, that is shown in one of the examples.

Yep, I see that now (wood / trees)

I think I just made the assumption that distance was an attribute rather than a function.
You know what they say about assumptions? (cept you are not an ass !, I am ! }

1 Like