IF Statement in App

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?

multiple true makes the input a list.

So you would typically iterate the list depending if you want or/any or and/all

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.

If you have multiple == true, but only a single device selected, it behaves the same as if multiple == true was not selected.

The difference comes when you have multiple==true and multiple devices are selected....

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.

I guess you need to show the input statement for device

In general I would not overload the variable device in an input statement, as it is predefined in device handlers to refer to this device instance.

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.

Again, no big deal, just curious.

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

Believe that the multiple: true always returns a list thus something like:

garageOnLights.each {
   if (it.currentSwitch == 'on) {

would be more appropriate.

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

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") ....}


So I could have done in my IF ==["on"] and probably had it work.

Along those lines will either currentswitch or currentSwitch work? Seems like it is different for different things.

I appreciate all the help in clearing it up.

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?

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.