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

Nice list @petro, thanks for compiling.

adding my bit: just discovered we can also expand the domain, as an alternative:

count

{% set lights = expand(states.light)
|selectattr('state','eq','off')|list|count %}
{{lights}}

list:

{% set lights = expand(states.light)
|selectattr('state','eq','off')|map(attribute='name')|list %}
{{lights}}
1 Like

This works for me:

List:

{{ states.light | selectattr(‘state’,‘eq’,‘on’) | map(attribute=‘entity_id’) | join(’,’) }}

What’s the purpose of expanding the domain?

The result of the following two templates is the same so the expansion seems like an unnecessary step.

yes

noticed that myself
 first thought the one was listing as list, and the expand as comma separated, but they are not. they are identical. How odd.

(aware of |list versus |join(',') )

sorry for the confusion.

The expand function is useful if you want to combine domains (or groups or entities). For example, this counts how many light and switch entities are off.

4 Likes

ha, that’s very nice indeed, never saw that before in 1 template. in the cookbook!
thanks Taras.

same goes for the '=='

I’ve always used ‘eq’:

{{expand(states.light, states.switch)
| selectattr('state','eq','on')|map(attribute='name')
  |join(',\n')}}

learned a lot today.

3 Likes

ah, that is nice. Thanks @123, i’ll add it to the top post.

Nice! Out of curiously. The big batteries group topic uses an automation to periodically (or at startup) populate a group. Wouldn’t that be an options for those who REALLY want those groups back?

1 Like

The removal of group.all_devices is a bit of a disaster for me.

I use this very regularly as a trigger and condition for very many automations as it very reliably lets me tell if there is anyone home or not, or if someone has literally just gotten home (using nmap tracking of devices on Wifi)

There literally couldn’t be anything simpler than a condition state check of home or not_home, and a trigger resulting in a state change from home to not_home, but it looks like I can’t do this anymore.

I’m going to see if I can build a sensor that creates a dynamic group to replicate the old behaviour. Manually creating a group with a list of devices won’t work in case people change devices (and end up with a new entity name). If I add additional devices in future I would have to remember to add them to the group. The group needs to update the second a new device appears home.

Is there any point in campaigning to keep just group.all_devices for this use case??

1 Like

Scroll up and read my last post
Or here is another solution.

3 Likes

You can create manually same group with your device_tracker too
That I did

Thank you for your reply, the 2nd link looks ideal as it is a native script to create the dynamic group, so it can be called at startup and at any time should another device be added to home assistant. I’ll do some testing with this.

This makes me wonder if my entire approach to home / not home is incorrect, perhaps I should be using zones?

Is there an easy way to make a script that just creates the group.all_* automatically again?
I don’t want to screw around with a lot of automations of mine that use device trackers, which use to use the group.all_devices status. I manually created a group called group.all_devices and added the entities manually.
Does anyone know a script or automation that may automatically write a group file?

3 posts up!

1 Like

@VDRainer. Ooops. Missed that one scrolling through. Thanks

Thanks for this!

I need group.all_devices as I use appdaemon and have a listen state on group.all_devices to trigger an action when “everyone’s left” home or “Someone’s arrived”.

This should hopefully do the trick.

1 Like

Why would you need that for appdaemon? Theres about 900 ways to get all devices in appdaemon. Simply self.get_state('device_tracker') would get them all.

Hell, if you want to listen for all device_trackers


self.listen_state(self.callback, 'device_tracker')
1 Like

Wrong.

That will work for determining if “Someone arrived” home. ie. whenever any device tracker changes state from not_home to home

What about for determining if “Everyone left” home. ie all device trackers that are currently home changes to not_home. Your suggestion will trigger as soon as the first device tracker changes state.

I’m sure there are a million ways to do what I want in python, but group.all_* was the simplest.

If you have a simple alternative, let me know.

Just make it the first if statement in your callback.

if all([ self.get_state(entity_id) == 'not_home' for entity_id, value in self.get_state('device_tracker') ]):

EDIT: Making it more readable.

Also an FYI to simulate the group being on


if any([ self.get_state(entity_id) == 'home' for entity_id, value in self.get_state('device_tracker') ]):

any() and all() are built in methods to python that are very powerful.

any() checks a list for 1 item that could be resolved to what python thinks is True.
all() checks a list for all items that could be resolved to what python thinks is True.

1 Like

I don’t have an issue with get_state.
get_state gets the state of an entity it doesn’t listen to it to create a trigger.

How would you make this work with a listen_state?