How come doing this works

go to developer tools
this one works

    {% set jackie_lat = state_attr('device_tracker.jackie','latitude') %}
    {% set jackie_long = state_attr('device_tracker.jackie','longitude') %}
    {% set close_devices = namespace(devices=[]) %}
    {% for item in states['device_tracker'] %}
  
      {% set entity_id1 = item.entity_id %}
      {{ item.state }}
      {% if entity_id1 != 'device_tracker.jackie' and item.state != 'home' and item.state == entity_id1 %}
        {% set state = states.entity_id1 %}
        {% set device_lat = state_attr(entity_id1, 'latitude') %}
        {% set device_long = state_attr(entity_id1, 'longitude') %}
        {% set distance = distance(jackie_lat, jackie_long, device_lat, device_long) %}
        {% if distance <= 1 %}
          {% set close_devices.devices = close_devices.devices + [{'entity_id': entity_id1, 'distance': distance}] %}
        {% endif %}
      {% endif %}
    {% endfor %}
    {{ close_devices.devices | length > 0 }}

this does not?

    {% set jackie_lat = state_attr('device_tracker.jackie','latitude') %}
    {% set jackie_long = state_attr('device_tracker.jackie','longitude') %}
    {% set close_devices = namespace(devices=[]) %}
    {% for item in states['device_tracker'] %}
  
      {% set entity_id1 = item.entity_id %}
      {{ item.state }}
      {% if entity_id1 != 'device_tracker.jackie' and item.state != 'home' %}
        {% set state = states.entity_id1 %}
        {% set device_lat = state_attr(entity_id1, 'latitude') %}
        {% set device_long = state_attr(entity_id1, 'longitude') %}
        {% set distance = distance(jackie_lat, jackie_long, device_lat, device_long) %}
        {% if distance <= 1 %}
          {% set close_devices.devices = close_devices.devices + [{'entity_id': entity_id1, 'distance': distance}] %}
        {% endif %}
      {% endif %}
    {% endfor %}
    {{ close_devices.devices | length > 0 }}

checking to see if device_tracker is not jackie and not home causes error TypeError: ‘<=’ not supported between instances of ‘NoneType’ and ‘int’.
this works
{% if entity_id1 != ‘device_tracker.jackie’ and item.state == ‘home’ %}
this does not
{% if entity_id1 != ‘device_tracker.jackie’ and item.state != ‘home’ %}
this does not work
{% if entity_id1 != ‘device_tracker.jackie’ %}
yet this works
{% if entity_id1 != ‘device_tracker.jackie’ and item.state != ‘home’ and item.state == entity_id1 %}
huh?

Have you looked at the Proximity integration?

It is hard to tell from the code what you mean by ‘works’, but two things pop out when looking at this code: first of all it seems unlikely that item.state == entity_id1 is true, so I guess that makes you not execute the faulty code within the block below.

The second is: many device trackers do not have gps coordinates, so they would create an error like the one you mention.

I will try this out thank you

you were right tested {% if state_attr(entity_id1, ‘latitude’) != none%}

    {% set jackie_lat = state_attr('device_tracker.jackie','latitude') %}
    {% set jackie_long = state_attr('device_tracker.jackie','longitude') %}
    {% set close_devices = namespace(devices=[]) %}
    {% for item in states['device_tracker'] %}
      {% set entity_id1 = item.entity_id %}
      {% if entity_id1 != 'device_tracker.jackie' and item.state != 'home' %}
          {% if state_attr(entity_id1, 'latitude') != none%}
          {% set state = states.entity_id1 %}
          {% set device_lat = state_attr(entity_id1, 'latitude') %}
          {% set device_long = state_attr(entity_id1, 'longitude') %}
          {% set distance = distance(jackie_lat, jackie_long, device_lat, device_long)|round(2) %}
          {% if distance <= 2 %}
            {% set close_devices.devices = close_devices.devices + [{'entity_id': entity_id1, 'distance': distance}] %}
          {% endif %}  
        {% endif %}
      {% endif %}
    {% endfor %}
    {{ close_devices.devices | length > 0 }}

You can filter your entities beforehand instead of doing it in the loop:

{% set jackie_lat = state_attr('device_tracker.jackie','latitude') %}
    {% set jackie_long = state_attr('device_tracker.jackie','longitude') %}
    {% set close_devices = namespace(devices=[]) %}
    {% for item in states.device_tracker | rejectattr('state', 'eq', 'home') | rejectattr('entity_id', 'eq', 'device_tracker.jackie') | selectattr('attributes.latitude', 'defined') %}
      {% set entity_id1 = item.entity_id %}
        {% set state = states.entity_id1 %}
        {% set device_lat = state_attr(entity_id1, 'latitude') %}
        {% set device_long = state_attr(entity_id1, 'longitude') %}
        {% set distance = distance(jackie_lat, jackie_long, device_lat, device_long)|round(2) %}
        {% if distance <= 2 %}
          {% set close_devices.devices = close_devices.devices + [{'entity_id': entity_id1, 'distance': distance}] %}
        {% endif %}
    {% endfor %}
    {{ close_devices.devices | length > 0 }}
1 Like