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

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?

in AD3 (haven’t checked to see if the attributes are correct anymore for ad4)

self.listen_state(self.check_devices, 'device_tracker')
def check_devices(self, entity, attribute, old, new, kwargs):
    if all([ self.get_state(entity_id) == 'not_home' for entity_id, value in self.get_state('device_tracker') ]):
        # do your stuff here.
1 Like

The first code you’ve pasted, like I said originally, will trigger the first time any device’tracker changes state. Not all.

You 2nd code is not a listener. Unless I’m mistaken, in appdaemon get_state only checks the state of a device once when exucuted. It doesn’t monitor changes to state. That’s what listen_state is for.

Yes… but…

the if statement checks all the devices…

This is identical to the functionality of group.all_devices

What petro wrote should work. You listen to the state changes of all entities, the callback gets executed when an entity state changes. In the callback you then check with get_state, whether all device trackers are “not_home” if this is true it continues otherwise not

1 Like