Count device_tracker devices online/home

Hello,
I found this code in another thread for system-monitoring. It displays all device_tracker devices (online and offline) as a number, Can I modify the code to get the number from all devices that are online/home only?

Thank you,
Philipp

  - platform: template
    sensors:
      count_device_trackers:
        entity_id: sensor.date
        value_template: >
          {%- set domains = ['device_tracker'] -%}
          {%- for domain in domains -%}
            {%- for item in states[domain] -%}
              {% if loop.first %}
                {{loop.length}}
              {% endif %}
            {%- endfor -%}
          {%- endfor -%}

I don’t know if this is possible with a HA sensor.
I just used a lovelace filter, added all the device trackers and filtered to show only devices in state ‘offline’ or ‘unavailable’.

You could do this with AppDaemon, if you are familiar with this.

I don’t have any device_trackers so I can’t test the following template (but it should work). Paste it into the Template Editor and let me know what it reports:

{{ states.device_tracker | selectattr('state', 'eq', 'home') | list | count }}
1 Like

Works perfectly, thank you!

1 Like