How to: geolocation trigger distance to device_tracker

using the would allow me to set a device_tracker as zone, but then the person would already be deceased I fear…

Would like to create a warning for quakes within a distance to the tracker of lets say 400 km?

should we use the geolocation trigger for that, and if yes, how,
or would it be best to trigger of the generic domain geolocation, and then set a {{trigger.to_state.state < 400}} condition.
The latter would of course need some nifty triggering setup, since these states geolocation are made dynamically and don’t always exist.

maybe create an extra zone with radius 400 km around the device_tracker (not sure how to though…since using a device_tracker always uses that devices gps attributes) ?
not really sure how to proceed best, so seek some assistance of community-fellows please?

dont think we can set a device_tracker to be the center for geolocation (only lat/lon is listed as options), nor can we set a template for the radius which would be an enormous improvement for the integration

Would hope for something like:

  - platform: usgs_earthquakes_feed
    feed_type: 'past_day_all_earthquakes'
    radius_template: >
    minimum_magnitude: 2.0
    device_tracker: device_tracker.life360_person_to_watch
#    latitude: -7.7
#    longitude: 110.3


I do not think you can make it dynamic using zones, since these are predefined and cannot be set dynamically (I believe).
You can try, though to use distance, as described in documentation:

Distance examples

If only one location is passed in, Home Assistant will measure the distance from home.

Using Lat Lng coordinates: {{ distance(123.45, 123.45) }}

Using State: {{ distance(states.device_tracker.paulus) }}

These can also be combined in any combination:
{{ distance(123.45, 123.45, 'device_tracker.paulus') }}
{{ distance('device_tracker.anne_therese', 'device_tracker.paulus') }}

This way you can measure distance between earthquake center and specific person and trigger action when it drops below specific radius.


yes, thanks, Ive started out with that, but the trouble starts when using that with dynamic entities. it is easy enough to template the distance between a device_tracker and a already existing geolocation.state. I need to be able to template that for dynamic entities though, as they do no always exist, in fact hope they never do :wink:

Without having tried it:
You could define a zone that spans the whole world, then use the geolocation trigger to start an automation with any geolocation entity that appears anywhere in the world, this entity’s data should then be available as trigger.entity_id or trigger.to_state, which I would hope could then be used in the distance function.

ok, thanks!
I will try and setup something like that. I fear though that doing so, Ha ends with enormous amounts of entities (had over 400 lightning strikes lately…) and thus overloading the instance.

would be really cool if we could simply use the device tracker for centration, just like we can do in the zone/proximity configuration

adding the input_number is of secondary importance, but would be a nice bonus :wink:


testing this now:

  - alias: Quake Alert
    id: 'Quake Alert'
      platform: event
      event_type: state_changed
      condition: template
      value_template: >
        {{'geo_location.quake') and
           distance('device_tracker.life360_name', < states('input_number.quakes_near')|float }}

which is still a bit of a workaround, because it won’t let me set the quake creation yet, and uses a more global set of quakes, built around a fixed lat/lon combination. Ideally I would create the entities around the device_tracker.

Also, the above listens to all state changes and I don’t know yet if that impacts the system terribly…shouldn’t be necessary.

counting sensor near:

          {% set km = states('input_number.quakes.near')|float %}
          {% set ns = namespace(count=0) %}
          {% for s in states.geo_location | selectattr('attributes.source','eq','usgs_earthquakes_feed') %}
            {% if distance(s.entity_id, 'device_tracker.life360_name') < km %}{% set ns.count = ns.count + 1 %}{% endif %}
          {% endfor %}
          {{ ns.count }}

might be better to use above for counting, and a binary_sensor sensing count is !=0

        friendly_name: Earthquakes near active
#        entity_id: sensor.count_earthquakes
        value_template: >
          {{states('sensor.count_earthquakes_near_name')|int > 0 }}
        device_class: safety

and use this for trigger