you don’t want to do this. state is a method and you’re overwriting it. This could break in the future versions. Use something different like ‘desired_state’ IGNORE
I do have some long running reminders, white noise scripts and automations with a big delay that sometimes run for hours and it meant I couldn’t restart HA whenever I wanted. So I created this sensor:
{{ expand(states.script, states.automation)
| rejectattr('entity_id','==','script.ha_restart')
| selectattr('attributes.current','>', 0)
| selectattr('state','==','on')
| list | count }}
It returns how many automations and scripts are running currently, except my own script for restart purposes.
Now I’ll run a script named “ha_restart” and when there are no other scripts or automations running, it’ll restart HA by itself. I basically queued my restart process, which means I won’t have to disrupt our house automations whenever I want to test something or update a component in HACS.
Thanks to this thread I managed to solve a big headache and I decided to share in case it helps someone else, thanks a lot for the template tips!!
Wouldn’t it make more sense to queue the automations and restart them after the HA restart? Would help, if an un-planned restart of HA is happening?
But before you ask, I have absolutely no idea how to do this, it’s just an idea…
That would be great but it’s not really easy to get automations and scripts back to where they were.
For now I’m happy with getting my custom component updated without stopping what’s already running.
Have your automation create a datetime when the duration should end. Then have an automation that fires when the datetime is reached. Restarts in the middle won’t matter.
That’s another option! Maybe can even be a single automation with two distinct paths for the original trigger and the datetime trigger. And could even work for some automations in which I just have an “wait for trigger” but would require changing a lot of stuff. I’ll start considering that.
For now I just need to restart, get my custom component updated and I don’t need that happening ASAP.
I also would need to change stuff like this blueprint I use which only has a single trigger.
I happened to read this just before this thread. You’ll find some concrete and robust examples of what @petro suggested there.
I made the recommended change to my template but this got me a error message TemplateSyntaxError: Encountered unknown tag 'desired_state'
. Was there something else I should be changing?
yeah, update the template to use desired_state instead of state
{% set domain = 'light' %} {% desired_state = 'on' %} {% set count = states[domain] | selectattr('state','eq', state) | map(attribute='entity_id') | list | count %} {{ count }}
, but this got me a error message TemplateSyntaxError: Encountered unknown tag 'desired_state'
.
you didn’t update the selectattr to use desired_state, it’s still using state
Was that just a copy/paste template that you found?
EDIT: Probably the original templates
Just change it back to state, was having a brainfart.
okay
Petro, why not consolidate this thread with the Count lights thread that you are also responding to. I think this one has a lot more information to share and would maybe save some time on responding about the same thing in two different places. just a thought.
They are similar but not, one is for counting one is for making groups.
I propose this enhanced version:
domain = data.get('domain', '')
group = data.get('group', '')
icon = data.get('icon', '')
if not isinstance(domain, str) or not isinstance(group, str) or not domain or not group:
logger.warning("bad domain or group! Not executing.")
else:
if domain == "switch":
name = "All switches"
elif domain == "binary_sensor":
name = "All binary sensors"
else:
name = "All "+domain+"s"
if not icon:
service_data = {"object_id": group, "entities": hass.states.entity_ids(domain), "name": name}
else:
service_data = {"object_id": group, "entities": hass.states.entity_ids(domain), "name": name, "icon": icon}
hass.services.call("group", "set", service_data, False)
In automation:
- service: python_script.create_all_group
data:
domain: fan
group: all_fans
icon: mdi:fan
Thanks @petro for the pyhton script - it helped me a lot. I extended it - with my NO knowledge in python - to optionally provide prefixes and suffixes to just group the entities whose entity_ids which match it.
If wanted to do it with re and filter but i didnt find a way to import these in HA python scripts.
domain = data.get('domain', '')
group = data.get('group', '')
prefixes_or_suffixes = data.get('prefixes_or_suffixes', '')
def filter_by_pattern(entity_id):
return bool(re.search(entity_id_pattern, entity_id, flags=re.IGNORECASE))
if not isinstance(domain, str) or not isinstance(group, str) or not domain or not group:
logger.warning("bad domain or group! Not executing.")
else:
if not isinstance(prefixes_or_suffixes, str) or not prefixes_or_suffixes:
entities = hass.states.entity_ids(domain)
else:
prefixes_or_suffixes = prefixes_or_suffixes.split(",")
entities = []
for entity_id in hass.states.entity_ids(domain):
for prefix_or_suffix in prefixes_or_suffixes:
if entity_id.find(prefix_or_suffix) != -1:
if entity_id not in entities:
entities.append(entity_id)
service_data = {"object_id": group, "entities": entities}
hass.services.call("group", "set", service_data, False)
Use it like this:
service: python_script.create_all_group
data:
domain: light
group: deko_licht
prefixes_or_suffixes: light.deko_licht_,light.demo_licht_
I found your response while looking for a way to create a light group in an automation. The group.set service creates a regular group, and although that works when used with scene.apply in the automation, it does not work when I try to get any of the color / brightness attributes.
I have the first part working, where I dynamically create a group of lights that can be used in a scene.apply call to set color, etc., but that same group does not work when trying to access the color properties of the group in another automation. If there was a light_group.set service, that would solve my problem.
Alternatively, do you know if it is possible to use a template to create a light_group using a regular group of all light entities?