Well, given the success of my previous script, it was inevitable that someone would ask for something else.
This is something I haven’t been using HA long enough to really need myself, but it will let you find items that are defined as part of a group, but don’t actually exist in the system any longer
I decided to use a weblink to display the name of the entity, since I couldn’t find just a basic text component, and a weblink is something I understand reasonably well, and assume it doesn’t add any functionality into the system. Once again, the group and elements it adds should be temporary, and not last across reboots.
So @Mariusthvdb and @arsaboo are at least partially responsible for this one
I’m not really happy with this right now, I can’t prevent the link from opening a new tab, and wish I didn’t have to use a link, and could just do plain text.
Anyway, here you go, and I will get the first comment so that I can keep the code up to date. Let me know what you think, and what enhancements this needs.
def process_group_entities(group, grouped_entities, hass, logger, process_group_entities):
# logger.warn("processing group {}, currently {} grouped items".format(group.entity_id, len(grouped_entities)))
for e in group.attributes["entity_id"]:
domain = e.split(".")[0]
if domain == "group":
process_group_entities(hass.states.get(e), grouped_entities, hass, logger, process_group_entities)
else:
grouped_entities.add(e)
# logger.warn("finishing group {}, currently {} grouped items".format(group.entity_id, len(grouped_entities)))
def scan_for_dead_entities(hass, logger, data, process_group_entities):
target_group=data.get("target_group","deaditems")
show_as_view = data.get("show_as_view", True)
real_entities = set()
grouped_entities = set()
for s in hass.states.all():
domain = s.entity_id.split(".")[0]
if domain != "group":
real_entities.add(s.entity_id)
else:
if (("view" not in s.attributes) or
( s.attributes["view"] == False)):
real_entities.add(s.entity_id)
process_group_entities(s, grouped_entities, hass, logger, process_group_entities)
entity_ids=[]
counter=0
for e in (grouped_entities - real_entities):
name = "weblink.deaditem{}".format(counter)
hass.states.set(name, "javascript:return false", {"friendly_name":e})
entity_ids.append(name)
counter = counter +1
service_data = {'object_id': target_group, 'name': 'Nonexisting Items',
'view': show_as_view, 'icon': 'mdi:cube-unfolded',
'control': 'hidden', 'entities': entity_ids,
'visible': True}
hass.services.call('group', 'set', service_data, False)
scan_for_dead_entities(hass, logger, data, process_group_entities)