I’d like an overview of all sensor that are unavailable, state = none or unknown. I put together this:
{% for item in states %}
{% if item.state | lower == 'unavailable' or item.state | lower == 'unknown' or item.state | lower == 'none'%}
{{ item.attributes.friendly_name }} is {{ item.state }}
{% endif %}
{% endfor %}
…which lists the sensors correctly in the template window. But it needs some trimming to remove unnecessary spaces and so on. But - how to best show the result? I thought of a templated sensor, but they can only contain 255 characters which may be too little. Suggestions?
I did something like this with my ZWave devices and ran into the 255 character limit. I simply changed the value_template to be on or off and used attribute_templates to record the actual list:
attribute_templates:
entities: >-
{% compute the list here %}
I coded the logic of value_template so that if entities is going to contain devices then the value of the sensor is on, otherwise it is off. I also had to change some of my automation logic: where I previously would use last_changed to know whether my list of entities changed I had to switch to last_updated because last_changed ignores attributes and if the list changes from 2 to 3 devices or there’s any other change to it, that matters to me.
I tried the @petro solution, and it works fine. At first I thought something was wrong since it listed 95 devices after restart, but it just needed an update after the startup phase.
However, now I’d like some help formatting the output. It lists all information about each entity. I only want the entity name and the state
While it now list like this (which is good, but hard to read)…
<template state light.belysning_poolomrade_5=unavailable; friendly_name=Belysning poolområde (5), supported_features=41, icon=mdi:lightbulb, assumed_state=False @ 2019-12-12T15:54:43.056278+01:00>, <template state sensor.latest_alarm_change=unavailable; friendly_name=Senaste förändring, icon=mdi:clock @ 2019-12-12T15:55:06.426483+01:00>, <template state sensor.unavailable=unknown; friendly_name=Sensorer utan data @ 2019-12-12T15:54:42.381244+01:00>, <template state sensor.zxp_qr_code=unknown; friendly_name=zxp QR Code @ 2019-12-12T15:54:42.617662+01:00>
to prevent needing to use an automation to trigger a refresh every minute you could set up the “Time & Date Sensor” then use the “sensor.time” in the config for the “unavailable” sensor you created.
That will cause the sensor to refresh whenever the sensor.time updates (which is every minute0. So you will get the same result as using the automation without the extra code.
building on this, it would be even cooler if we could have the listing show both the entity and its state (because the selection is for 3 possible states now), and even better, list per attribute unavailable, unknown and none.
If been fiddling with groupby but havent found the correct syntax yet…
a bit like this, the loaded components, grouped by integration:
##############################################################################################################
# python script to show the loaded components on a Hassio instance, and order them alphabetically, grouping
# components that have sub components (attributes)
# this script gets its data from the rest_sensor:
# - platform: rest
# name: Hassio Main config
# resource: !secret resource_hassio_main_config
# authentication: basic
# value_template: >
# {{ value_json.version }}
# json_attributes:
# - components
# - unit_system
# - config_dir
# headers:
# Content-Type: application/json
# Authorization: !secret api_bearer_token
# User-Agent: Home Assistant REST sensor
#
##########################################################################################
# Codes for text_colors declared in
# Custom card: /custom_ui/state-card-value_only.html
# changed to use below customizing options:
##########################################################################################
#
# case "*": return "bold";
# case "/": return "italic";
# case "!": return "red";
# case "+": return "green";
# case "=": return "yellow";
# case "%": return "grey";
# case "$": return "brown";
# case "#": return "blue";
# default: return "normal";
#
# https://community.home-assistant.io/t/template-to-display-loaded-components-on-ha-instance/114402/59
# @123 and @apop pointed to the rest sensor, and made that availabe in Lovelace
# @petro had a great hand in creating the script
# thanks for joining in on a great HA community!
# @mariusthvdb 20190504
##############################################################################################################
attributes = {}
components = hass.states.get('sensor.hassio_rpi4_config').attributes['components']
cnt = len(components)
components.sort()
# Make a dictionary of all main domains, add each sub domain to the main domain list.
compdict = {}
for component in components:
if component.count('.') == 0 and component not in compdict:
compdict[component] = []
if component.count('.') == 1:
domain, subdomain = component.split('.')
compdict[domain].append(subdomain)
# Make the dictionary into a flat list of strings.
complist = []
for key, value in compdict.items():
if value:
value.sort()
# Adding a domain & series of sub domains
complist.append('- {}: \n --> {}'.format(key, ', '.join(value)))
else:
complist.append('- {}'.format(key))
# join each component with a carriage return
complist = '\n'.join(complist)
text = '{} Loaded Components:\n' \
'{}'.format(cnt, complist)
attributes['friendly_name'] = 'Components'
attributes['icon'] = 'mdi:format-list-bulleted-type'
attributes['Components'] = cnt
attributes['---------------'] = '--------'
attributes['text'] = text
hass.states.set('sensor.overview_components', cnt, attributes)
# hass.states.set('sensor.overview_components', cnt, {
# 'icon': 'mdi:format-list-bulleted-type',
# 'friendly_name': 'Components',
# 'text': text
# })
##############################################################################################################
# first attempt, simply creating an unordered list of components
# components = hass.states.get('sensor.hassio_main_config').attributes['components']
# count = len(components)
# components.sort()
# list = ', '.join(components)
#
# text = '*========== {} Loaded Components ========\n' \
# '+{}' \
# .format(count,
# list)
#
#
# hass.states.set('sensor.hassio_main_components', count, {
# 'custom_ui_state_card': 'state-card-value_only',
# 'text': text
# })
Whitespace is removed for display in the attributes tab. It may be removed all together. Only way to tell is to look at the attribute in the template editor.
I’d like to use something similar to have a list of binary sensors of a certain gropus, whan I’m leaving home, listed by they’re firendly name is it possible to make a template like this?
I made something like this, starting from your suggestion: