Nearest zone

Hi,

My job takes me all over the country by train and I need to find my way home a lot by using public transport. The railway in my country has an api i can poll to get the train schedule however they require to put a fixed route in your config.

I was trying to make this dinamic to my location but for this to work i would need to be able to tell the nearest Station to my location. I already have all the stations in my zone.yaml, however i am trying to find how to determine what the nearest “zone” is to my personal location.

all the examples i can find are to find the nearest entity to one particular zone but i need it the other way around.

Proximity - Home Assistant seems not to be the correct way to go.

The only thing I can think of is to create all the proximity entities then a sensor with a list
Example

{ 
  "zone.somewhere" : states('zone.somewhere'), 
  "zone.somewhereelse": states('zone.somewhereelse') 
}

From this sensor you should be able to get the minimum value with just filter min.
Then do a reverse search to get the zone name from the minimum value.

i was afraid you would say that. i would realy try to avoid making 800 sensors for this :sweat_smile: but if it is the only way.

It used to be real easy:

{{ closest('device_tracker.me', states.zone) }}

But at some point, they changed the way zones are expanded. Now it expands to a list of person entities within each zone, so this doesn’t work anymore.

But you could do something along the lines of:

{% set ns = namespace(x=[]) %}
{% for zone in states.zone %}
{% set ns.x = ns.x + [(distance(zone, 'device_tracker.me'), zone.name)] %}
{% endfor %}
{{ (ns.x|sort)[0][1] }}
3 Likes

Thank you, this actualy solved my issue.

1 Like