Help with a sensor (i think)

Yes I understand that it would be the option, count the entities that are in the card that I already have! Would it be possible? As I am a novice and I cannot find a way to do it.

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

You could perhaps try using input_number? You’d have to set up automations/scripts to increment the counter every time a light was switched on, but Alexa certainly will read off the current value on command (don’t know about Google). Values saved this way also survive a reboot, which can be an advantage.

I am doing tests and it seems to work wonderfully … Now I will tell you the final result !!!

For other devices that don’t have a specific domain like lights do, e.g. windows, doors and gates, are all binary sensors, you would have to create a group and count the devices in the group. That template is in one of the links I posted above.

IT WORKS PERFECT!!! Thank you very much, now only one more thing is possible a binary sensor that is recognized as a device, I have adapted your template to the switches and there are some things that I have by PING sensor (binary)

Thank you very much for your help!!!

Perfect I already understood it !!! I am going to try!! Thank you!!

1 Like

I can’t add the binary sensors to the account … it doesn’t recognize them !! :frowning:

I’m not sure what you mean by this. Could you please explain further?

I have several devices of YOURS that adapting the template have been recognized, but I want to add some device that I have configured as a binary sensor, type connected by PING

They can be added, it is a bit what you have explained above but I cannot get the template to recognize the group

What have you tried?

Paste your code here.

sorry for interrupting, for educational reasons I tried to add the above sensor
I have in my configuration

sensor: !include_dir_merge_list sensors/

so I changed the code as below (removed 1st line)

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

but I get the following error

missed comma between flow collection entries at line 7, column 10:
            {% set domain = 'light' %}
             ^

What is wrong?

Ok, then I put it on! Thank you!!

This is the body of the template, where the created group is!

{% set domain = ‘group.switch’ %}
{% set count = states[domain] | selectattr(‘state’,‘eq’, ‘on’) | map(attribute=‘entity_id’) | list | count %}
{{ count }}

This is the content of the group

switch:
name: Dispositivos conectados
entities:

  • switch.bf5616bef6579e310e0hfj
  • switch.bf3d88fafabc06705fo9da
  • switch.bf1e79a21e645aafbdwzlr
  • switch.bf0060aa2c3f394fafeafg
  • binary_sensor.play4
  • binary_sensor.deco_engel
  • binary_sensor.nintendo_switchlite

And this is the error that the template gives me in the HA section to test if it is OK!

Error desconocido al mostrar la plantilla

A multi-line template needs the “>” symbol:

value_template: >
  {% set domain = 'light' %}
  {% set count = states[domain] | selectattr('state','eq', 'on') | map(attribute='entity_id') | list | count %}
  {{ count }}
1 Like

to be honest I tried that yesterday
In the developers tool if I test the template it is working
but it gives the below result

value_template: >
  
  
  4

where I think it should give ‘4’ only (not value_template: >)

if I make a sensor it isn’t working at all
the result is 0
and in logs says

Log Details (WARNING)
Logger: homeassistant.components.template
Source: components/template/__init__.py:52
Integration: template (documentation, issues)
First occurred: 9:37:57 AM (1 occurrences)
Last logged: 9:37:57 AM

Template sensor 'number_of_lights_on' has no entity ids configured to track nor were we able to extract the entities to track from the value template(s). This entity will only be able to be updated manually.

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: