Know how many and which devices are switched on

Hello, I hope you can help me, I would like to know how to create a sensor that tells me how many light bulbs or devices are on. I have managed to do that but I would like to know which devices are on.

  - platform: template
    sensors:
      current_enchufes_importantes_on:
        friendly_name: Enchufes Importantes ON
        icon_template: mdi:lightning-bolt-outline
        unit_of_measurement: 'on'
        value_template: >
          {% set total = 0 %}
          {% if is_state('switch.ventilador', 'on') %}
            {% set total = total + 1 %}
          {% endif %}
          {% if is_state('switch.calefactor_bano', 'on') %}
            {% set total = total + 1 %}
          {% endif %}
          {% if is_state('switch.monitor', 'on') %}
            {% set total = total + 1 %}
          {% endif %}
          {% if is_state('switch.monitor_dos', 'on') %}
            {% set total = total + 1 %}
          {% endif %}
          {% if is_state('switch.grave', 'on') %}
            {% set total = total + 1 %}
          {% endif %}
          {% if is_state('switch.tele_dos', 'on') %}
            {% set total = total + 1 %}
          {% endif %}
          {{total}}

image

your sensor above could be simplified significantly.

first create a group of devices you want to count that are are β€˜on’:

group:
  enchufes_importantes:
    name: Enchufes Importantes
    entities:
      - switch.ventilador
      - switch.calefactor_bano
      - switch.monitor
      - switch.monitor_dos
      - switch.grave
      - switch.tele_dos

then for the sensor above use the following:

value_template: >
  {{ expand('group.enchufes_importantes') | selectattr('state', 'eq', 'on')  | list | count }}

and to get the friendly names of the items:

value_template: >
  {{ expand('group.enchufes_importantes') | selectattr('state', 'eq', 'on') | map(attribute='name') | list }}
2 Likes

Thank you very much for the solution!

1 Like