Help with a sensor (i think)

That is what you would expect for that tool as it has no idea that you are defining a template sensor. A template sensor will look in the value_template key and not include the key in the result.

Apologies for the error Finity spotted. I have updated my post.

Please learn how to post formatted code. See point 11 here: How to help us help you - or How to ask a good question

The template you want is:

value_template: >
  {{ states|selectattr('entity_id','in',state_attr('group.switch','entity_id'))|selectattr('state','eq','on')|list|count }}
1 Like

Ok, Thank you very much for your help, I already have everything working thanks to your help, now I have only seen that the template sensor does not self-update but I will generate an automatism for it, calling service.

Thank you!! :wink:

Or you could add a time sensor entity to the template sensor to update it every minute:

sensor:
  platform: template
  sensors: 
    number_of_lights_on:
      friendly_name: "Number of Lights On"
      unit_of_measurement: 'Lights'
      entity_id: sensor.time
      value_template: >
        {% set domain = 'light' %}
        {% set count = states[domain] | selectattr('state','eq', 'on') | map(attribute='entity_id') | list | count %}
        {{ count }}

If you don’t have a sensor.time you will have to add it: