Template to display loaded components on HA instance?

Thanks ! Will test today.

Ok ok, so it does work! That’s what I thought. I wasn’t sure how customUI handled it’s tagging for the colors.

if you’re interested, Ill post the custom-ui card again for your reference.

can we have other ways of sorting the listing?
maybe add an empty line per letter, or group by component.[0]. many config.x config.y config.z for example, or the binary_sensor and sensor which have more than 1 subcomponent.

Also, I know you like short configs. This will be the same as @apop’s:

type: 'custom:card-templater'
card:
  content_template: >-
    {{ "<details>\n" + state_attr('sensor.my_config','components') | sort | join('\n') + "\n</details>" }}
  title_template: '{{states.sensor.my_config.attributes.components|length}} Components Loaded'
  type: markdown
entities:
  - sensor.my_config
1 Like

cool, thanks! will test when I install the Lovelace cards this evening.

would that be possible in the python card setup too? so I can show that in the regular HA I am still using.

Not sure what you mean here

sorry, I responded to the wrong post I fear, thought you had found a way too establish what I asked here:

this should work but i’m not sure if you’ll like the format

components = hass.states.get('sensor.hassio_main_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('{}: {}'.format(key, ', '.join(value)))
    else:
        complist.append(key)

# join each component with a carriage return
complist = '\n'.join(complist)

text = '*========== {} Loaded Components ========\n+'.format(cnt)
text = text + complist

hass.states.set('sensor.hassio_main_components', len(cnt), {
        'custom_ui_state_card': 'state-card-value_only',
        'text': text
    })

what’s there not to like :wink:

except for this error…

Sat May 04 2019 12:51:03 GMT+0200 (CEST)

Error executing script: too many values to unpack (expected 2)
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/python_script.py", line 166, in execute
    exec(compiled.code, restricted_globals, local)
  File "components_ordered.py", line 14, in <module>
ValueError: too many values to unpack (expected 2)

Just updated, forgot call.

get this error:

Error executing script: object of type 'int' has no len()
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/homeassistant/components/python_script.py", line 166, in execute
    exec(compiled.code, restricted_globals, local)
  File "components_ordered.py", line 30, in <module>
TypeError: object of type 'int' has no len()

so changed the sensor set to

hass.states.set('sensor.hassio_main_components_ordered', cnt, {
        'custom_ui_state_card': 'state-card-value_only',
        'text': text
    })

which is very promising. It has an error though: it puts these 3 components:

weather, weather.buienradar, weather.darksky,

all under weather:

weather: buienradar, darksky

and does so with all components, that also have sub.keys. While in fact they are individual components. Maybe that can be added to the script?

  - type: 'custom:card-templater'
    card:
      content_template: >
        {{ "<details>\n" + state_attr('sensor.hassio_main_config','components') | sort | join('\n') + "\n</details>" }}
      title_template: '{{states.sensor.hassio_main_config.attributes.components|length}} Components Loaded'
      type: markdown
    entities:
      - sensor.hassio_main_config

errors out with:
03

while @apop 's template simply doesnt show up at all… dl the latest cards (card-tools, templater and installed properly) refreshed etc etc…

I thought that’s what you wanted. Those are all weather attributes why list weather 10 times when you can list weather once with each sub domain?

you might be right… I was thinking of the weather component and the weather sensor, and first glance made me think those were listed together now. But as you can see here:

08
my weather components are listed both under sensor and weather. So, this is indeed correct! Magic.

Only thing is, I can’t color my components globally any more…

Only the first gets the color code.
Ive added a coding to the complist, but that too has yet to be refined… preferably have the main component be bold eg, and the attributes italic (to give an example)

this is what I tried:

components = hass.states.get('sensor.hassio_main_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('#{}: {}'.format(key, ', '.join(value)))
    else:
        complist.append(key)

# join each component with a carriage return
complist = '\n'.join(complist)

text = '*========== {} Loaded Components ========\n' \
       '%{}'.format(cnt, complist)

#text = text + complist

hass.states.set('sensor.hassio_main_components_ordered', cnt, {
        'custom_ui_state_card': 'state-card-value_only',
        'text': text
    })

resulting in (see alert):

04

still, making progress:

24

using:

complist.append('!{}: \n #--> {}'.format(key, ', '.join(value)))

if you want to color every line, just add it to the join and place one in the begining.

components = hass.states.get('sensor.hassio_main_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('{}: {}'.format(key, ', '.join(value)))
    else:
        complist.append(key)

# join each component with a carriage return
complist = '\n+'.join(complist)
#            /\
#            |
#          Color

text = '*========== {} Loaded Components ========\n+'.format(cnt)
#                                                 /\
#                                                 |
#                                               Color

text = text + complist

hass.states.set('sensor.hassio_main_components', cnt, {
        'custom_ui_state_card': 'state-card-value_only',
        'text': text
    })

almost!:

30

as you can see the ! is displayed instead of used as a color code for the domains with attributes in
complist.append('!{}: \n #--> {}'.format(key, ', '.join(value)))

I need to somehow separate the building if the list into items I can color separately, or put differently, have the join coding only work for items without attributes.

title: bold, *
single components: green, +
compound components: red, ! with attributes in blue, #

if not possible at all, Id leave the green for the single components out, displaying them in default, while the red and blue signal out the compound items:

35

quite neat!

just add the format to the else statement when checking value

you beat me… Was just testing this:

components = hass.states.get('sensor.hassio_main_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)

#text = text + complist

hass.states.set('sensor.hassio_main_components_ordered', cnt, {
        'custom_ui_state_card': 'state-card-value_only',
        'text': text
    })

which seems to be perfect!

thank you!

1 Like

finalizing this thread, since it has been solved, I post my full script, for anyone with interest in showing the loaded components on a HA instance:

##############################################################################################################
# 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
##############################################################################################################

components = hass.states.get('sensor.hassio_main_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)

#text = text + complist

hass.states.set('sensor.hassio_main_loaded_components', cnt, {
        'custom_ui_state_card': 'state-card-value_only',
        '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
#     })
1 Like

what about non hassio setup?