Domain and Entity Lists
All Domains
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
{%- set d = 'light' %}
{%- set c = 'echo' %}
{%- for state in states[d] %}
{%- if c in state.entity_id.lower() %}
- {{ state.entity_id }}
{%- endif %}
{%- 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 area_id’s
{% set areas = states
| selectattr('attributes.device_class', 'defined')
| map(attribute='entity_id')
| map('area_id') | unique | reject('none') | list %}
{{ areas }}
List all entities with count in an area
{%- 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 %}
Find all media player ids that are playing and part of group
{% set ns = namespace(players = []) %}
{% for player in states.media_player
| selectattr('state', 'eq', 'playing')
| selectattr('attributes.group_members', 'defined')
if player.attributes.group_members[0] == player.entity_id %}
{% set ns.players = ns.players + [player.entity_id] %}
{% endfor %}
{{ ns.players }}
Iterate through a JSON result
{% set outage_json = '{"Outage": [{"Status": "No outage has occurred since program launched."}, {"System In Outage": "No"}, {"Utility Voltage": "239 V"}, {"Utility Voltage Minimum": "238 V"}, {"Utility Voltage Maximum": "240 V"}, {"Utility Threshold Voltage": "156 V"}, {"Utility Pickup Voltage": "190 V"}, {"Startup Delay": "5 s"}, {"Outage Log": ["10-24-2023 20:46:14, Duration: 0:00:00, Estimated Fuel: 44076.36 cubic feet", "10-17-2023 12:11:55, Duration: 0:00:23, Estimated Fuel: 0.00 cubic feet", "07-22-2023 19:47:15, Duration: 0:00:00, Estimated Fuel: 44114.31 cubic feet", "05-21-2023 09:08:52, Duration: 0:00:11, Estimated Fuel: 0.00 cubic feet", "03-30-2023 23:08:28, Duration: 0:00:00, Estimated Fuel: 44168.32 cubic feet", "01-10-2023 06:45:37, Duration: 0:51:42, Estimated Fuel: 1837.74 cubic feet", "01-09-2023 08:48:57, Duration: 14:13:22, Estimated Fuel: 1758.62 cubic feet", "11-04-2022 21:47:47, Duration: 1:46:02, Estimated Fuel: 209.62 cubic feet", "11-04-2022 21:47:24, Duration: 0:00:11, Estimated Fuel: 0.00 cubic feet", "11-03-2022 15:03:51, Duration: 0:00:00, Estimated Fuel: 41639.07 cubic feet", "05-18-2022 18:21:30, Duration: 0:33:12, Estimated Fuel: 107.62 cubic feet", "05-18-2022 18:02:46, Duration: 0:00:11, Estimated Fuel: 0.00 cubic feet", "05-18-2022 12:37:03, Duration: 0:18:34, Estimated Fuel: 36.91 cubic feet", "11-10-2021 04:39:45, Duration: 0:00:21, Estimated Fuel: 0.00 cubic feet", "10-24-2021 15:58:40, Duration: 0:51:49, Estimated Fuel: 967.26 cubic feet", "10-24-2021 10:06:47, Duration: 0:17:04, Estimated Fuel: 133.57 cubic feet", "10-24-2021 09:18:09, Duration: 0:48:37, Estimated Fuel: 107.58 cubic feet", "10-24-2021 08:05:20, Duration: 0:00:13, Estimated Fuel: 0.00 cubic feet"]}]}'|from_json %}
{% set outage_logs = outage_json.Outage[8]["Outage Log"] %}
{% for log in outage_logs %}
- {{ loop.index }}. Outage on: {{ log }}
{% endfor %}