Template Counting lights on, works in template editor but wont work in sensor.yaml

Hello,

I try to count the number off: on, off, unavailable lights in my configuration
the following works in the template editor and give me back a number…

when I try to put it in my sensor.yaml file and try to validate it, the validation starts but never ends…

Whats the culprit here?

 - platform: template
   sensors:
     totaal_aantal_lampen_aan:
      friendly_name: 'Totaal Aantal lampen aan'
      value_template: "{{ states.light | selectattr( 'state', 'eq', 'on') | list | count }}"
     
  - platform: template
   sensors:
       totaal_aantal_lampen_uit:
      friendly_name: "Totaal Aantal lampen uit"
       value_template: "{{ states.light | selectattr( 'state', 'eq', 'off') | list | count }}"
     
  - platform: template
    sensors:
      totaal_aantal_lampen_niet_beschikbaar:
      friendly_name: "Totaal Aantal lampen niet beschikbaar"
     value_template: "{{ states.light | selectattr( 'state', 'eq', 'unavailable') | list | count }}"

regards
Frank

You can only have one definition for the template sensors in that file.

- platform: template
   sensors:
     totaal_aantal_lampen_aan:
       friendly_name: 'Totaal Aantal lampen aan'
       value_template: "{{ states.light | selectattr( 'state', 'eq', 'on') | list | count }}"

     totaal_aantal_lampen_uit:
       friendly_name: "Totaal Aantal lampen uit"
       value_template: "{{ states.light | selectattr( 'state', 'eq', 'off') | list | count }}"
     
     totaal_aantal_lampen_niet_beschikbaar:
       friendly_name: "Totaal Aantal lampen niet beschikbaar"
       value_template: "{{ states.light | selectattr( 'state', 'eq', 'unavailable') | list | count }}"

Also make sure your indentation is correct. The “sensors” and the the “friendly_name” of your second sensor and the" value_template" of your third sensor is not correct.

That is not true. You can have as many as you want or need to make the code readable. I usually have one template platform per device, with as many sensors under that as needed. e.g. One template platform for energy sensors, one for my media player sensors, one for my water leak sensors etc…

Check your spacing carefully, it’s all over the place. This is how it should be:

 - platform: template
   sensors:
     totaal_aantal_lampen_aan:
       friendly_name: 'Totaal Aantal lampen aan'
       value_template: "{{ states.light | selectattr( 'state', 'eq', 'on') | list | count }}"
     
  - platform: template
    sensors:
      totaal_aantal_lampen_uit:
        friendly_name: "Totaal Aantal lampen uit"
        value_template: "{{ states.light | selectattr( 'state', 'eq', 'off') | list | count }}"
     
  - platform: template
    sensors:
      totaal_aantal_lampen_niet_beschikbaar:
        friendly_name: "Totaal Aantal lampen niet beschikbaar"
        value_template: "{{ states.light | selectattr( 'state', 'eq', 'unavailable') | list | count }}"
2 Likes

Yeah, check out my configuration. I have about 10 template sensor sections to keep things ‘semi’ organized.

Thanks for the clarification. I confused that with a complete “sensor” block in a package.