Wildcard in Rules for Devices

Is there a way to use wildcards to automatically pick up all devices with a specific label?

Example: I want to power off all switches when I leave for work. If all the switches begin with "Switch", I would like to use something like "Switch*" instead of having to call out each switch in the code.

I wouldn't have to remember which Rules need updating when adding new devices.

Create a group with all your switches in it, then control that group with your rules. Then you only have to update the group when you want to add/remove a switch.

edit: You can even create a hierarchy of groups. e.g. a "Kitchen Lights" group, and a "Living Room Lights" group could both be placed into an "All lights" group. Much more flexible than wildcards based on names, and there's only one place where new devices need to be updated.

4 Likes

Interesting issue... the first thought is to create a group but that doesn't answer the need - managing the group becomes manual labor! Programatically, an app could be written that generates a dynamic list that's run by schedule... here's a example found via daGoogle:

def getOnSwitches() {
    // find all the switches that are on
    def on = switches.findAll {
    	it.currentValue("switch") == "on"
    }
    log.debug "there are ${on.size()} switches on"
    
    // In SmartThings, you can get device properties on entire
    // collections just as you would a single device. The result 
    // will be a list
    def onNames = on.displayName
    
    // pretty print by joining the list returned with a comma
    log.debug "These switches are on: ${onNames.join(', ')}"
}
1 Like

As opposed to the manual labor of naming things so they will match a wildcard?

1 Like

Looking up that code brought me to this thread Groovy question - getting the names of all devices in a certain state - #2 by Jim - Writing SmartApps - SmartThings Community, and Jim's next post had a link to the original docs.smartthings.com, which is now gone. But I saved an offline copy (somewhere).

Internet Archive has the old Smarthings docs:
https://web.archive.org/web/20210506231643/http://docs.smartthings.com/en/latest/getting-started/groovy-basics.html

1 Like

To directly answer this question: no. For the most part, apps can only use devices that you have specifically selected. (Some built-in apps do have a special "use all devices of capability X" feature, but this still wouldn't really help you. This is also not available to user apps, though it's not clear to me if you're using true custom code or just a custom Rule Machine rule.)

I'd second the suggestion from @mikes above to use a group for this purpose. Then, all you have to remember to do is update the group--and as a bonus, the rule is a bit easier to write since you're only using a single device in the rule.

I'm not sure how the suggestion above to (programatically) find devices in a specific state helps, either; the crux of the issue is getting the devices themselves, which again can really only be done by manual selection in the UI.

I use a lot of groups here quite often nested within other groups. 5 levels deep in one instance -

  • House: All Lights
    • Lounge: Lights
      • Lounge: Floor Lights
        • Lounge: Floor Lamps
          • Lounge: Left floor lamp (2 bulbs)
          • Lounge: Right floor lamp (2 bulbs)
        • Lounge: Cube Lamps
        • Lounge: Anglepoise
      • Lounge: Ceiling
        • Lounge: Ceiling East
        • Lounge: Ceiling West
    • Bedroom: All Lights
      • Bedroom: Nanoleaf
      • Bedroom: All bulbs
        • Bedroom: Ceiling Lights (3 bulbs)
        • Bedroom: Floor
        • Bedroom: Reading
        • Bedroom: Bedside (2 lamps left and right)

Most of the lower level groups are included in other groups or scenes named by functionality e.g. Bedroom: Morning Lights and Bedroom: Nightlights. I find that this helps a lot when creating rules.

@rob, interesting setup. I never thought about nesting groups. I presume they work well? It doesn't fix what I want, but it is something new for me to try for my automation.

They do work well, but you may have to ensure you turn off the Enable on/off optimization? option at least at the top level group. If on/off optimization is left on then it can sometimes miss lights in the subgroups depending on their settings.

I think it would help achieve what you want for e.g. turning off all switches - you could easily have one group that would contain all the devices you want to switch off when you leave. That's exactly what I do here with my House: All lights group which includes Annexe: Ceiling, Bedroom: All lights, Kitchen Light Strip, Lounge: All lights, Study: All lights, Commons: Hallway ceiling and Garage: All lights.

If I add a new device I'll add it to the House: All lights group or one of the groups contained within it and it will be turned off when I leave.