[SOLVED] Display amount of online/offline devices within group - xx/xx

Hi there,
I was looking for a way to display the amount of devices within a group or list which are either in an offline or off state. For example:

Door Sensors: 14/18 (14 online out of the total of 18 sensors)
ESPHome: 5/5 (all devices online out of 5 in total)

Have a look at the auto-entities card

1 Like

Thanks Dave! Looks close to what I need however I can’t get my head around the templating in order to get it in the format I want:

Door Contact Sensors 14/18
ESPHome Devices 5/5

Oh, sorry. You just want text rather than a list. You could create a template sensor for each set and have it output the number of online devices.

Templating is not my forte however there should be examples on this forum to get you there

You don’t provide details, but let’s say you have a group named group.door_sensors, and each of the entities in this group have a state of off, offline, etc. Then maybe something like:

{% set doors = expand('group.door_sensors') %}
Door Sensors: {{ devs|rejectattr('state','in',('off','offline'))|list|count }}/{{ devs|count }}

If that doesn’t work then you’ll need to provide more details about your groups and the entities in them (states, attributes, whatever.)

Does expand add the grouped entities to the template listener? I vaguely remember seeing that…

I do remember hearing something about that, too, but I don’t know the status. Of course, the OP didn’t say what they wanted to do with the output of the template, so… They might want a template sensor, but they might not either.

Hello again!,

Thanks for all the replies! I’ve managed to get a working solution with help from a chap on the HA Facebook community group … Here is the code: https://pastebin.com/s49zdUrY

image

I’ll put it here to save people going to the other webpage, or if the pastebin expires

  platform: template
  sensors:
    door_sensors_status_array:
      entity_id:
        - group.all_door_sensors
      value_template: >-
        [{%- for e in state_attr('group.all_door_sensors', 'entity_id') %}
          {% if loop.first %}{% else %},{% endif %}
          {%- if states(e)|lower == 'on' %}1{% else %}0{% endif -%}
        {%- endfor -%}]
    door_sensors_status_on:
      entity_id:
        - sensor.door_sensors_status_arrayy
      icon_template: 'mdi:door-open'
      unit_of_measurement: count
      friendly_name: Door Sensors Open
      value_template: >-
        {{ states('sensor.door_sensors_status_array')|from_json|sum }}
    door_sensors_status_count:
      entity_id:
        - sensor.door_sensors_status_array
      icon_template: 'mdi:door-open'
      friendly_name: Door Sensor Count
      unit_of_measurement: count
      value_template: >-
        {{ states('sensor.door_sensors_status_array')|from_json|length }}
    door_sensors_status_percent:
      entity_id:
        - sensor.door_sensors_status_on
        - sensor.door_sensors_status_count
      friendly_name: Door Sensors Open
      icon_template: 'mdi:door-open'
      unit_of_measurement: '%'
      value_template: >-
        {{ '%0.1f' | format(states('sensor.door_sensors_status_on')|float / states('sensor.door_sensors_status_count')|float * 100.0) }}
    door_sensors_status:
      entity_id:
        - sensor.door_sensors_status_percent
      friendly_name: Door Sensors Open
      icon_template: 'mdi:door-open'
      value_template: >-
        {{ states('sensor.door_sensors_status_on') -}}/{{-  states('sensor.door_sensors_status_count') -}}
2 Likes