I have read this topic and just use the following code which seems to work fine, updating whenever a light is switched. I have tested with a badge showing the result and triggering lights, it updates almost direct. Running v117.5.
I read something about HA now automatically checking states changes and updating templates in one of the latest versions I think. Maybe that’s why it works now.
The number of lights switched on doesn’t take into account the light-groups automatically imported from the HUE integration. So when I switch on the study_desk light I get two items, study_desk and the study room.
Long time since you posten this but if you see this, maybe you can have a look at my code. I have simplified it but get “Unavailable” on the sensor. Looks like your but for some reason it isn’t working.
This is a fantastic thread and I’m trying to implement a similar count for interior motion detectors. The idea is trigger an automation when two sensor trigger in order to reduce false triggers. Unfortunately implementing this value_template, I’m running into errors.
Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: expected token ‘,’, got ‘_in_1_sensor_home_security_motion_detection’) for dictionary value @ data[‘sensors’][‘multiple_motion’][‘value_template’]. Got “{% set motion = [\n states.binary_sensor.4_in_1_sensor_home_security_motion_detection,\n states.binary_sensor.4_in_1_sensor_home_security_motion_detection_2,\n states.binary_sensor.4_in_1_sensor_home_security_motion_detection_3,\n states.binary_sensor.4_in_1_sensor_home_security_motion_detection_4\n ] %}\n{{ motion | selectattr(‘state’,‘eq’,‘on’) | list | count }}\n”. (See ?, line ?).
Could someone point out my error?
I had read under the “Templating” doc that perhaps I should be using states(‘binary_sensor.4_in_1_sensor_home_security_motion_detection’) to avoid errors, but when I did do that the errors west away, but the template count did not work and stayed stuck at 0.
You should: it’s because these sensors start with a number, the states syntax thinks you’re looking for [4] then finding the rest of the name. Post the code that you tried that didn’t work…
Thanks for the quick reply from you and Troon! Absolutely perfect.
That’s an interesting issue that because the sensor’s name starts with a number we need to approach this differently. Never considered it. The sensor is a Z-Wave sensor that auto populated the name. Will keep this in mind next time.
So that I fully understand, let’s say the sensor name did not start with a number, would the other way work without using ‘expand()’ ?
Did you solved your issue with the automatic light groups of Hue? I also have this “issue”.
I now have this, but that isn’t really pretty.
I made it this way, because I will add more light regular and not rooms, so I don’t have to adjust this template.
# Lights count
- platform: template
sensors:
lightsoncount:
friendly_name: "Lights on count"
value_template: >
{% set total = states.light|selectattr('state','equalto','on')|list|length %}
{% if is_state('light.woonkamer', 'on') %}
{% set total = total - 1 %}
{% endif %}
{% if is_state('light.tuin', 'on') %}
{% set total = total - 1 %}
{% endif %}
{% if is_state('light.alle_lampen', 'on') %}
{% set total = total - 1 %}
{% endif %}
{% if is_state('light.baby', 'on') %}
{% set total = total - 1 %}
{% endif %}
{% if is_state('light.custom_group_for_lights', 'on') %}
{% set total = total - 1 %}
{% endif %}
{% if is_state('light.garage', 'on') %}
{% set total = total - 1 %}
{% endif %}
{% if is_state('light.hal', 'on') %}
{% set total = total - 1 %}
{% endif %}
{% if is_state('light.keuken', 'on') %}
{% set total = total - 1 %}
{% if is_state('light.oprit', 'on') %}
{% set total = total - 1 %}
{% endif %}
{% if is_state('light.slaapkamer_eline', 'on') %}
{% set total = total - 1 %}
{% if is_state('light.vrc_1', 'on') %}
{% set total = total - 1 %}
{% endif %}
{% endif %}
{% endif %}
{{total}}
Is there a way to count the amount of lights, decvices, entities and such that are in a area? I have searched all over this forum but cannot seem to find it specific for counting in area.
Of course, I could make groups and count amount in group, but there must be a cleaner way, right?
Hope someone here can help me out!