Totals Sensors / Room Lights on, Doors Unlocked, Help filtering

I am trying to make cards that say a tally of total doors open or total lights on.

After some searching, I came up with this

Lights On
{{ states.light|selectattr("state", "equalto", "on")|list|length }}

Doors Unlocked
{{ states.lock|selectattr("state", "equalto", "unlocked")|list|length }}

Doors Open
{{ states.cover|selectattr("state", "equalto", "open")|list|length }}

People Home
{{ states.person|selectattr("state", "equalto", "home")|list|length }}

This allowed me to create cards that displayed the total number of lights on or doors open. They would update instantly in real-time.

This was great except none of the numbers were accurate to the real world because it would pick up duplicate sensors or light rooms and groups. In some cases, a single bulb could increment the number by 5.

I needed a way to filter out some of my groups or entities to make the totals accurate.

I created groups with the lights I wanted to count and created a sensor with this.

{{ states | selectattr('entity_id','in',state_attr('group.room_lights_on','entity_id'))| selectattr('state','equalto','on') |list|length }}

This now fixes my accuracy issue and now shows the correct number of lights on. However; it does not update very quickly at all. In the template editor, it updates, but on a card, even a page refresh doesn’t seem to trigger a number change sometimes.

It seems like using a group slows down the polling. I could use some help figuring out how I can filter out or create a whitelist of entities so that I can get real-time totals numbers that are accurate.

Thanks

I now have a solution to this thanks to Reddit user “whatapitynevermind”

Based on his sugestion of expanding the group first to reduce the polling on the group.

The final resulting sensor is.

{{ expand('group.room_lights_on') | selectattr('state', 'eq' , 'on') | list | count }}

Which responds nearly instantaneously.

1 Like

Thanks for this I stumbled across it on a search as I want similar.
How did you actually use the code above to display in the button is it a custom button with the custom fields ?
I’ve got the code working but can’t work out how to get the number to display on a card now.

The buttons are all custom:button-card in horizontal-stacks within a vertical-stack

2022-02-01 08_24_45-NVIDIA GeForce Overlay DT

Colors change based on status. (Also colors are designed to work with any of the 30 or so themes I have)

The goal was a single pane of glass with every room listed on it, and one tap could do basic room functions such as lights.
As well as a single glance, temp, motion, and cameras (Currently off in screenshot)

Tapping and holding on light gives me a popup dimmer with scene buttons.

and taping on a room name will open a page with all the remaining functions of that room.
Temp graphs, Media Players, Live Camera feed etc. With a large back button at the top.

This allows my wife to launch HASS and get to every smart function in our house within 2 taps.


I also have rows that show up and disappear based on circumstances.
I have a medication reminder with checkboxes that greys out and shrinks away as each pill is taken and the RFID tags scanned.

I have a presence panel with driving location, speed, phone battery status that shows up when one of us is away and disappears when we are both homes.

Others are a notice if my plex drive is low in space.

It’s not perfect, but it’s my attempt to make it quick and simple.

Other example screenshots
This is my nanoleaf lights when I tap and hold.

This is my theme selection page with CSS tiles matching the themes code and an example box so you can top and see what each theme will look like.

I spent a stupid amount of time making sure my buttons will look decent regardless of the theme selected.

1 Like

Hi just got the notification…
Thanks for the reply, my question was was about the top row of cards where you list the number of things opened etc next to the icons.
I have actually managed to get it working of sort using custom button cares and a grid

Ah, that’s a custom-button card within a custom:vertical-stack-in-card

type: custom:vertical-stack-in-card
horizontal: true
cards:
  - type: custom:button-card
    entity: sensor.total_room_lights_on
    tap_action:
      action: navigate
      navigation_path: /ui-rooms/lights-on
    name: Lights
    icon: mdi:door-closed
    show_state: true
    color_type: card
    color: rgba(var(--rgb-primary-color), 0.3)
    state:
      - value: 0
        color: rgba(var(--rgb-primary-color), 0.3)
        styles:
          name:
            - color: '#ffffff24'
          state:
            - color: '#ffffff24'
          icon:
            - color: '#ffffff24'
    styles:
      card:
        - height: 55px
        - '--mdc-ripple-color': var(--sidebar-icon-color)
        - '--mdc-ripple-press-opacity': 0.5
      grid:
        - grid-template-areas: '"i s" "n n"'
        - grid-template-columns: 1fr 1fr
        - grid-template-rows: 1fr 40%
      icon:
        - '--mdc-icon-size': 25px
        - height: 25px
        - width: 25px
        - padding-left: 5px
        - padding-top: 2px
        - color: '#ffffff44'
      name:
        - justify-self: center
        - padding-left: 5px
        - padding-top: 0px
        - font-size: 90%
        - color: '#ffffff88'
1 Like

Thanks for that looks cleaner then mine lol.
I like the look of your other cards though, I keep fancying a Nanoleaf but always talk myself out of it

Beautiful work. Are you using Caule’s Theme?

Yes, I started with his theme pack and added some new shades and variables to work with.