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.