The most Powerful card in HOme Assistant

Short Intro

Home Assistant is truly the pinnacle of what embodies a successful open source project. Its limitless possibilities and its thriving community and developers who work together and share knowledge make it a platform that keeps getting better every day.

Today I want to honor one of those contributors, Thomas Loven. On Nov 8, 2019, he released the auto-entities card for Home Assistant. If you don’t know what auto-entities is, it is perhaps the most powerful card you can use when building custom dashboards in Home Assistant.

On first glance it seems rather simple. A card that can automatically list all your devices based on a filter where the filter can be domain, area, name etc. With auto-entities you have a lot of choices and in this post we will look into one of these choices and what we will look into specifically is templating.


Templating

Templates for those of you that don’t know, is a piece of code that can be written within a card for logical expressions. Lits like programming inside a card - almost, but lets not get bogged down in details. Combining templating with auto entities suddenly provides the card with “Superpowers” and I absolutely mean it. Lets look at an example using yaml.

Standard Atuo-Entities

As mentioned, auto-entities is a rather simple card at first, but it has many secrets to uncover. Before we look into exactly what there is to uncover, lets take a look at the standard way of doing things when it comes to auto-entities:

Regular Auto-entities

It looks boring, is non intuitive and quickly becomes just a long list of entities unless you specify exactly what you want!

Show full code Hide code
All lights — Entities card
type: custom:auto-entities
card:
  type: entities
  title: All lights
  show_header_toggle: true
filter:
  include:
    - domain: light
  exclude:
    - state: unavailable
sort:
  method: name
  ignore_case: true
show_empty: false

For the above configuration we select a domain (lights) to show in the card and then we say that we want to exclude the ones that are unavailable. This is fine, but its very simple and very basic. We don’t have any sorting except by name, we show ALL lights within the Home Assistant installation (that means groups of lights and lights within groups) and you get a long list with a simple toggle. As mentioned, this is fine, but it needs more in order to be properly useful.

Let’s say you want to dim a light or change its color, you would then need to perform a “hold” action on that light and wait for the light popup to show in order to change those settings. Also there is no sorting other than by name so it is easy to turn on/off the incorrect light.

Let the magic begin!

Now, if we instead introduce a bit of templating into this card we will get something that kind of works like magic:

We can turn the boring flat regular auto-entities configuration into something that is sorted, listed by areas and that has all the control readily available at the touch of a finger.

And the absolute best thing about it? It auto updates when you add, remove and change your areas and lights around. That means that there is no more need for updating the card if you add more lights!

Since we render the lights using mushroom-light-cards, we can also show the individual controls for each light automatically if they are present!

And the best part?

Scaling works out of the box.

The card will scale automatically to almost any device! A browser in 4k, a tablet, a phone etc.

How?

So how exactly does this work?

Well, the answer is complex, but with templating we have the power to:

  1. For each area, add a thin divider and a header row with the area name.

  2. We then list that area’s lights as Mushroom light cards with brightness, color temp, and color controls ( if they are present for the device they will be rendered)

  3. If no areas exist, we show a “No Areas found” message telling the user to create areas and add lights.

  4. Hidden lights are excluded, lights are sorted by name, and the parent entities card shows a header toggle that will turn off ALL the lights if pressed.

Show full code Hide code
All lights — Auto-entities by area
type: custom:auto-entities
card:
  show_header_toggle: true
  type: entities
filter:
  template: >-
    {%- set areas = states.light | map(attribute='entity_id') | map('area_name') | unique | reject('none') | list | sort -%}
    {% if areas | length == 0 %}
    [
      {
        'type': 'custom:button-text-card',
        'title': 'No Areas found in the Home Assistant installation',
        'subtitle': 'Create some areas and populate them with soem lights',
        'icon': 'mdi:chat-question',
        'background_color': 'var(--primary-color)',
      }
    ]
    {% else %}
    [
    {%- for area in areas %}
      {
        'type': 'custom:button-card',
        'styles': {
          'card': [
            { 'height': '5px' },
            { 'max-height': '10px' },
            { 'padding': '0' },
            { 'margin': '0' },
            { 'background-color': 'var(--primary-color)' }
          ]
        }
      },
      {
        'type': 'custom:button-text-card',
        'title': '{{ area }}',
        'icon': 'mdi:lightbulb',
        'background_color': 'var(--primary-color)',
      },
      {%- set lights = states.light | selectattr('entity_id', 'in', area_entities(area)) | sort(attribute='name') -%}
      {%- for light in lights %}
      {
        'type': 'custom:mushroom-light-card',
        'entity': '{{ light.entity_id }}',
        'use_light_color': 'true',
        'show_brightness_control': 'true',
        'show_color_control': 'true',
        'collapsible_controls': 'false',
        'show_color_temp_control': 'true',
        'styles': { 'card': [ { 'border-radius': '20px' } ] }
      }
      {%- if not loop.last %},{% endif %}
      {%- endfor %}
      {%- if not loop.last %},{% endif %}
    {%- endfor %}
    ]
    {% endif %}
  exclude:
    - hidden_by: "*"

Conclusion


Perhaps after reading this and testing it out you now understand WHY auto-entities is a “supercard” in Home Assistant.

Auto-entities has the possibility of basically replicating ANY card, with ANY design, with NESTED card configurations based on your needs - auto populated with specific devices/domains/entities etc. Now am I saying that this is easy and simple to do? No, and it is not. I’ve spent countless hours trying to debug certain jinja template errors that simply makes no sense in order to get the configuration I want to render properly in my dashboards. The fun REALLY starts when you try to nest auto-entities card within an auto-entities card. That will really throw you for a loop if you are not careful with your syntax, but I think that is best served for another blog post.

What I am saying is that basically (and with some work), you can create automatic cards for ALL your different types of entities. Lights, temperature sensors, humidity sensors, active motion sensors and so on.

Anyways, stay tuned for the next blog post regarding Home Assistant and you might learn a thing or two.