Count - Power Sockets with a special entity id

Hello Community,

I´m a zero coder so I have no clue how I can achieve my request.
I want to count all power sockets which are ON and have a special entitiy_id containing “switch.steckdose_”.

All my power plugs are named with the same schema steckdose_ so I believe thats the only way to sort it out.

For my lights I found something to count all lights, which are not groups - but I have no idea how the syntax is working or at least how I can get out from developer tools the infos I need:
{{ states.light | selectattr(‘state’,‘eq’,‘on’) | rejectattr(‘attributes.entity_id’, ‘defined’) | list|count }}

Thanks in advance for your help.

Greetings
Electro

Switches are just more of the same. What you put after the type is not relevant to the template as written.

what this does is:

states.light → gives you all the light states
| filter (or pipe) to select the state that is on
| filter to reject define
| filter to list (make a list of the values)
| filter to count them

{{ states.switch | selectattr('state','eq','on') | rejectattr('attributes.entity_id', 'defined') | list|count }}

1 Like

What is the parameter for CONTAINS or STARTS WITH? Or is the only way to work with a filter like that.

{% set filter = [’ [switch.sonos_sonos_beam_surround_enabled’] %}
{{ states.switch | selectattr(‘state’,‘eq’,‘on’) | rejectattr(‘entity_id’, ‘in’, filter) | list|count }}

But a filter make it really static :frowning:

I figured it our :slight_smile:

gt/lt makes the magic!

{{ states.switch | selectattr('state','eq','on') | rejectattr('entity_id', 'lt', 'switch.steckdose_') | list|count }}

You’re relying on a side effect and programming-wise it isn’t idiomatic. Perhaps check the regex functions in the docs: Templating - Home Assistant.

mmmh… then I need to search further!

{{ states.switch | regex_findall_index("switch.steckdose_") | selectattr('state','eq','on') | list|count }}

ends in: IndexError: list index out of range

Try this:

{{ states.switch | selectattr('state','eq','on') | selectattr('entity_id', 'match', 'switch.steckdose_') | list | count }}
1 Like

Thank you very much, this is working… I also know why my was never working :frowning:
I used the entity_id name same like the entitiy_name but ID is all in small letters :sleepy:

1 Like