Home Assistant: Condition Based Auto Entities Card

Overview

I was looking for a way to show / hide information based on states or attributes of an entity. Example use cases:

  1. Show any windows / doors which are open.
  2. List any low battery devices.

Pre-Requisite

Optional:

  1. Home Assistant Community (HAC)

Required:

  1. lovelace-auto-entities

Use the developer tools to find out more information about your devices which will help with the setup.
auto-entities basic setup

Basics

Create a manual card and switch to the code view. The starting point below defines the type of card (auto-entities) and the look and feel of the entities to be listed. These can be customised to anything you’d like within the documentation
type: 'custom:auto-entities'
card:
type: entities
title: Windows and Doors
filter:
include:
show_empty: true

The example above uses the entities card and sets a title. Add the filters and include directive to the card so it can be saved. These will be explained below.

The show_empty will only make the card visible when there are entities listed. If there are none, it will be as if the card was never there outside of the edit mode.

Show Open Windows/Doors

The main focus are the filters. The sensor I want to bring into this are of a certain device class. All my windows and door entites are binary sensors (on/off) and part of the device class “door” or “window”. With this pattern in mind, I can add two parameters to my “include” filter:
filter:
include:
- domain: binary_sensor
attributes:
device_class: window
- domain: binary_sensor
attributes:
device_class: door

Saving the above should list all entities which are binary sensors with door or window device class.

Next, filter the entities for entities which are only open. This is stored in Home Assistant as a state and verified using the state developer tool.

filter:
include:
- domain: binary_sensor
state: 'on'
attributes:
device_class: window
- domain: binary_sensor
state: 'on'
attributes:
device_class: door

Lastly,

Show Lower Battery Devices

Building on the above, a card that shows low battery entities adds a little bit more complexity. This will use the same starting part of the card.
filter:
include:
- domain: sensor
options:
secondary_info: last-changed
attributes:
device_class: battery
state: <= 15

The above changes to only pick up from sensors which are of class battery. The secondary info displays additional information in an entities card of when the state was last changed.

the state under the domain section filters on the a value reported back by the entity. In the example above, anything with 15(%) or less will be included in the auto-entities card. Along with the show_empty: false the card will only be visible if there is an entity with 15% or less.

Next is to sort the entities in descending (reverse) order so that entities with the lowest battery will appear at the top.

sort:
method: state
reverse: true
nurmeric: true

The should above sets the sort order based on the state, in reverse order and the thing it is sorting on (state) is a number.

Summary

The addon is a little more complicated than some of the out of the box cards but it is also very powerful once you can see how the filters and sorting work. Additional things I've used it for are where there is water detected when there should not be and when lights are turned on.

YAML

Open windows and doors:
type: 'custom:auto-entities'
card:
type: entities
title: Open Entrances
filter:
include:
- domain: binary_sensor
options:
secondary_info: last-changed
state: 'on'
attributes:
device_class: window
- domain: binary_sensor
options:
secondary_info: last-changed
state: 'on'
attributes:
device_class: door
exclude:
- entity_id: '*blind*'
show_empty: true
sort:
method: state
ignore_case: true

type: 'custom:auto-entities'
card:
type: entities
title: Low Batteries
filter:
include:
- domain: sensor
options:
secondary_info: last-changed
attributes:
device_class: battery
state: <= 15 show_empty: false sort: method: state reverse: true ignore_case: true nurmeric: true

About Danny

I.T software professional always studying and applying the knowledge gained and one way of doing this is to blog. Danny also has participates in a part time project called Energy@Home [http://code.google.com/p/energyathome/] for monitoring energy usage on a premise. Dedicated to I.T since studying pure Information Technology since the age of 16, Danny Tsang working in the field that he has aimed for since leaving school. View all posts by Danny → This entry was posted in Houseware, IOT, Virtualization and tagged , , , , . Bookmark the permalink.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.