Count The Things: Templating Device Counts in Home Assistant

home assistant

I've been using Home Assistant for a little over a year. I started off small, installing Home Assistant and connecting devices that were already compatible. These included devices such as smart speakers, my printer, router, and Roku. Eventually, I added WiFi light bulbs and then purchased a Zigbee controller to really get my smart home going. While certainly not an extensive setup compared to others, my current setup has 18 Zigbee devices, including temperature sensors, motion sensors, lights, smart plugs, and water leak detectors. With the addition of these devices and a dashboard to control them all, I found one thing missing: a quick view of what was going on with these devices. I decided to address this by adding a series of Mushroom Chips cards and helpers in Home Assistant. First, I created the Binary sensor template helpers. I've included an explanation and the state templates below:

Assumptions/Requirements

  • A working Home Assistant installation
  • One or more of the following device classes: motion, switch, light
  • While not required, my setup displays the entities using Mushroom Chips cards from HACS. Other standard cards such as entity, entities, or the glance card could be substituted.

Overview

  1. Create Binary Sensor Template Helpers
  2. Add the helpers to the dashboard using Mushroom Chips cards.

Binary Sensor Templates

Active Motion Sensors

Counts the number of motion sensors detecting motion.

{{ states.binary_sensor 
  | selectattr('state', 'eq', 'on')
  | selectattr('attributes.device_class', 'eq', 'motion')
  | list
  | count
}}

Lights Turned On

Counts the number of individual lights that are turned on. Light groups are excluded to avoid over-counting.

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

Total Switches Turned On

Counts the number of individual switches or smart plugs that are turned on. Switch groups are excluded to avoid over-counting.

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

Dashboard

After creating these entities, I used Mushroom Chips cards to display the entities on my dashboard. I've included the YAML for cards.

Screenshot%20from%202024-06-10%2019-45-12

type: custom:mushroom-chips-card
chips:
  - type: entity
    entity: sensor.count_total_lights_on
    use_entity_picture: false
    icon_color: amber
    tap_action:
      action: navigate
      navigation_path: /dashboard-kiosk/lighting
  - type: entity
    entity: sensor.count_total_switches_on
    use_entity_picture: false
    icon_color: primary
    tap_action:
      action: navigate
      navigation_path: /dashboard-kiosk/switches
  - type: entity
    entity: sensor.active_motion_sensors
    icon_color: green
    tap_action:
      action: navigate
      navigation_path: /dashboard-kiosk/safety
    hold_action:
      action: none
    double_tap_action:
      action: none
alignment: center

Previous Post Next Post