Developer Templates

Domain and Entity Lists

All Domains

{%- set unique_domains = states | map(attribute='domain') |list | unique | list -%}
{%- for domain in unique_domains -%}
- {{domain + "\n"}}
{%- endfor -%}

Count of domains and entities by domain

{% set ns = namespace(domains=[]) %}
{%- for s in states -%}
{%- set ns.domains = (ns.domains + [s.domain])|unique|list -%}
{%- endfor %}
I have {{ states|length  }} states and {{ ns.domains|length }} domains in Home Assistant.
By domain these are;
{% set c = 0 %}
{%- for domain in ns.domains %}
- {{ states[domain]|length }} {{ domain }}
{%- endfor %}

or this

{%- set domains = ['sensor'] -%}
{%- for domain in domains -%}
  {%- for item in states[domain] -%}
    {% if loop.first %}
{%- set unique_domains = states | map(attribute='domain') |list | unique | list -%}
{%- for domain in unique_domains -%}
{%- set domains = [domain] -%}
{%- for domain in domains -%}
  {%- for item in states[domain] -%}
    {% if loop.first %}
- {{domain |capitalize}} domain has {{ loop.length}} entities
    {%- endif %}
  {%- endfor -%}
{%- endfor -%}
{%- endfor -%}

Filtered Entity List

{%- for item in states['light'] %}
{%- set d = 'light' %}
{%- set c = '' %}
{%- for state in states[d] %}
{%- if c in state.entity_id.lower() %}
      - {{ state.entity_id  }}
{%- endif %}
{%- endfor %}
{%- endfor %}



Or this works too.
- {{ states|selectattr('state', 'ne', 'unavailable')|map(attribute='entity_id')|list|join('\n- ') }}

All entity_id list

{%- for state in states -%}
- {{state.entity_id + "\n"}}
{%- endfor -%}

Filter entity list for all domains

{%- set domains = states | map(attribute='domain') |list | unique | list %}
{%- for item in domains %}
  {%- set d = item %}
  {%- set c = '' %}
    {%- for state in states[d] %}
    {%- if c in state.entity_id.lower() %}
- {{ state.entity_id  }}
    {%- endif %}
  {%- endfor %}
{%- endfor %}

or by Domain
{%- set domains = 'sensor' -%}
- {{ states|selectattr('domain', 'eq', domains)|map(attribute='entity_id')|list|join('\n- ') }}

by domain with counts
{%- set domains = ['sensor'] -%}
{%- for domain in domains -%}
  {%- for item in states[domain] -%}
    {% if loop.first %}
      {{domain |capitalize}} domain has {{ loop.length}} entities
    {% endif %}
  {%- endfor -%}
{%- endfor -%}

Attributes by entity in a domain

{%- set domain_name = 'weather' %} 
{% for state in states[domain_name] -%}
-{{loop.index }} {{ state.entity_id}}
  {% for attr in state.attributes -%}
  - {{ attr}}
  {% endfor %}
{% endfor %}

Auto create lovelace cards from all existing entities

