added a dedicated Lovelace bit to the script, which doesn’t use the formatting codes (Markdown card doesn’t handle these at all, so looks a bit silly) One can use markdown code in the card definition, but there’s not much use with this card.
Lovelace output:
btw @petro,
I’ve used your template for the template card, made it multiline and use state_attr(), but the list isn’t using the \n after each component.
this is perfect (so I know the cards first use in my setup works…):
but then this shows:
using this code:
- type: custom:card-templater
card:
content_template: >-
{{ "<details>\n" + state_attr('sensor.mqtt_hub_config','components') | sort | join('\n') + "\n</details>" }}
title_template: >
{{state_attr('sensor.mqtt_hub_config','components')|length}} Components Loaded
type: markdown
entities:
- sensor.mqtt_hub_config
entering that exact details template in dev-template show the correct format:
would that be a card issue, or a template issue…
anyways here’s the adapted python_script for use in lovelace and a markdown card:
##############################################################################################################
# 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: Mqtt hub config
# resource: !secret resource_mqtt_hub_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
#
# use in Lovelace:
# - type: custom:useful-markdown-card
# content: >
# [[ sensor.mqtt_hub_loaded_components.attributes.text_lovelace ]]
##########################################################################################
# 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
##############################################################################################################
components = hass.states.get('sensor.mqtt_hub_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 = []
complistLovelace = []
for key, value in compdict.items():
if value:
value.sort()
# Adding a domain & series of sub domains
complist.append('!- {}: \n /--> {}'.format(key, ', '.join(value)))
complistLovelace.append('- {}: \n --> {}'.format(key, ', '.join(value)))
else:
complist.append('+- {}'.format(key))
complistLovelace.append('- {}'.format(key))
# join each component with a carriage return
complist = '\n'.join(complist)
complistLovelace = '\n'.join(complistLovelace)
text = '*========== {} Loaded Components ========\n' \
'{}'.format(cnt, complist)
textLovelace = '========== {} Loaded Components ========\n' \
'{}'.format(cnt, complistLovelace)
#text = text + complist
hass.states.set('sensor.mqtt_hub_loaded_components', cnt, {
'custom_ui_state_card': 'state-card-value_only',
'text': text,
'text_lovelace': textLovelace
})
##############################################################################################################
# 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
# })