PSA: Turn on/off all lights in Home Assistant 0.104+ (group.all_* changes)

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… :rofl:

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.

1 Like

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.

Thanks for looking out @petro . I will make the change.

I happened to read this just before this thread. You’ll find some concrete and robust examples of what @petro suggested there.

1 Like

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 :sweat_smile:

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?

Is there some way to group lights that are on AND in a specific area?

EDIT:
I found this that somehow looks at the area even though I’ve seen many stating that area cant’t be pulled from entities. Can this be used/tweaked to allow dynamic groups with lights ON and in specific area?

{%- set search_state = 'on' %}
{%- set search_area = 'Vardagsrum' %}
{%- set ns = namespace(lights=[]) %}
{%- for light in states.light | selectattr('state','eq', search_state) | rejectattr('attributes.entity_id', '!=', undefined) if area_name(light.entity_id) == search_area %}
  {%- set ns.lights = ns.lights + [ light.entity_id ] %}
{%- endfor %}
{{ ns.lights }}

EDIT: 2 nvm I did not tink of that a group can not be updated without a service call/restart.

I don’t have an answer because I have never experimented with Light Groups that way.