In a custom app I have a statement as follows:
If (device.currentswitch == "on") { do something}.
If the device has multiple true, how does it handle this? Does it interate thru all the devices and they all have to be on? Or is it an OR type interation?
But what does it do by default? Or what I entered just doesn't do anything?
I only had one device selected, even tho multiple was true. And it didn't work.
If I did a log.debug "${device.currentswitch}" It displayed the value just fine.
It's no biggy, just wondering what the default execution of the IF is in this case. If there is any.
But it didn't work that way. The IF statement never evaluated true even tho the one device was on. When I changed it to device.currentswitch[0] == "on" then it worked.
This is the input statement:
input "garageOnLights", "capability.switch", required: true, multiple: true, title: "Garage Lights To Turn On"
In this case I don't need multiple so it isn't any big deal. It just caused me some debugging trying to figure out why it didn't work. Took a while to realize I had multiple true. And thus got me wondering what the default execution was.
Not sure if it matters but all documentation has camelCase for “currentSwitch”.
This is some code I have in one of my apps that has multiple switches, offSwitches being the input:
def onSwitchList = offSwitches.findAll { it?.latestValue("switch") == "on" }
def anyOn = onSwitchList.size() > 0 ? true : false
if (anyOn) {
//do something
}
With multiple: true, device.currentSwitch is a list, not a string like 'on' or 'off' -- even with just a single device selected. So it's [thisDevice] or [firstDevice, secondDevice], etc. .currentSwitch will do the same thing, and give ["on"] or ["on", "off"], etc.
If you want to know if any of them are on, you could do device.currentSwitch.contains("on")
If you want to know that all of them are on you could do !device.currentSwitch.contains("off")
And then you can iterate through the list with device.each{if(it.currentSwitch == "on") ....}
It should always be currentAttributeName, or currentValue("attributeName").
The test for ["on"] would only work if you knew that only a single device was selected, which means why would you use multiple:true in the first place?