Template tips

To see (almost) the same information in the template editor as you can in the states view you can try the following template in the editor:

Media:
{% for player in states.media_player -%}
.    - {{ player.name }} ({{player.state}})
{% for attr in player.attributes -%}
.      +--  {{ attr }}: {{ player.attributes[attr]}} 
{% endfor %}
{% endfor %}
---------------------------------------------------------------------
Sensors:
{% for state in states.sensor -%}
.    - {{ state.name | lower }}: {{state.state_with_unit}}
{% endfor %}

---------------------------------------------------------------------
All states:
{% for state in states -%}
.    - {{ state.name }}
{% for attr in state.attributes -%}
.      +--  {{ attr }}: {{ state.attributes[attr]}} 
{% endfor %}
{% endfor %}

As a sample output this is how one of my chromecasts are represented:

.    - Chromecast Uppe (playing)
.      +--  volume_level: 1.0 
.      +--  is_volume_muted: False 
.      +--  media_content_id: https://content.viaplay.se/{deviceKey}/barn/vaiana-2016 
.      +--  media_content_type: tvshow 
.      +--  media_duration: 6210.016 
.      +--  media_position: 481.02 
.      +--  media_position_updated_at: 2018-06-06 11:24:04.571020+00:00 
.      +--  media_title: Vaiana 
.      +--  app_id: 6313CF39 
.      +--  app_name: Viaplay 
.      +--  friendly_name: Chromecast Uppe 
.      +--  supported_features: 21437 

Compared to the state page:

I found this useful when experimenting with new templates and hope it can be of use to others.

9 Likes

I use something similar it works with all domains instead of hard coding the media_player domain:

{% set display = ['media_player','sensor'] %} #Change this list to display a different subset of domains.
{% for device in states if device.domain in display -%}
{{ device.entity_id }} ({{ device.state_with_unit.strip() }}) 
{% for attr in device.attributes | sort() -%}
... {{ attr }}: {{ device.attributes[attr] }}
{% endfor %}  
{% endfor %}

5 Likes

These are great.

Do you just keep these snippets in a text file somewhere and copy and paste when you need them?
I just wanted to make sure I hadn’t missed something stupidly obvious…
:roll_eyes:

@chrio @petro

Amazing!, extremely useful.

Yes, I have done this before. Once you do it enough though, you can start to memorize it.

Nah, I keep the snippets in a post on the forum instead. :wink:

1 Like

I liked the idea of specifying the types you are interested in, thanks @petro!

I made a variant of it to help me get lines to copy/paste into my groups config:

{% set filter = ['sensor','switch','light'] %}
{% for device in states if device.domain in filter -%}
      - {{'%-35s'|format(device.entity_id) }} # {{ device.attributes.friendly_name }}
{% endfor %}

Which will produce output like this:
image

the %-35s sets the string length of the device text so that the comments gets lined up nicely.

3 Likes

Just another little tip:

You have: 
{% for state in states.automation -%}
   {% if loop.last %}{{ loop.index }} {% endif %}
{%- endfor %}Automations

Mine is 113 and counting :-). Is there a way to filter the active/inactive ones?

First, if you’re just trying to get a count of automations, this is much simpler:

{{ states.automation|count }} Automations

What do you mean by “active” and “inactive”? If you mean, e.g., you want a count of the automations that are ‘on’, then:

{{ states.automation|selectattr('state','eq','on')|list|count }} Automations are on
1 Like

Great! Much better. And you are right, I mean the automations that are on and not disabled.