Help with a sensor (i think)

Hello, I am very new to this Home Assistant, and I am learning little by little, and I would like to know if it is viable to know through a sensor, I understand the number of lights and devices that are currently on at a specific time, for example to petition…

Thanks for the help!!

Yes it’s possible. There are a couple of ways of doing this. The easiest is a entity filter card. This is not a sensor this will just generate a list of entities in a card in home assistant:

Check the examples a t the bottom of the page.

Or you can query a group of entities with a template to list which are on:

Or count how many are on:

Ok, but my intention is to ask Google assistant what is the status of the system and answer me right now there are X lights on and X devices connected, that’s why I said about the sensor.

The entity filter theme is already operational on my main screen and it works! But for the voice response ?? Thank you!

Then you want the count in a template sensor.

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.