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.

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.