Join current_power_w of all switch

Well you’re going to hate this result. If I remove the final filter (max) the result is an empty list []. Here’s the part you’re gonna hate, if I remove this line:

{{current_power_list | list | length}}

now the result is what one would expect:

[3000, 4000, 1000, 2000, 5000]

?!?

this is my error

But when you add max it complains? I’m confused. If the attribute doesn’t exist it shouldn’t be an empty list, it should be a list of ‘undefined’.

Ok i figured it out. Generators can only be accessed once. Kinda lame but ok, i get it.

I think this should work. Let me test on my end.

{% set switches = [ 'switch.cozinha_maquina_lavar_roupa_38',
                    'switch.escritorio_servidor_cpu_22',
                    'switch.cozinha_cilindro_24',
                    'switch.cozinha_frigorifico_34',
                    'switch.sala_multimedia_32', ] %}
{% set current_power_list = states.switch | selectattr('entity_id','in', switches) | list %}
{% if current_power_list | length > 0 %}
  {% set max = current_power_list | map(attribute='attributes.current_power_w') | list | max %}
  {% set stateobj = current_power_list | selectattr('attributes.current_power_w', 'eq', max) | list %}
  {{ stateobj[0].entity_id }}
{% else %}
  None
{% endif %}

EDIT tested on my end and it should work.

1 Like

Got it. Odd but there it is.

It works. In this example, I added a line to report the attribute’s value, just to confirm it’s reporting the highest one.

1 Like

So generators are a 1 time list. Which makes sense, you can only iterate through it once. Using the list filter turns in into a reusable list of data.

ehehehe its works!! thanks :slight_smile:

By marking petro’s solution to your second question, it unmarked the original solution to your first question. One thread topic cannot have more than one solution.

That means other people who read the first post in this thread, with your original question, will see the wrong solution:

1 Like

ok changed

ok only one more thing can i have two different map(attribute=‘attributes.current_power_w’ and ‘attributes.power’ because i have a switch different of the others?
i got this erros because that switch
Error rendering template: UndefinedError: ‘mappingproxy object’ has no attribute ‘current_power_w’

this is not possible?

No, you can’t have more than one map attribute. There may be ways to do it, but it would be a pain in the ass. You may want to move to a python script instead of a template.