Distances between zones or persons

I was playing with distances yesterday and came up with a couple that some folks might find useful. Thought I’d put them here for y’all.

The first one is distance between all zones and persons in the setup.

type: markdown
title: Distance From Zone to Person
content: |
  {% set ns = namespace(table_format='', table_line='') -%}
  {%- set ns.table_format = "|---" -%}
  {%- set ns.table_line = "| Zone" -%}
  {%- for person in integration_entities("person") -%}
    {%- set ns.table_format = ns.table_format + "|---:" -%}
    {%- set ns.table_line = ns.table_line + "| " + state_attr(person, 'friendly_name').split(' ')[0] -%}
  {%- endfor %}
  {{ ns.table_line }}
  {{ ns.table_format }}
  {%- for zone in integration_entities("zone") | sort() -%}
    {%- set ns.table_line = "| " + state_attr(zone, 'friendly_name') -%}
    {%- for person in integration_entities("person") -%}
      {%- set ns.table_line = ns.table_line + " | " + ("<ha-icon icon='mdi:map-marker'></ha-icon>&nbsp;&nbsp;&nbsp;" if states(person) | lower() == zone | replace('zone.', '') else distance(zone, person) | round(1) | string  + ' mi') -%}
    {%- endfor %}
  {{ ns.table_line }}
  {%- endfor %}

This one looks like:
image
Yes, me, my wife, and my truck are my persons.

And, the second is distance between all zones in a setup.

type: markdown
title: Distance From Zone to Zone
content: |
  {% set ns = namespace(table_format='', table_line='') -%}
  {%- set ns.table_format = "|:---:" -%}
  {%- set ns.table_line = "| " -%}
  {%- for zone in integration_entities("zone") | sort() -%}
    {%- set ns.table_format = ns.table_format + "|:---:" -%}
    {%- set ns.table_line = ns.table_line + "| " + state_attr(zone, 'friendly_name') | replace(' ', '<br>') -%}
  {%- endfor %}
  {{ ns.table_line }}
  {{ ns.table_format }}
  {%- for zone1 in integration_entities("zone") | sort() -%}
    {%- set ns.table_line = "| <b>" + state_attr(zone1, 'friendly_name') | replace(' ', '<br>') + '</b>' -%}
    {%- for zone2 in integration_entities("zone") | sort() -%}
      {%- set ns.table_line = ns.table_line + " | " + ("-" if zone1 == zone2 else distance(zone1, zone2) | round(1) | string) -%}
    {%- endfor %}
  {{ ns.table_line }}
  {%- endfor %}

And, the output:
image

I hope someone can use these or use the ideas in these.

8 Likes

This is brilliant! Exactly what i was looking for. I understand most of what the code does, bit fuzzy on the table formatting, but hey.
I used example 1, but i don’t get the map marker icon on mine, just distance as 0.0. Any idea why that might be?

Oh, I found an error in there and never came back to update. Sorry. I had to convert states(person) to lower before comparing it to zone.

It has been updated above.

First off thank you for this it helps a lot.

I do have a problem though. Some of my zone distances are spot on, while others are sometimes five miles off in distance.

Is the distance a straight line “as the crow flies” scenario, or map based?

Also I use Waze which gives travel times. Could a travel time be incorporated into this for two people.

Thanks so much.

Glad to help.

I have no idea if it is as the crow flies or map based. I never mapped any of them to verify them. Just thought it was cool to use the distance command this way.

As far as Waze travel time, there is an integration for that.
Waze Travel Time - Home Assistant (home-assistant.io)

I have never used it. But, I assume it sets up entities with times that you could certainly bring in.

@carltonwb
So, I got interested in this question again and did some searching.

According to
Home Assistant - Know the distance between two people (linkedin.com)

It is the Euclidean distance which is ‘as the crow flies’.

I do not know where they got this info or if it is correct. But I think it might be based on your observations.

Again, if you need map based, see the Waze link above. I have actually started using this. From a distance, it seems to work fine. But when I am home, it shows me 3 minutes away. I just home the wife does not think I am at the neighbor’s house :slight_smile: