Itâs likely to reveal that a switch entity isnât limited to controlling a light and is also used for other purposes (for example, enabling/disabling a feature in a device).
You will need to find some characteristic that is common exclusively to your 4 light-controlling switches and use it in your template to filter out the other switches.
Maybe your first post didnât explain what you were attempting to achieve but your alleged Solution is dramatically different from what the original template attempted.
It changed from checking a dynamic list of all switches in the system to checking a hard-coded list of four five switches (edit: you originally said you only have 4 switches but your template has 5).
I suggest you add a label to each switch, and to any future additional light switches, and then you can check them dynamically (i.e. no hard-coded entity_ids).
For example, if you label them âlightswitchâ you can use this:
BTW, how can I implement this on sensorâs battery? I want to count batteries that are above 60%, 30% and 10%. I already setup labels for the sensorâs battery.
There are several existing blueprints designed for handling entities with a âLow Batteryâ. You may wish to explore them and see if any meet your requirements.
If you just want a quick list of entities, copy-paste the following template into the Template Editor. It reports all entities with a âbatteryâ label (change it to match whatever label you created) that have a state value less than 30.
{% set b = label_entities('battery') %}
{% set eid = b | select('has_value') | list %}
{% set val = eid | map('states') | map('float', 0) | list %}
{% set z = zip(eid, val)| selectattr(1, 'lt', 30) | list %}
{{ z | map('join', ': ') | join('\n') }}
It doesnât work on my end. I changed the label to label that I set which is âsensorbatteryâ but it doesnât count. I managed to find a solution to your previous code that I used in counting lights. Here it is:
I tested it on my system and it correctly identified five entities whose state value is less than 30. It presented the results as a list of entity_ids and corresponding values.
{% set b = label_entities('battery') %}
{% set eid = b | select('has_value') | list %}
{% set val = eid | map('states') | map('float', 0) | list %}
{% set nam = eid | expand | map(attribute='name') | list %}
{% set z = zip(nam, val)| selectattr(1, 'lt', 30) | list %}
{{ z | map('join', ': ') | join('\n') }}
so we can also do this directly on the b variable:
{% set b = label_entities('batterij') %}
{% set val = b | map('states') | map('float', 0) %}
{% set nam = b | expand | map(attribute='name') %}
{% set z = zip(nam, val)| selectattr(1, 'lt', 30) %}
{{ z | map('join', ': ') | join('\n') }}
update
except⌠the listings are not correct at all, many of these battery levels simply are not the ones mapped to the devices.
and I can list that for all of the other sensorsâŚ
now what is wrong here.
if I use my namespace template
{%- set alert_level = states('input_number.battery_alert_level')|int %}
{%- set ns = namespace(batt_low=[]) %}
{%- set ns = namespace(batt_low=[]) %}
{%- for s in label_entities('batterij')
if is_number(states(s)) and states(s)|int < alert_level
or not has_value(s) %}
{%- set ns.batt_low = ns.batt_low + [s] %}
{%- endfor %}
{{ns.batt_low}}
it lists the correct entities (I did check with the hardcoded 30, as in the zip example too)
I am not yet able to find the issue hereâŚ
but it does find a lot of 100% batteries and lists them as 0, maybe the default kicking in
itâs the expand | map(attribute='name') that frustrates the zip, as expand sorts the output.
using {% set nam = b | map('state_attr', 'friendly_name') %} solves that, so we should do:
{% set b = label_entities('batterij') %}
{% set val = b | map('states')| map('float',-1) %}
{% set nam = b | map('state_attr', 'friendly_name') %}
{% set z = zip(nam, val) %}
{{ z | map('join', ': ') | join('\n') }}
thanks to @hilburn for informing me in Discord
adding some more spice, sorting on val and using a dynamic alert_level:
{% set b = label_entities('batterij') %}
{% set alert_level = states('input_number.battery_alert_level')|int %}
{% set val = b | map('states')| map('float',-1) %}
{% set nam = b | map('state_attr', 'friendly_name') %}
{% set z = zip(nam, val)| selectattr(1, 'lt', alert_level)|sort(attribute=1) %}
{{ z | map('join', ': ') | join('\n') }}