{# @Author: Mahasri Kalavala (a.k.a @skalavala #}

{%- macro plural(name) -%}
  {%- if name[(name|length|int -1):] == "y" -%}
  {{- name[:-1] -}}ies
{%- else -%}
  {{- name -}}s
{%- endif -%}
{%- endmacro -%}

title: My Lovely Home Automation
views:
  - title: Home
    cards:
{%- set domains = states | map(attribute='domain') |list | unique | list %}
{%- for item in states['camera'] %}
      - id: {{ item.entity_id.replace('.', '_') }}
        type: picture-entity      
        title: {{ item.name }}
        entity: {{ item.entity_id }}
        camera_image: {{ item.entity_id }}
        show_info: true
        tap_action: dialog
{% endfor %}
{%- for item in states['media_player'] %}
      - type: media-control
        entity: {{ item.entity_id }}
{% endfor %}
{%- for item in domains if item != "camera" and item != "media_player" %}
      - type: {% if item == "device_tracker" -%}
        entity-filter
        {%- elif item == "camera" -%}
        picture-entity
        {%- else -%}
          entities
        {%- endif %}
{%- if states[item]|list |length|default(0)|int > 1 %}
        title: {{ plural(item.replace('_', ' ') | title)  }}
{%- else %}
        title: {{ item.replace('_', ' ') | title }}
{%- endif %}
        show_header_toggle: true
        entities:
{%- for e in states[item] %}
          - {{ e.entity_id }}
{%- endfor %}
{%- if item == "device_tracker" %}
        state_filter:
          - 'home'
        card:
          type: glance
          title: My Device Trackers
{% endif %}
{% endfor -%}

Area entities

List all areas

{%- set area_name = 'Office' %} 
{%- for area in (area_entities(area_name))  -%}
 {{ 'There are ' + loop.length|string + '  entities in the ' + area_name + ' Area' if loop.first }} 
- {{ area  }}
{%- endfor  %}

Entities and count in given area

{%- set area_name = 'Office' %} 
{%- for area_entity in (area_entities(area_name))  -%}
 {{ 'There are ' + loop.length |string + '  entities in the ' + area_name + ' Area' if loop.first }} 
- {{ area_entity  }}
{%- endfor  %}

All entities in an area with a given entity and condition

{% set entity_id = 'media_player.vizio_two' %} 
{% set cond = 'office' %} 
{% set entities = area_entities(area_id(entity_id)) %}
{% for entity in entities %}
{%- if cond in entity %}
- {{ entity }}
{%- endif %}
{%- endfor %}

Entities in an Area

{% set areaname = 'Dining Room' %} 
{% set entities = area_entities(areaname) %}
{% for entity in entities %}
- {{ entity }}
{%- endfor %}

Entities in domain with a filter

{% set domain_name = 'light' %}
{% set area_filter = 'Office' %}

{% set ns = namespace(areas=[], dom = expand(
  states[domain_name]|selectattr('name','ne','All'),
  )) %}

{% for elm in ns.dom if not area_id(elm.entity_id) in ns.areas%}
   {% set ns.areas = ns.areas + [area_id(elm.entity_id)] %}
{%- endfor %}

{% for area in ns.areas %}
   {%- if area_name(area) == area_filter  %}
        Area Name: {{ area_name(area) }}
          {%- for elm in area_entities(area) if elm in ns.dom|map(attribute="entity_id") %}
          - {{ state_attr(elm, "friendly_name") }}
  {%- endfor %}
  {%- endif %}
{%- endfor %}

Entities in an Area with a Filter

{% set ent_filter = 'off' %}
{% set domain_name = 'light' %}

{% set ns = namespace(areas=[], dom = expand(
  states[domain_name]|selectattr('name','ne','All'),
  ) | selectattr("state","eq", ent_filter) | list) %}

{% for elm in ns.dom if not area_id(elm.entity_id) in ns.areas%}
   {% set ns.areas = ns.areas + [area_id(elm.entity_id)] %}
{%- endfor %}

{% for area in ns.areas %}

Area Name: {{ area_name(area) }}
  {%- for elm in area_entities(area) if elm in ns.dom|map(attribute="entity_id") %}
  - {{ state_attr(elm, "friendly_name") }}
  {%- endfor %}

{%- endfor %}

All area names for a given domain

{% set domain_name = 'light' %}

{% set ns = namespace(areas=[], entities = expand(
  states[domain_name]|selectattr('name','ne','All'),)) %}

{% for elm in ns.entities if not area_id(elm.entity_id) in ns.areas%}
   {% set ns.areas = ns.areas + [area_id(elm.entity_id)] %}
{%- endfor %}

{%- for area in ns.areas %}
Area Name: {{ area_name(area) }}

{%- endfor %}

Integrations

All attributes and states for an integration

{%- set int_name = 'vizio' %} 
{{ expand(integration_entities(int_name)) }}

Entities in each integration filter by domain


{%- set int_name = 'vizio' %} 
{%- for int in (integration_entities(int_name))  -%}
 {{ 'There are ' + loop.length |string + '  entities in the ' + int_name + ' integration' if loop.first }} 
- {{ int  }}
{%- endfor  %}

Names of all integrations

Conditional states

Immediate IF

{{ 'Yes' if is_state('light.kitchen', 'on') else 'No' }}

or

{{ iif(is_state('light.kitchen', 'on'), 'Yes', 'No') }}

Motion states

##https://community.home-assistant.io/t/filtering-in-templates/193266/5
{% for state in states.binary_sensor %}
{% if '' in state.name.lower() %}
 {% for state in states.binary_sensor %}
{%- if 'motion' in state.name.lower() %}
{{ state.name }}: {{ 'motion' if state.state == 'on' else 'no motion' }}
{%- endif %}
{%- endfor %}

Entity States with filters


{% for state in states if state.state not in ['unknown', 'unavailable', 'disconnected'] and 'version' not in state.entity_id and 'ip' not in state.entity_id and 'wifi' not in state.entity_id and 'ssid' not in state.entity_id %}
  name: {{ state.name }}
  State: {{ state.state }}
  Last Updated: {{ state.last_changed }}

  {% if loop.last %}
    -- End of Entity States --
  {% endif %}
{% endfor %}

Entity States with Multiple Name Filters

{% for state in states if state.domain in ['light', 'sensor', 'switch'] -%}
  {% if 'number' not in state.entity_id and
        'status' not in state.entity_id and
        'automation' not in state.entity_id and
        'echo' not in state.entity_id and
        'completion' not in state.entity_id and
        'script' not in state.entity_id and
        'input' not in state.entity_id and
        'update' not in state.entity_id and
        'iphone' not in state.entity_id and
        'sonos' not in state.entity_id and
        'response' not in state.entity_id and
        'slave' not in state.entity_id and
        'connected' not in state.entity_id and
        'utility' not in state.entity_id and
        'generator' not in state.entity_id and
        'roborock' not in state.entity_id and
        'parsonsnas' not in state.entity_id and
        'restart' not in state.entity_id and
        'count' not in state.entity_id and
        'energy' not in state.entity_id and
        'tv' not in state.entity_id and
        'loudness' not in state.entity_id and
        'crossfade' not in state.entity_id and
        '1d' not in state.entity_id and
        '1mon' not in state.entity_id and
        'ip' not in state.entity_id and
        'wifi' not in state.entity_id and
        'ssid' not in state.entity_id and
        'version' not in state.entity_id and
        'progress' not in state.entity_id and
        'last' not in state.entity_id and
        'siren' not in state.entity_id and
        'roku' not in state.entity_id and
        'dell15' not in state.entity_id and
        'espresense' not in state.entity_id and
        'record' not in state.entity_id and
        'infra' not in state.entity_id and
        'low_battery' not in state.entity_id and
        '1min' not in state.entity_id and
        'type' not in state.entity_id and
        'current' not in state.entity_id and
        'log' not in state.entity_id and
        'delay' not in state.entity_id and
        'rln16' not in state.entity_id and
        'frequency' not in state.entity_id and
        'volt' not in state.entity_id and
        'disturb' not in state.entity_id and
        'rainmachine' not in state.entity_id and
        state.state not in ['unknown', 'unavailable', 'idle', 'disconnected'] -%}
    {{ state.entity_id }} : {{ state.state }}
  {% endif -%}
{% endfor %}

Entity States with Domain and Name Filters

{% for state in states if state.domain in ['light', 'sensor', 'switch'] 
  and state.state not in ['unknown', 'inactive', 'unavailable', 'disconnected'] 
  and not ('ip' in state.entity_id 
    or 'event' in state.entity_id 
    or 'ssid' in state.entity_id 
    or 'wifi' in state.entity_id 
    or 'version' in state.entity_id 
    or 'restart' in state.entity_id 
    or 'progress' in state.entity_id 
    or 'status' in state.entity_id 
    or '1mon' in state.entity_id 
    or '1d' in state.entity_id 
    or '1min' in state.entity_id 
    or 'max' in state.entity_id 
    or 'last' in state.entity_id 
    or 'ota' in state.entity_id 
    or 'energy' in state.entity_id 
    or 'completion' in state.entity_id 
    or 'mem' in state.entity_id 
    or 'count' in state.entity_id 
    or 'scan' in state.entity_id 
    or 'infra' in state.entity_id 
    or 'disturb' in state.entity_id 
    or 'loudness' in state.entity_id 
    or 'crossfade' in state.entity_id 
    or 'full' in state.entity_id 
    or 'format' in state.entity_id 
    or 'connected' in state.entity_id 
    or 'slot' in state.entity_id 
    or 'voltage' in state.entity_id 
    or 'parsonsnas' in state.entity_id 
    or 'dell15' in state.entity_id 
    or 'rainmachine' in state.entity_id 
    or 'sonos' in state.entity_id) -%}
  Entity: {{ state.entity_id }}
  State: {{ state.state }}
  Name: {{ state.name }}
  Last Updated: {{ state.last_changed }}

  {% if loop.last -%}
    -- End of Entity States --
  {% endif -%}  
{% endfor %}

Devices by Area

{%- for area in areas() %}
  {%- set area_info = namespace(printed=false) %}
  {%- for device in area_devices(area) -%}
    {%- if not device_attr(device, "disabled_by") and not device_attr(device, "entry_type") and device_attr(device, "name") %}
      {%- if not area_info.printed %}

{{ area_name(area) }}:
        {%- set area_info.printed = true %}
      {%- endif %}
- {{ device_attr(device, "name") }}{% if device_attr(device, "model") and (device_attr(device, "model") | string) not in (device_attr(device, "name") | string) %} ({{ device_attr(device, "model") }}){% endif %}
    {%- endif %}
  {%- endfor %}
{%- endfor %}

Sonos

# Get all favorite names as a list (old behavior)
{{ state_attr("sensor.sonos_favorites", "items").values() | list }}

# Pick a specific favorite name by position
{{ (state_attr("sensor.sonos_favorites", "items").values() | list)[3] }}

# Pick a random item's `media_content_id`
{{ state_attr("sensor.sonos_favorites", "items") | list | random }}

# Loop through and grab name & media_content_id
{% for media_id, name in state_attr("sensor.sonos_favorites", "items").items() %}
  {{ name, media_id }}
{% endfor %}