Template Badge Question - number of lights on

I saw in the lovelace mushroom update this month an example of a template badge that shows the number of lights on. I’d love to do this, but have no idea how to proceed. Any pointers from the experts out there?

{{ states.light | selectattr('state','eq','on') | list | count }}
1 Like

Thanks! Where do I put this code?

Nevermind, I figured it out. Pretty slick. Thanks man!

If you did figure it out, please tell us how. I was looking for that when I found this post.

1 Like

I try a sligthly more advanced code, but it seems not to work, maybe someone has an idea (looks like the 2nd selectattr is the issue).

{{states.cover
  |selectattr('state', 'in', 'open')
  |selectattr('attributes.device_class', 'eq', 'shutter')
  |list
  |count
}}

Try adding “selectattr(‘attributes.device_class’, ‘defined’)” before.

1 Like

Hi @kevandcan, please mark the post that helped you as the solution so anyone who’s searching for this can benefit from it.
You do that by selecting the three dots under the post:

image

Then select the check box:

image

Also, by doing so this prevents that someone else steps in for trying to help.

No, in fact

selectattr('attributes.device_class', 'eq', 'shutter')

for some reasons seems not computed OK in a badge

I told you how to fix it.

Thanks @Ildar_Gabdullin but:

This is the 3rd time I am telling you about this check:

Yes, did that too, does not work. Let’s keep cool :sunglasses:

Eventually this code does what I want, if someone is interested

{{ expand('cover.volets_tous')
  |map(attribute='attributes.current_position')
  |select('eq', 0)
  |list
  |count
}}

Very interesting.
Can you post the last code with selectattr which does not work?
Also, why do you think it does not work? Can you see any errors in Log?

Does not work = badge displaying nothing.

I think the last code I tried was:

{{states.cover
  |selectattr('attributes.device_class', 'eq', 'shutter')
  |selectattr('state', 'in', 'open')
  |list
  |count
}}

Works fine in Dev. Tools Templates, not in badge.

4th time…
Try adding this before 1st selectattr: Template Badge Question - number of lights on - #9 by Ildar_Gabdullin

Thanks for your patience @Ildar_Gabdullin !
I must be blind, never understood your first post properly :disappointed:

Indeed, this code works fine!

{{states.cover
  |selectattr('attributes.device_class', 'defined')
  |selectattr('attributes.device_class', 'eq', 'shutter')
  |selectattr('state', 'in', 'open')
  |list
  |count
}}

Welcome to the club, we all are sometimes)

1 Like