Addressing individuals in a multi-switch input

(Convinced ST will never be unbroken…) #1

Ok, I don’t think I have seen this done so I am wondering if it is even possible.

Got an input like:

section("Use the following lights...") {
		input "switch1", "capability.switch", multiple: true, required: true

Let’s say the user now selects six switches. How can I refer to these six lights individually?

Or is a separate input required for each switch to be able to refer to them individually?

(George Sudarkoff) #2

If I understand the question correctly:

for (switch in switch1) {

(Convinced ST will never be unbroken…) #3

Thanks. But that would loop through them all and turn them on. I want to turn some on and others off.

(Edward Pope) #4

I am new here. So not sure that I am helping or hindering. I would think that if you have a user selecting multiple items that the code would process each selection individually/ Wouldn’t it just cache the following selected commands until all items in the cache are satisfied?.

(George Sudarkoff) #5

Inside the loop, “switch” is an individual switch in the collection. You can examine its label if you wanna know which one it is and decide what to do with it. But you can probably do easier or more efficient whatever you’re trying to do. So, what exactly are you trying to do?

(Convinced ST will never be unbroken…) #6

Identify the switches the user has input in the preferences, and manipulate their state based on randomized selection and scheduling.

(Michael) #7

You will need a for loop since you don’t know which switches were chosen. In that for loop you can have If statements to determine the switch and decide what to do. Here is an example:

for (a in switch1) {
   //log.debug "switch: $a.label"
   if (a.label == "Kitchen Switch") {
   if (a.label == "Living Room Switch") {

(George Sudarkoff) #8

In my original loop, instead of “on”, pick either on or off randomly. Voila. Unless I still don’t understand you. :slight_smile:

(Convinced ST will never be unbroken…) #9

I need to select a switch at random; here is what I came up with…

def myScheduleHandler(evt) {
     def theSwitches = settings.randomSwitches
     def theRandomSwitch = theSwitches[new Random().nextInt(randomSwitches.size())]
     log.debug theRandomSwitch

Now that I have theRandomSwitch, I can use a ‘for’ loop to refer to it.


(George Sudarkoff) #10

I’m not sure what you need the ‘for’ for. You already selected one switch. Just turn it on or off:


(Convinced ST will never be unbroken…) #11

A simple case of missing the 'for’est for the trees. (c;

(Chrisb) #12

I’m going to throw this example code here just to attach it to this thread. It really has nothing to do with what you’re doing @scottinpollock other than it’s a demonstration of another way to interact with multiple devices attached to one variable. I figure is someones searching the forums for help they’ll find your thread and the more examples the easier it will be for them to get the help they need.

1   doors.each { doorOpen ->			
2       	if (doorOpen.currentContact == "open") {
3               log.debug "$doorOpen.displayName"	
4        	def toReplace = doorOpen.displayName
5    		def replaced = toReplace.replaceAll(' ', '%20')
6    		log.debug replaced					
8               phrase = phrase.replaceAll('%20And%20', '%20')	
10    		if (phrase == "") {			
11                     	phrase = "The%20" + replaced 	
12   		} else {						
13             	        phrase = phrase + ",%20And%20The%20" + replaced	
14    		}
15       }

Here’s the line by line documentation:

1. cycles through all contact sensor devices selected
2. if the current selected device is open, then:
3. echo to the simulator the device's name
4. make variable 'toReplace' = the devices name.
5. make variable 'replaced' = 'toReplace' with all the spaces changed to %20
6. echo to the simulator the new name.
8. Remove any previously added "ands" to make it sound natural.
10.  If Phrase is empty (ie, this is the first name to be added)...
11. ...then add "The%20" plus the device name.
12.  If Phrase isn't empty...
13. ...then add ",%20And%20The%20".
14. Closes the IF statement.
15. Closes the doors.each cycle

(Edward Pope) #13

Thank you that was helpful in seeing the basics of the Groovy language as implemented with SmartThings