Creating dashboard using templating

I’m trying to make a dashboard showing showing all sensors having battery as gauge cards, for example:

      - type: gauge
        entity: sensor.sonoff_a4b0007735_battery
        needle: true
        severity:
          green: 50
          yellow: 10
          red: 0

Having many such devices, I’m looking for a way to automatically have a card for each of all the relevant sensors. I’ve tried the templating approach as following (in the dashboard configuration):

{% for state in states.sensor  | selectattr('attributes.device_class', 'defined')  | selectattr('attributes.device_class','==', 'battery') %}
      - type: gauge
        entity: {{ state.entity_id }}
        needle: true
        severity:
          green: 50
          yellow: 10
          red: 0
{% endfor %}

This however doesn’t work:

Unable to parse YAML: YAMLException: missed comma between flow collection entries (121:2) 118 | icon: mdi:tools 119 | badges: [] 120 | cards: 121 | {% for state in states.sensor | ... --------^ 122 | - type: gauge 123 | entity: {{ state.entity_ ...

Note: line 121 is the one containing the start of the template ‘{% for …’

Can somebody advise why and if not possible, suggest other ways to achieve my goal?

You can’t just add templates to lovelace a dashboard if that is what you’re trying to do. You can use custom cards though to achieve your goal.

Like this:

example:

type: custom:auto-entities
show_empty: false
card:
  type: entities
  state_color: true
filter:
  include:
    - attributes: 
        device_class: battery
sort:
  method: state
  reverse: false
  numeric: true

btw, you’re looking for gauges. You’d need to do something like this.

type: custom:auto-entities
filter:
  include:
    - domain: sensor
      attributes:
        device_class: battery
      options:
        type: gauge
        needle: true
        severity:
          green: 50
          yellow: 10
          red: 0
        entities:
          - this.entity_id
card:
  type: custom:layout-card
  layout: vertical
sort:
  method: state
  reverse: false
  numeric: true
2 Likes

Thanks, that sort of did the job. I understand now how this custom card can be used to produce multiple shown data point for the filtered entities. This produces good looking results for cases where card with “type: entities” is wanted, but in my specific case all those gauge cards can only be within one card (“custom:layout-card”) and it doesn’t look good compared to other cards within the same dashboard.

I do miss a more generic ability to generate several cards which are not bound together. If only this can be done using some kind of template (or even more, to be able to use the template engine for the entire board generation)

I believe it would be very expensive to use full templating in the front-end. There are custom template cards/rows as well. But i’d use them sparingly and they don’t produce seperate cards.

The solution doesn’t have to be expensive if it will not be in the front-end. As a idea:
Let HomeAssistant allow templating while creating a new dashboard (e.g. one-time evaluation of the templates) so this would produce the regular (“static”) dashboard code and user can proceed editing it from there.

That’s called lovelace_gen and is already available. :slight_smile: