just to be able to see the unfiltered entities for the ['none', 'unavailable', 'unknown']
states, functional to be able to fine tune the filtering, I re-added the original small section.
also, I de-tabbed the attribute building, because I didn’t want these to be none and build under all circumstances, if only to be able to display that nicely in the Lovelace Frontend.
Also, I added some customization (…) on the main All sensor, to have an immediate place if all is ok. (we can’t customize entities in Ha Customize, that are created in Python scripts)
Below, I post the script and the Lovelace card. The external group and script to call this python script are all you need in HA config to get this going.
Hope this is of use for the community, have fun.
Python script
##########################################################################################
# Python script to replace formely used template sensor, which causes havoc in HA 115
# https://community.home-assistant.io/t/develop-and-rebuild-template-sensor-to-python-script/228699
# thanks to @VDRainer for providing most of the code here, and persevering my thoughts and
# questions
# 23 sep 2020 @mariusthvdb
##########################################################################################
# excludes based on certain conditions
if hass.states.get('binary_sensor.outside_daylight_sensor').state == 'on':
exclude_conditional_entities = ['light.parking_light']
else:
exclude_conditional_entities = []
exclude_entities = ['group.battery_sensors','group.github_repos_in_use',
'group.github_repos_not_in_use','group.indoor_temp_sensors',
'group.iungo_switches_actueel','group.philips_temp_sensors',
'sensor.theboss',
'sensor.openweathermap_forecast_precipitation',
'sensor.openweathermap_forecast_temperature_low']
excludes = exclude_conditional_entities + exclude_entities
exclude_groups = ['group.entity_blacklist'] # configured in HA group outside this python script
exclude_domains = ['media_player','automation','geo_location']
# some variables
count_unfiltered = 0
count_all = 0
count_none = 0
count_unavailable = 0
count_unknown = 0
count_sensors = 0
list_unfiltered = []
list_all = []
list_none = []
list_unavailable = []
list_unknown = []
list_sensors = []
attributes = {}
# create unfiltered list to fine-tune filtering
for entity_id in hass.states.entity_ids():
state = hass.states.get(entity_id).state
if state in ['none', 'unavailable', 'unknown']:
list_unfiltered.append(entity_id)
count_unfiltered = count_unfiltered + 1
# add groups entities to exclude_entities list
for group in exclude_groups:
for entity_id in hass.states.get(group).attributes['entity_id']:
excludes.append(entity_id)
# iterate over all entities
for entity_id in hass.states.entity_ids():
domain = entity_id.split('.')[0]
if entity_id not in excludes and domain not in exclude_domains:
state = hass.states.get(entity_id).state
if state in ['none','unavailable','unknown']:
count_all = count_all + 1
list_all.append(entity_id)
if state == 'none':
count_none = count_none + 1
list_none.append(entity_id)
if state == 'unavailable':
count_unavailable = count_unavailable + 1
list_unavailable.append(entity_id)
if state == 'unknown':
count_unknown = count_unknown + 1
list_unknown.append(entity_id)
if domain == 'sensor':
count_sensors = count_sensors + 1
list_sensors.append(entity_id)
# build the attributes
if count_all > 0:
icon = 'mdi:thumb-down'
icon_color = 'red'
attributes['all_count'] = count_all
attributes['all_entities'] = ', '.join(sorted(list_all))
else:
icon = 'mdi:thumb-up'
icon_color = 'green'
attributes['all_count'] = count_all
attributes['all_entities'] = 'All entities OK!'
if count_none > 0:
attributes['none_count'] = count_none
attributes['none_entities'] = ', '.join(sorted(list_none))
else:
attributes['none_count'] = count_none
attributes['none_entities'] = 'No None entities'
if count_unavailable > 0:
attributes['unavailable_count'] = count_unavailable
attributes['unavailable_entities'] = ', '.join(sorted(list_unavailable))
else:
attributes['unavailable_count'] = count_unavailable
attributes['unavailable_entities'] = 'No Unavailable entities'
if count_unknown > 0:
attributes['unknown_count'] = count_unknown
attributes['unknown_entities'] = ', '.join(sorted(list_unknown))
else:
attributes['unknown_count'] = count_unknown
attributes['unknown_entities'] = 'No Unknown entities'
if count_sensors > 0:
attributes['sensors_count'] = count_sensors
attributes['sensors_entities'] = ', '.join(sorted(list_sensors))
else:
attributes['sensors_count'] = count_sensors
attributes['sensors_entities'] = 'All sensors Ok'
if count_unfiltered > 0:
attributes['unfiltered_count'] = count_unfiltered
attributes['unfiltered_entities'] = ', '.join(sorted(list_unfiltered))
else:
attributes['unfiltered_count'] = count_unfiltered
attributes['unfiltered_entities'] = 'All and Unfiltered Ok'
attributes['friendly_name'] = 'UUN Ordered'
attributes['icon'] = icon
attributes['icon_color'] = icon_color
# set the sensor
hass.states.set('sensor.uun_ordered', count_all, attributes)
#How it all started
# count = 0
# attributes = {}
#
# for entity_id in hass.states.entity_ids():
# state = hass.states.get(entity_id).state
# if state in ['unknown']:
# attributes[entity_id] = state
# count = count + 1
#
# attributes['friendly_name'] = 'Unavailable Entities'
# attributes['icon'] = 'mdi:message-alert-outline'
#
# hass.states.set('sensor.unavailable_entities', count, attributes)
Lovelace card:
type: entities
entities:
- type: custom:hui-element
card_type: entities
title: Unavailable Unknown None
show_header_toggle: false
style: |
ha-card {
box-shadow: none;
margin: 0px -16px 0px -16px;
}
.card-header {color: var(--primary-color);font-weight: bold;}
.:
"#states > div:nth-child(n+1) > fold-entity-row":
$:
"#head > entity-row-maker > hui-section-row":
$: |
div.divider {height: 0px;}
div.label {font-weight: bold;}
entities:
- sensor.uun_ordered
- entity: script.update_entities_uun
secondary_info: last-triggered
action_name: Update
- type: custom:fold-entity-row
head:
type: section
label: Unknown entities
entities:
- type: custom:hui-markdown-card
style: |
ha-card {
box-shadow: none;
margin: -16px -16px -16px -26px;
}
content: |
Count: {{state_attr('sensor.uun_ordered','unknown_count')}}
{{state_attr('sensor.uun_ordered','unknown_entities')}}
- type: custom:fold-entity-row
head:
type: section
label: Unknown sensors
entities:
- type: custom:hui-markdown-card
style: |
ha-card {
box-shadow: none;
margin: -16px -16px -16px -26px;
}
content: |
Count: {{state_attr('sensor.uun_ordered','sensors_count')}}
{{state_attr('sensor.uun_ordered','sensors_entities')}}
- type: custom:fold-entity-row
head:
type: section
label: Unavailable
entities:
- type: custom:hui-markdown-card
style: |
ha-card {
box-shadow: none;
margin: -16px -16px -16px -26px;
}
content: |
Count: {{state_attr('sensor.uun_ordered','unavailable_count')}}
{{state_attr('sensor.uun_ordered','unavailable_entities')}}
- type: custom:fold-entity-row
head:
type: section
label: None
entities:
- type: custom:hui-markdown-card
style: |
ha-card {
box-shadow: none;
margin: -16px -16px -16px -26px;
}
content: |
Count: {{state_attr('sensor.uun_ordered','none_count')}}
{{state_attr('sensor.uun_ordered','none_entities')}}
- type: custom:fold-entity-row
head:
type: section
label: Full list filtered
entities:
- type: custom:hui-markdown-card
style: |
ha-card {
box-shadow: none;
margin: -16px -16px -16px -26px;
}
content: |
Count: {{state_attr('sensor.uun_ordered','all_count')}}
{{state_attr('sensor.uun_ordered','all_entities')}}
- type: custom:fold-entity-row
head:
type: section
label: Full list unfiltered
entities:
- type: custom:hui-markdown-card
style: |
ha-card {
box-shadow: none;
margin: -16px -16px -16px -26px;
}
content: |
Count: {{state_attr('sensor.uun_ordered','unfiltered_count')}}
{{state_attr('sensor.uun_ordered','unfiltered_entities')}}