custom_templates/tools.jinja
{% macro list_entities(type, device_class='', state='off', select='', reject='', area='', exclude_area='') %}
{% set ns = namespace(sensors=[]) %}
{% if device_class and area %}
{% for entity in type | selectattr('attributes.device_class', 'eq', device_class) | selectattr('entity_id', 'in', area_entities(area)) | selectattr('state','eq',state) %}
{% set ns.sensors = ns.sensors + [dict(entity_id = entity.entity_id, name = entity.name, state = entity.state)] %}
{% endfor %}
{% elif device_class %}
{% for entity in type | selectattr('attributes.device_class', 'eq', device_class) | selectattr('state','eq',state) %}
{% set ns.sensors = ns.sensors + [dict(entity_id = entity.entity_id, name = entity.name, state = entity.state)] %}
{% endfor %}
{% elif area and reject %}
{% for entity in type | selectattr('entity_id', 'in', area_entities(area)) | rejectattr('entity_id', 'search', reject) | selectattr('state','eq',state) %}
{% set ns.sensors = ns.sensors + [dict(entity_id = entity.entity_id, name = entity.name, state = entity.state)] %}
{% endfor %}
{% elif area and select %}
{% for entity in type | selectattr('entity_id', 'in', area_entities(area)) | selectattr('entity_id', 'match', select) | selectattr('state','eq',state) %}
{% set ns.sensors = ns.sensors + [dict(entity_id = entity.entity_id, name = entity.name, state = entity.state)] %}
{% endfor %}
{% elif area %}
{% for entity in type | selectattr('entity_id', 'in', area_entities(area)) | selectattr('state','eq',state) %}
{% set ns.sensors = ns.sensors + [dict(entity_id = entity.entity_id, name = entity.name, state = entity.state)] %}
{% endfor %}
{% elif exclude_area and reject %}
{% for entity in type | rejectattr('entity_id', 'in', area_entities(exclude_area)) | rejectattr('entity_id', 'search', reject) | selectattr('state','eq',state) %}
{% set ns.sensors = ns.sensors + [dict(entity_id = entity.entity_id, name = entity.name, state = entity.state)] %}
{% endfor %}
{% elif exclude_area %}
{% for entity in type | rejectattr('entity_id', 'in', area_entities(exclude_area)) | selectattr('state','eq',state) %}
{% set ns.sensors = ns.sensors + [dict(entity_id = entity.entity_id, name = entity.name, state = entity.state)] %}
{% endfor %}
{% elif select and reject %}
{% for entity in type | selectattr('entity_id', 'match', select) | rejectattr('entity_id', 'search', reject) | selectattr('state','eq',state) %}
{% set ns.sensors = ns.sensors + [dict(entity_id = entity.entity_id, name = entity.name, state = entity.state)] %}
{% endfor %}
{% elif select %}
{% for entity in type | selectattr('entity_id', 'match', select) | selectattr('state','eq',state) %}
{% set ns.sensors = ns.sensors + [dict(entity_id = entity.entity_id, name = entity.name, state = entity.state)] %}
{% endfor %}
{% elif reject %}
{% for entity in type | rejectattr('entity_id', 'search', reject) | selectattr('state','eq',state) %}
{% set ns.sensors = ns.sensors + [dict(entity_id = entity.entity_id, name = entity.name, state = entity.state)] %}
{% endfor %}
{% endif %}
{{ ns.sensors | to_json }}
{% endmacro %}
{% macro count_entities(type, device_class='', state='off', select='', reject='', area='', exclude_area='') %}
{% from 'tools.jinja' import list_entities %}
{{ list_entities(type,device_class,state,select,reject,area,exclude_area) | from_json | count }}
{% endmacro %}
Batttery Template sensor
- name: "Devices with low battery"
unique_id: e9da96dc-aa51-47c1-8a6d-435de1ce5149
attributes:
navigation: batteries
entities: >
{% set ns = namespace(sensors=[]) %}
{% set ignore_entities = ['sensor.andys_iphone_battery_level','sensor.andys_iphone_icloudv3_battery','sensor.christines_iphone_battery_level','sensor.christines_iphone_icloudv3_battery','sensor.ipad_battery_level','sensor.mums_ipad_battery_level'] %}
{% set battery_level = 55 %}
{% for state in states | selectattr('attributes.device_class', 'eq', 'battery') | rejectattr('entity_id', 'in', ignore_entities) %}
{% if 0 <= state.state | int(-1) <= battery_level | int %}
{% set ns.sensors = ns.sensors + [dict(entity_id = state.entity_id, name = state.name | replace(' battery', '') | replace(' Battery', ''), state = state.state | int)] %}
{% endif %}
{% endfor %}
{% set low_batteries = ns.sensors | sort (attribute='state') %}
{{ low_batteries }}
state: "{{ this.attributes.entities | count }}"
icon: >
{% if this.state not in ['unavailable','unknown'] %}
{% if this.state | int > 0 %}
mdi:battery-alert
{% else %}
mdi:battery
{% endif %}
{% else %}
mdi:battery
{% endif %}
Lights on
- name: "Number of Lights On"
unique_id: 3b279d97-e05a-4c8b-9368-2ce58d7fd46c
attributes:
navigation: lights
label: >
{% if this.state not in ['unavailable','unknown'] %}
{% if this.state | int > 0 %}
{{ this.state }} On
{% else %}
All Off
{% endif %}
{% else %}
All Off
{% endif %}
entities: >
{% from 'tools.jinja' import list_entities %}
{{ list_entities(states.light,exclude_area='groups',state='on') }}
count: >
{% from 'tools.jinja' import count_entities %}
{% set ns = namespace(entities=[]) %}
{% for s in states.light | map(attribute='entity_id') | map('area_name') | unique | reject('none') | list %}
{% set ns.entities = ns.entities + [ (s, count_entities(states.light,state='on',area=s,exclude_area='groups') | from_json )] %}
{% endfor %}
{{ dict.from_keys(ns.entities) }}
state: "{{ this.attributes.entities | count }}"
icon: >
{% if this.state not in ['unavailable','unknown'] %}
{% if this.state | int > 0 %}
mdi:lightbulb-group
{% else %}
mdi:lightbulb-group-outline
{% endif %}
{% else %}
mdi:lightbulb-group-outline
{% endif %}
Doors Open
- name: "Number of Doors Open"
unique_id: 905cf252-7adb-4b68-b696-ae089d96b11f
attributes:
navigation: doors
label: >
{% if this.state not in ['unavailable','unknown'] %}
{% if this.state | int > 0 %}
{{ this.state }} Open
{% else %}
All Closed
{% endif %}
{% else %}
All Closed
{% endif %}
entities: >
{% from 'tools.jinja' import list_entities %}
{{ list_entities(states.binary_sensor,device_class='door',state='on') }}
count: >
{% from 'tools.jinja' import count_entities %}
{% set ns = namespace(entities=[]) %}
{% for s in states.binary_sensor | map(attribute='entity_id') | map('area_name') | unique | reject('none') | list %}
{% set ns.entities = ns.entities + [ (s, count_entities(states.binary_sensor,device_class='door',state='on',area=s) | from_json )] %}
{% endfor %}
{{ dict.from_keys(ns.entities) }}
state: "{{ this.attributes.entities | count }}"
icon: >
{% if this.state not in ['unavailable','unknown'] %}
{% if this.state | int > 0 %}
mdi:door-open
{% else %}
mdi:door
{% endif %}
{% else %}
mdi:door
{% endif %}
Sockets on
- name: "Number of Sockets On"
unique_id: c838cee8-1919-4000-8fc0-d6587433e6fc
attributes:
navigation: sockets
label: >
{% if this.state not in ['unavailable','unknown'] %}
{% if this.state | int > 0 %}
{{ this.state }} On
{% else %}
All Off
{% endif %}
{% else %}
All Off
{% endif %}
entities: >
{% from 'tools.jinja' import list_entities %}
{{ list_entities(states.switch,device_class='outlet',state='on') }}
count: >
{% from 'tools.jinja' import count_entities %}
{% set ns = namespace(entities=[]) %}
{% for s in states.switch | map(attribute='entity_id') | map('area_name') | unique | reject('none') | list %}
{% set ns.entities = ns.entities + [ (s, count_entities(states.switch,device_class='outlet',state='on',area=s) | from_json )] %}
{% endfor %}
{{ dict.from_keys(ns.entities) }}
state: "{{ this.attributes.entities | count }}"
icon: mdi:power-socket-uk
Motion Detected
- name: "Number of Motion Detected"
unique_id: 156e99a5-cdb6-4d6b-9bea-5e05d5a3c548
attributes:
navigation: motion-sensors
label: >
{% if this.state not in ['unavailable','unknown'] %}
{% if this.state | int > 0 %}
{{ this.state }} Detected
{% else %}
All Clear
{% endif %}
{% else %}
All Clear
{% endif %}
entities: >
{% from 'tools.jinja' import list_entities %}
{{ list_entities(states.binary_sensor,device_class='motion',state='on') }}
count: >
{% from 'tools.jinja' import count_entities %}
{% set ns = namespace(entities=[]) %}
{% for s in states.binary_sensor | map(attribute='entity_id') | map('area_name') | unique | reject('none') | list %}
{% set ns.entities = ns.entities + [ (s, count_entities(states.binary_sensor,device_class='motion',state='on',area=s) | from_json )] %}
{% endfor %}
{{ dict.from_keys(ns.entities) }}
state: "{{ this.attributes.entities | count }}"
icon: mdi:motion-sensor
for sidebar
- type: horizontal-stack
cards:
- type: custom:button-card
entity: sensor.number_of_lights_on
template:
- custom_card_andyblac_count_info
- custom_popup_andyblac_tap
variables:
ulm_custom_card_andyblac_count_info_color_on: yellow
popup_title: "Lights"
popup_content: !include popups/lights.yaml
# hold_action:
# action: navigate
- type: custom:button-card
entity: sensor.number_of_sockets_on
template:
- custom_card_andyblac_count_info
- custom_popup_andyblac_tap
variables:
ulm_custom_card_andyblac_count_info_color_on: blue
popup_title: "Sockets"
popup_content: !include popups/sockets.yaml
# hold_action:
# action: navigate
- type: custom:button-card
entity: sensor.number_of_doors_open
template:
- custom_card_andyblac_count_info
- custom_popup_andyblac_tap
variables:
ulm_custom_card_andyblac_count_info_color_on: blue
popup_title: "Doors"
popup_content: !include popups/doors.yaml
- type: custom:button-card
entity: sensor.number_of_motion_detected
template:
- custom_card_andyblac_count_info
- custom_popup_andyblac_tap
variables:
ulm_custom_card_andyblac_count_info_color_on: blue
popup_title: "Motion"
popup_content: !include popups/motion.yaml
- type: custom:button-card
entity: sensor.devices_with_low_battery
template:
- custom_card_andyblac_count_info
- custom_popup_andyblac_tap
variables:
ulm_custom_card_andyblac_count_info_color_on: blue
popup_title: "Low Batteries"
popup_content: !include popups/battery.yaml
for individual room
- type: custom:mushroom-chips-card
view_layout:
grid-area: header
chips:
- type: back
- type: entity
entity: binary_sensor.office_door_contact
content_info: none
tap_action:
action: more-info
- type: entity
entity: sensor.wiser_lts_temperature_office
- type: template
icon: mdi:power-socket-uk
content: "{{ state_attr('sensor.number_of_sockets_on', 'count')['Office'] }}"
- type: template
icon: mdi:lightbulb
content: "{{ state_attr('sensor.number_of_lights_on', 'count')['Office'] }}"