Any/all state of a group

If I were to creat a motion sensor input that allowed for multiple sensors is there an easy way to test the state of the group? Or would I need to iterate through the list?

I could use motion groups but I would like to keep all the controls in a single app.

———

On a somewhat related note, is there any order to the items in a group like this? I created a group of lights and when I looped through them they weren’t ordered by name or device I’d.

I don't think Hubitat provides a method to check all at the same time, but you don't need to iterate over the entire collection; Groovy provides some methods that can probably help you find what you're looking for with a Closure without needing to do a Java-esque loop or whatnot.

For example, if you have:

input name: "theSensors", type: "capability.motionSensor", multiple: true //, ...

Then you get a List of device objects (actually, it's a Hubitat-specific DeviceWrapperList, but it appears to subclass List, so you can treat them similarly) for theSensors. You can then do the aforementioned Groovy methods on these, like:

 // returns true if any active:
theSensors.any { it.currentValue("motion") == "active" }

// returns true if all inactive
theSensors.every { it.currentValue("motion") == "inactive" } 

You can find more on Groovy List methods in the Groovy docs, List (Groovy JDK enhancements), or with a web search (be aware that Hubitat uses Groovy 2.4, but I'm not aware of any breaking changes in 2.5 or 3.x).

Regarding ordering, you should be able to use the sort() method (documented above, but part of the Iterable interface and not specifically List). I'm not sure what it will do by default if you don't use the variant that provides a closure to specify the sort (my guess is display name?), but aside from display purposes, I've never found a reason to do this. Perhaps you have a specific use case.

Thanks for the reply. I didn’t know what to search for. The list functions look like exactly what I need.

For the order I do have a specific use in mind. I have nine can lights along the front soffit of my house. I have a custom app that controls these and I have been refactoring it since my C-4’s radio died and I migrated to a C-7.

The first priority is that they act as security lights. To do this I have three groups setup: all, nightlight, and nightlight other. At sunset the nightlight group comes on at 50%. Then when motion is detected the “all” group comes on at 100%. When motion goes inactive the nightlight group goes back to 50 and the nightlight other group turns off.

This works great. Now to complicate things the lights also follow the holidays. To do this I set up odd and even groups. Right now they are in Thanksgiving mode and instead of the the nightlight groups the lights come on in yellow and orange in those two groups.

We are up to five groups but that is ok. Now for the Fourth of July I need three colors. In the old code I setup groups 3.1, 3.2, 3.3 and this worked. But I also played with loading the lights individually. Now I have 8 groups and 9 individual lights. I’m sure you can see where this is going.

So I created a multi selector and was surprised at the order the lights came on when I looped through the lights. Unfortunately I’ve had issues sending single light requests. It looks like the mesh gets flooded but that’s an issue for another day.

So the bottom line is that I was looking to do my five groups and one single multi selector. I’m still experimenting with the app and kicking around various ideas.

This could come into play in the living room too where I have 30 track lights setup. They are MiLight GU-10 lamps and setting up a group of all of them gets ugly. Things like on/off work ok but setting the hue on the group ends horribly.

Anyway, a ton more info than you probably wanted. I’ll look into the list functions. Thanks again.