Hi @finity, If you have the indentation levels right, it will not add the group unless there are two or more items in it (including the trigger for the script). However, there is currently nothing in the script to remove the group if it already exists.
If you have already dealt with everything in the view, and want to get rid of it, you might need to restart HA.
in order to get groups that aren’t included anywhere, try this (I’m not actually on my HA system right now, but this should work, or at least be pretty close). I can verify it when I am done work if necessary.
Remove group from the domains_to_ignore variable or parameter
Change the block which is currently this
for state in hass.states.all():
domain = state.entity_id.split(".")[0]
if (domain not in domains_to_ignore):
entity_ids.append(state.entity_id)
if (domain == "group") and (state.entity_id != target_group):
groups.append(state.entity_id)
to
for state in hass.states.all():
domain = state.entity_id.split(".")[0]
if (domain not in domains_to_ignore):
entity_ids.append(state.entity_id)
if (domain == "group"):
if (state.entity_id != target_group):
groups.append(state.entity_id)
if (("view" in state.entity_id.attributes) and (state.entity_id.attributes["view"] == false)):
entity_ids.append(state.entity_id)
I will take a look at the group.set service (I wasn’t aware of it until just now, and also see there’s a group.remove). One thing I have found frustrating is because I am doing everything inside a function, a lot of objects aren’t automatically available (which is why I pass hass and logger into the function)
Weirdly enough, when I run group.set with {"object_id": "catchall", "view":false, "visible":false}, instead of modifying group.catchall, it creates new group.catchall_2, group.catchall_3,…
I hope to do that by selecting the normal_mode again, or switching of my dev_mode switch. But here’s the dev tools:
and, unfortunately the group stays in view…
and, also unfortunately, I see a group.catchall_2… !
this is new, worked before.
maybe take the visibility and view out of the python.
the script itself does work, I do it with my dev tools, and they show and hide on command. It is a predefined group though, which I simply have not grouped anywhere, so it shows up in the far right of the tab-bar, when made visible and a view by the script.
We should probably try to do the same with this Ungrouped catch_all group.
group:
developer:
name: Developer
icon: mdi:developer-board
# view: true
entities:
- switch.mode_developer
- script.reload_frontend
- script.reload_python
- script.catchall
- script.restart_ha
- script.rc_reboot_iungo
- script.sc_reboot_iungo
- group.catchall # if made on the fly by python_script
group.developer is displayed as view in dev_mode (see previously posted switch, or input_select.mode).
it reveals the script for running the python script. (Ive removed the view: true in the python script)
running the script
script:
catchall:
alias: Catch all ungrouped
sequence:
- service: python_script.populate_catchall
creates the group.catchall, which is then displayed. Switching of dev-mode, takes it all out again
I added three new parameters
show_as_view (whether to create it as a view or a group, defaults to True)
show_if_empty (whether to show the group or not depending on the number of items, defaults to False)
min_items_to_show (this controls the show_if_empty. It defaults to showing the group if at least one uncaught item was found).
I’ve also modified the script so that it doesn’t exclude Groups by default, so it will find groups that aren’t included anywhere (I found a few I’d forgotten about).
It also ignores Hidden items (alerts were causing me problems while testing, not sure how I will deal with this in the long run).
def scan_for_new_entities(hass, logger, data):
ignore = data.get("domains_to_ignore","zone,automation,script,zwave")
domains_to_ignore=ignore.split(",")
target_group=data.get("target_group","group.catchall")
show_as_view = data.get("show_as_view", True)
show_if_empty = data.get("show_if_empty", False)
min_items_to_show = data.get("min_items_to_show", 1)
logger.info("ignoring {} domain(s)".format(len(domains_to_ignore)))
logger.info("Targetting group {}".format(target_group))
entity_ids=[]
groups=[]
for s in hass.states.all():
state=hass.states.get(s.entity_id)
domain = state.entity_id.split(".")[0]
if (domain not in domains_to_ignore):
if (domain != "group"):
if (("hidden" not in state.attributes) or
(state.attributes["hidden"] == False)):
entity_ids.append(state.entity_id)
else:
if (("view" not in state.attributes) or
(state.attributes["view"] == False)):
entity_ids.append(state.entity_id)
if (domain == "group") and (state.entity_id != target_group):
groups.append(state.entity_id)
logger.info("==== Entity count ====")
logger.info("{0} entities".format(len(entity_ids)))
logger.info("{0} groups".format(len(groups)))
high_order=0
for groupname in groups:
group = hass.states.get(groupname)
if int(group.attributes["order"]) > high_order:
high_order=int(group.attributes["order"])
for a in group.attributes["entity_id"]:
if a in entity_ids:
entity_ids.remove(a)
attrs={}
attrs["view"]=show_as_view
if (len(entity_ids)) > min_items_to_show or show_if_empty:
attrs["visible"]=True
else:
attrs["visible"]=False
attrs["friendly_name"]="Ungrouped Items"
attrs["icon"]= "mdi:magnify"
attrs["view"]=show_as_view
attrs["order"] = high_order
entity_ids.insert(0,"script.scan_for_new_devices")
attrs["entity_id"]=entity_ids
hass.states.set("group.catchall", "new", attrs)
scan_for_new_entities(hass, logger, data)