{%- for d in states | groupby('domain') %}
{% if loop.first %}{{loop.length}} Domains:
{% endif %}- {{ d[0] }}: {{d[0]|count}}
{%- endfor %}
this has to be corrected, even though it is a very old post…
you are counting the number of characters of that domain name…
this works a bit better:
- unique_id: ha_main_domains
name: Ha main domains
icon: mdi:format-list-numbered
state: >
{{states|groupby('domain')|map(attribute='0')|list|count}}
attributes:
domains: >
{% set x = ['unavailable','unknown'] %}
{%- for d in states|groupby('domain') %}
- {{ d[0]}}: ({{states[d[0]]
|rejectattr('state','in',x)
|list|count}})
{%- endfor -%}
You already have state objects, no reason to access states again. That’s what groupby does.
{% set x = ['unknown', 'unavailable'] %}
{%- for d, es in states | groupby('domain') %}
- {{ d }}: ({{ es | rejectattr('state', 'in', x) | list | count }})
{%- endfor %}
o thats nice!
however, there’s always a caveat, I had this
{% for i in d[1] if i.state not in x -%}
> {{i.name}}: *{{i.state}}*
{% endfor %}
as additional in my markdown (which I simply left out for the post above):
{% set x = ['unavailable','unknown'] %}
{%- for d in states|groupby('domain') %}
{% if loop.first %} ### Domains: *{{loop.length}}* - Entities: *{{states|count}}* {% endif %}
**{{- d[0]}}:** *({{states[d[0]]
|rejectattr('state','in',x)
|list|count}})*
{% for i in d[1] if i.state not in x -%}
> {{i.name}}: *{{i.state}}*
{% endfor %}
{%- endfor -%}
and I cant simply jot that in in your shorter template
id love to get those back in there, and return:
uh, yes you can…/.
{% set x = ['unknown', 'unavailable'] %}
{%- for d, es in states | groupby('domain') %}
- {{ d }}: ({{ es | rejectattr('state', 'in', x) | list | count }})
{% for i in es if i.state not in x -%}
> {{i.name}}: *{{i.state}}*
{% endfor %}
{%- endfor %}
huh, I did that:
{% set x = ['unknown', 'unavailable'] %}
{%- for d, es in states | groupby('domain') %}
- {{ d }}: ({{ es | rejectattr('state', 'in', x) | list | count }})
{% for i in d[1] if i.state not in x -%}
> {{i.name}}: *{{i.state}}*
{% endfor %}
{%- endfor %}
and it threw: UndefinedError: 'str object' has no attribute 'state'
must have been a spacing in the dev editor.
cool, thx!
yeah, because you aren’t using the correct variable
yes!
and now change rejectattr to selectattr and turn this in the ultimate unavailable checker
one thing I had wanted to do was add:
{% set exclude_domains = ['alarm_control_panel','alert','automation','button',
'counter','media_player','proximity','scene'] %}
and have the unavailable checker use that. Any chance you can spot where to do so, in the if statement?
btw, if at all possible, it would be awesome if we could exclude domains without unavailable entities:
{% set x = ['unavailable','unknown'] %}
{% set exclude_domains = ['alarm_control_panel','alert','automation','button',
'counter','media_player','proximity','scene'] %}
{% set x = ['unknown', 'unavailable'] %}
{%- for d, es in states | groupby('domain') %}
**{{ d }}:** *({{es|selectattr('state','in',x)
|list|count}})*
{% for i in es if i.state in x -%}
> {{i.name}}: *{{i.state}}*
{% endfor %}
{%- endfor %}
and for that {{es|selectattr('state','in',x)|list|count}}
== 0 would have to be excluded from the for loop…
yep, if statement using d
heck, I would have sworn I had done that, but now it works:
{%- for d, es in states | groupby('domain') if d not in exclude_domains %}
its remarkable btw, how much this weighs on the frontend. where the dev templates has this in the blink of an eye, frontend takes really long to show upon first reload
because the frontend has to send it to the backend
yes, I understand that.
its just that your format takes way longer than what I posted above. for the frontend that is, cant say for the dev tools, because those both are fast
btw, this fixes the other request:
{%- for d, es in states | groupby('domain') if d not in exclude_domains
and es|selectattr('state','in',x)
|list|count != 0 %}
my format has less calls. I think it’s a fluke on your end. my “format” only accesses the state machine once, yours accesses 1 + every domain you have.
now that you’re here… might I ask if you have a look at another template related to this counting/listing:
- unique_id: ha_main_components
name: Ha main components
icon: mdi:format-list-bulleted-type
state: >
{% set ns = namespace(components=[]) %}
{%- for d in state_attr('sensor.ha_main_config','components')|sort %}
{% set ns.components = ns.components + [d.split('.')[0] ] %}
{%- endfor -%}
{{ns.components|unique|list|count}}
attributes:
components_unique: >
{% set ns = namespace(components=[]) %}
{%- for d in state_attr('sensor.ha_main_config','components')|sort %}
{% set ns.components = ns.components + [d.split('.')[0] ] %}
{%- endfor -%}
{{ns.components|unique|list}}
count_components_raw: >
{{state_attr('sensor.ha_main_config','components')|count}}
components_raw: >
{{state_attr('sensor.ha_main_config','components')|sort}}
I made the state and unique attribute to slim down on
which the ‘raw’ attribute lists.
sensor.ha_main_config
is a rest sensor to the ha instance api.
optimally, I would have wanted to list those integrations under the component like the domain sensors above, so
alarm_control_panel: 5
manual
mqtt
overkiz
zha
etc, but I cant make it happen.
would appreciate if you could have a look.
( we did this before in python, but since Frenck told me not to use has.states.set, Ive moved pout of all python scripts using that, this is the final clincher)
{% for v in items %}
{% if '.' not in v %}
{% set cnt = items | select('contains', 'a') | list | count - 1 %}
{{ v }}: {{ cnt if cnt else '' }}
{% else %}
{{ v.split('.')[-1] }}
{% endif %}
{% endfor %}
Ha, this is indeed getting somewhere:
{% set items = state_attr('sensor.ha_main_config','components')|sort %}
{% for v in items %}
{% if '.' not in v %}
{% set cnt = items | select('contains', 'a') | list | count - 1 %}
{{ v }}: {{ cnt if cnt else '' }}
{% else %}
{{ v.split('.')[-1] }}
{% endif %}
{% endfor %}
but the counter is a bit off:
heck, I cant upload a text file to show you the output I am trying to template…
[
"airvisual",
"airvisual_pro",
"alarm_control_panel",
"alarm_control_panel.manual",
"alarm_control_panel.mqtt",
"alarm_control_panel.overkiz",
"alarm_control_panel.zha",
"alert",
"analytics",
"androidtv_remote",
"api",
"apple_tv",
"application_credentials",
"assist_pipeline",
"auth",
"automation",
"binary_sensor",
"binary_sensor.browser_mod",
"binary_sensor.cloud",
"binary_sensor.esphome",
"binary_sensor.group",
"binary_sensor.hassio",
"binary_sensor.homekit_controller",
"binary_sensor.hue",
"binary_sensor.knmi",
"binary_sensor.meteoalarm",
"binary_sensor.mobile_app",
"binary_sensor.mqtt",
"binary_sensor.openuv",
"binary_sensor.overkiz",
"binary_sensor.plugwise",
"binary_sensor.rest",
"binary_sensor.shelly",
"binary_sensor.sonos",
"binary_sensor.stookalert",
"binary_sensor.sun2",
"binary_sensor.synology_dsm",
"binary_sensor.template",
"binary_sensor.threshold",
"binary_sensor.unifiprotect",
"binary_sensor.upnp",
"binary_sensor.version",
"binary_sensor.workday",
"binary_sensor.xbox",
"binary_sensor.zha",
"binary_sensor.zwave_js",
"blueprint",
"bluetooth",
"bluetooth_adapters",
"browser_mod",
"buienradar",
"button",
"button.esphome",
"button.homekit_controller",
"button.mqtt",
"button.overkiz",
"button.shelly",
"button.synology_dsm",
"button.unifiprotect",
"button.zha",
"button.zwave_js",
"camera",
"camera.browser_mod",
"camera.buienradar",
"camera.generic",
"camera.mqtt",
"camera.synology_dsm",
"camera.unifiprotect",
"cast",
"climate",
"climate.mill",
"climate.mqtt",
"climate.overkiz",
"climate.plugwise",
"climate.zha",
"cloud",
"co2signal",
"config",
"conversation",
"counter",
"cover",
"cover.group",
"cover.homekit_controller",
"cover.mqtt",
"cover.overkiz",
"cover.shelly",
"cover.template",
"cover.tradfri",
"cover.zha",
"cpuspeed",
"custom_icons",
"device_automation",
"device_tracker",
"device_tracker.mobile_app",
"device_tracker.mqtt",
"device_tracker.ping",
"device_tracker.unifi",
"device_tracker.zha",
"dhcp",
"diagnostics",
"dlna_dmr",
"dlna_dms",
"easyenergy",
"energy",
"energyzero",
"entsoe",
"esphome",
"fan",
"fan.mqtt",
"fan.switch_as_x",
"fan.tradfri",
"fan.zha",
"feedreader",
"ffmpeg",
"file_upload",
"filesize",
"forecast_solar",
"frontend",
"gdacs",
"generic",
"geo_location",
"geo_location.gdacs",
"geo_location.usgs_earthquakes_feed",
"github",
"google_assistant",
"google_assistant_sdk",
"govee_ble",
"group",
"hacs",
"hardware",
"hassio",
"here_travel_time",
"history",
"homeassistant",
"homeassistant_alerts",
"homeassistant_hardware",
"homeassistant_sky_connect",
"homekit_controller",
"http",
"hue",
"humidifier",
"humidifier.mqtt",
"image_upload",
"inkbird",
"input_boolean",
"input_button",
"input_datetime",
"input_number",
"input_select",
"input_text",
"integration",
"ipp",
"knmi",
"light",
"light.browser_mod",
"light.esphome",
"light.group",
"light.homekit_controller",
"light.hue",
"light.mqtt",
"light.overkiz",
"light.philips_js",
"light.shelly",
"light.switch_as_x",
"light.tradfri",
"light.unifiprotect",
"light.zha",
"local_ip",
"lock",
"lock.mqtt",
"lock.overkiz",
"lock.unifiprotect",
"lock.zha",
"logbook",
"logger",
"lovelace",
"luftdaten",
"media_player",
"media_player.apple_tv",
"media_player.browser_mod",
"media_player.cast",
"media_player.dlna_dmr",
"media_player.esphome",
"media_player.group",
"media_player.mpd",
"media_player.panasonic_viera",
"media_player.philips_js",
"media_player.samsungtv",
"media_player.sonos",
"media_player.spotify",
"media_player.unifiprotect",
"media_player.xbox",
"media_source",
"mill",
"min_max",
"mobile_app",
"moon",
"mqtt",
"my",
"network",
"notify",
"notify.file",
"notify.google_assistant_sdk",
"notify.group",
"notify.mobile_app",
"notify.tts",
"number",
"number.mqtt",
"number.overkiz",
"number.plugwise",
"number.sonos",
"number.unifiprotect",
"number.zha",
"number.zwave_js",
"onboarding",
"openuv",
"openweathermap",
"overkiz",
"panasonic_viera",
"panel_custom",
"persistent_notification",
"person",
"philips_js",
"ping",
"plex",
"plugwise",
"powercalc",
"profiler",
"proximity",
"recorder",
"remote",
"remote.apple_tv",
"remote.panasonic_viera",
"remote.philips_js",
"remote.xbox",
"repairs",
"rest",
"rest_command",
"samsungtv",
"scene",
"scene.homeassistant",
"scene.hue",
"scene.mqtt",
"scene.overkiz",
"script",
"search",
"season",
"select",
"select.esphome",
"select.mqtt",
"select.overkiz",
"select.plugwise",
"select.template",
"select.unifiprotect",
"select.zha",
"select.zwave_js",
"sensor",
"sensor.afvalwijzer",
"sensor.airvisual",
"sensor.browser_mod",
"sensor.buienradar",
"sensor.co2signal",
"sensor.command_line",
"sensor.cpuspeed",
"sensor.easyenergy",
"sensor.energy",
"sensor.energyzero",
"sensor.entsoe",
"sensor.esphome",
"sensor.file",
"sensor.filesize",
"sensor.forecast_solar",
"sensor.gdacs",
"sensor.github",
"sensor.govee_ble",
"sensor.group",
"sensor.hacs",
"sensor.hassio",
"sensor.here_travel_time",
"sensor.homekit_controller",
"sensor.hue",
"sensor.inkbird",
"sensor.integration",
"sensor.ipp",
"sensor.knmi",
"sensor.local_ip",
"sensor.luftdaten",
"sensor.mill",
"sensor.min_max",
"sensor.mobile_app",
"sensor.mold_indicator",
"sensor.moon",
"sensor.mqtt",
"sensor.nederlandse_spoorwegen",
"sensor.nmbs",
"sensor.openuv",
"sensor.openweathermap",
"sensor.overkiz",
"sensor.plugwise",
"sensor.powercalc",
"sensor.rest",
"sensor.season",
"sensor.shelly",
"sensor.solaredge",
"sensor.sonos",
"sensor.speedtestdotnet",
"sensor.spook",
"sensor.statistics",
"sensor.stookwijzer",
"sensor.sun",
"sensor.sun2",
"sensor.synology_dsm",
"sensor.systemmonitor",
"sensor.template",
"sensor.thermobeacon",
"sensor.time_date",
"sensor.tomorrowio",
"sensor.tradfri",
"sensor.unifi",
"sensor.unifiprotect",
"sensor.upnp",
"sensor.uptime",
"sensor.utility_meter",
"sensor.version",
"sensor.watchman",
"sensor.xbox",
"sensor.zha",
"sensor.zodiac",
"sensor.zwave_js",
"shell_command",
"shelly",
"siren",
"siren.mqtt",
"siren.overkiz",
"siren.zha",
"siren.zwave_js",
"solaredge",
"sonos",
"speedtestdotnet",
"spook",
"spotify",
"ssdp",
"stookalert",
"stookwijzer",
"stream",
"stt",
"sun",
"switch",
"switch.group",
"switch.hue",
"switch.mqtt",
"switch.overkiz",
"switch.philips_js",
"switch.plugwise",
"switch.shelly",
"switch.sonos",
"switch.spook",
"switch.synology_dsm",
"switch.template",
"switch.tradfri",
"switch.unifi",
"switch.unifiprotect",
"switch.wake_on_lan",
"switch.zha",
"switch.zwave_js",
"switch_as_x",
"synology_dsm",
"system_health",
"system_log",
"tag",
"template",
"text",
"text.mqtt",
"text.unifiprotect",
"thermobeacon",
"threshold",
"timer",
"tomorrowio",
"trace",
"tradfri",
"tts",
"tts.cloud",
"tts.google_translate",
"unifi",
"unifiprotect",
"update",
"update.esphome",
"update.hacs",
"update.hassio",
"update.mqtt",
"update.shelly",
"update.synology_dsm",
"update.unifi",
"update.zwave_js",
"upnp",
"uptime",
"usb",
"utility_meter",
"vacuum",
"vacuum.mqtt",
"version",
"wake_on_lan",
"watchman",
"water_heater",
"water_heater.overkiz",
"weather",
"weather.buienradar",
"weather.knmi",
"weather.openweathermap",
"weather.tomorrowio",
"webhook",
"websocket_api",
"workday",
"xbox",
"zeroconf",
"zha",
"zodiac",
"zone",
"zwave_js"
]
Ok. so back to the original item, the update is as follows
{%- for d, es in states | groupby('domain') %}
{% if loop.first %}{{loop.length}} Domains:
{%endif%}{{ d }}: {{ es|count }}
{%- endfor %}
which produces
{%- for v in items %}
{%- if '.' not in v %}
{%- set cnt = items | map('regex_match', '^(%s\.)'%v) | reject('false') | list | count %}
{{ v }}{{ ': ' ~ cnt if cnt else '' }}
{%- else %}
{{ v.split('.')[-1] }}
{%- endif %}
{%- endfor %}
that is really awesome!
took me some fiddling with the markdown and added the main sensor to get the ‘abridged’ number in there too (loop.count is returning the count for all integration under the components) and ended up with this for now:
- type: entities
title: Components and integrations
card_mod:
class: class-header-margin
entities:
- type: custom:hui-element
card_type: markdown
card_mod:
style: |
ha-card.type-markdown {
box-shadow: none;
margin: -8px -16px -16px -16px;
overflow-y: scroll;
height: 450px;
}
content: >
{%- set main = states('sensor.ha_main_components') %}
{%- set items = state_attr('sensor.ha_main_config','components')|sort %}
{%- for v in items %}
{% if loop.first %} ### Components: *{{main}}* - Integrations: *{{loop.length}}* {% endif %}
{%- if '.' not in v %}
{%- set cnt = items|map('regex_match','^(%s\.)'%v)|reject('false')|list|count %}
**{{v}}** {{- ': (' ~ cnt ~')' if cnt else '' }}
{% else %}
> *{{v.split('.')[-1] }}*
{% endif %}
{%- endfor %}
which shows like:
still trying t find a way to get the integration sub list to not use an extra line between each, but that is not an easy feat… not a biggy either
Have yet tried it as a separate template sensor, and see what gives on the more info. mostly those are not formatted nicely, and markdown gives us more control
thanks again!