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

Hey guys I’ve read through the whole thread and it’s been great seen what all one can do if the are able to wrap there minds around templates. Which I have yet to do, it seems like such a daunting task and once you think you understand example one you realized that was not the case in example two.

  • So what is a good way to learn the ins and outs of HA template? I read the HA guide and while some of it is helpful it falls short in demonstrating the basic steps.

  • I have a template that I poached from this thread that counts all my lights that are on { states.light|selectattr('state','eq','on')|list|count }} map(attribute='entity_id') | list | count %}, but it manages to count them twice by including items that are in Light Group. I came across a post from @123 that show how I could get a light count of only certain entities with a specific attribute
    {{ states.light | selectattr('attributes.entity_id', 'defined') | list | count }}. How can I combine the 2 templates so that I am able to get an accurate count of the light that are on/off/unavailable or whatever state I want to be made aware of without getting duplicate number counts.

Anyone able to help me solve this?

Not sure if this is exactly what you want but it starts with all lights, rejects Light Group entities, selects the remaining lights that are on and reports the count.

{{ states.light
  | rejectattr('attributes.entity_id', 'defined')
  | selectattr('state','eq','on')
  | list | count }}
1 Like

Wow that is exactly what I was looking for? Man thanks for the quick reply. I was able to even do this
{% set domain = 'light' %} {% set state = 'on' %} {% set count = states[domain] | selectattr('state','eq', state) | map(attribute='entity_id') | list | count %} There {{ 'are' if count > 1 else 'is' }} {{ count }} {{ 'lights' if count > 1 else 'light' }} on. Many Thanks

1 Like

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!!

1 Like

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.