Counts the switches on

Hi folks, I have like 60 ish switches, every one with 3 outlets and located in different areas around my house. I’m working with minimalist UI, which provides “chips”. I would like to use one of those to know how many switches I have turned on, but I0m not sure as I cold not find anything on the web.
I just included the sensors yaml in my config file, as showed below:

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
sensor: !include sensors.yaml
frontend:
  themes: !include_dir_merge_named themes

While I was searching, I also found that I needed to create a group inside my configuration.yaml file, so I did it as stated below:

group:
  luces_cuartoprincipal:
    name: Luces cuarto principal
    entities:
      - switch.sonoff_1000f47d9a_1
      - switch.sonoff_1000f47d9a_2
      - switch.sonoff_1000f47d9a_3
      - switch.sonoff_1000e98e6c_1
      - switch.sonoff_1000e98e6c_2
      - switch.sonoff_1000950537_1
      - switch.sonoff_1000950537_2
      - switch.sonoff_100095020c_1
      - switch.sonoff_100095020c_2

And inside my sensors.yaml file I tried to create a sensor platform, But I’m not sure it would work as it uses a lot of code I found in certain topics in here, it looks like this:

- platform: template
  sensors:
    lucesencendidas_cuartoprincipal:
      entity_id:
        - switch.sonoff_1000f47d9a_1
        - switch.sonoff_1000f47d9a_2
        - switch.sonoff_1000f47d9a_3
        - switch.sonoff_1000e98e6c_1
        - switch.sonoff_1000e98e6c_2
        - switch.sonoff_1000950537_1
        - switch.sonoff_1000950537_2
        - switch.sonoff_100095020c_1
        - switch.sonoff_100095020c_2
      value_template: >
        {% set switches = [
          states.switch.sonoff_1000f47d9a_1,
          states.switch.sonoff_1000f47d9a_2,
          states.switch.sonoff_1000f47d9a_3,
          states.switch.sonoff_1000e98e6c_1,
          states.switch.sonoff_1000e98e6c_2,
          states.switch.sonoff_1000950537_1,
          states.switch.sonoff_1000950537_2,
          states.switch.sonoff_100095020c_1,
          states.switch.sonoff_100095020c_2,
          ] %}

I need a little help creating this sensor, as I wanted to show how many lights are located in certain areas. Thanks in advance!

- platform: template
  sensors:
    lucesencendidas_cuartoprincipal:
      value_template: >
        {{ expand(state_attr('group.luces_cuartoprincipal', 'entity_id'))
          | selectattr('state', 'eq', 'on') | list | count }}

EDIT

Changed the group’s entity_id to match the one shown in the first post.

Hey Taras! What if they are not used as light but switches?

Please test the revised version of the example I posted above and let me know if it reports the correct number of switches that are on.

Hey Teras! Thanks for your help! I could do it perfectly, I wasn’t home so unable to test it, thanks!

1 Like