Selecting dimmers and switches at the same time


(Calvin Robertson) #1

I’m trying to modify a smart app to turn on lights and set dimmers to a specified level. Similar to how you can select set dimmers to x in a hello home action. Does anyone know how to achieve this without creating separate selections for switches and dimmers in the preferences section?


(Bruce) #2

You can look at mySwitch.name or whatever.name to get its type, and then you can distinguish between “Switch” and “Dimmer Switch”.

I’m assuming you want to set the dimmers to x, but just turn on the switches.


(Duncan) #3

You can check device.hasCapability("Switch Level")


(Calvin Robertson) #4

Currently the app turns on the lights by listing the input variable “lights” and using the command on as:

lights.on()

When this executes, does it do this for each individual light selected? Is there a way to use a case statement to determine whether or not each individual light has the level capability on the fly or do I need to build a function to separate this beforehand? If I have to separate them beforehand, is there and easy way to separate the multiple selections inside the input?


(ActionTiles.com co-founder Terry @ActionTiles; GitHub: @cosmicpuppy) #5

lights is a List of Objects of Type Device.

So you can iterate over this list in the usual Groovy ways, including iterating a closure with an if clause expression device.hasCapability("Switch Level").

There should be plenty of examples of the syntax in others SmartApps; I don’t trust myself to post from memory untested, but might come back.


(Bruce) #6
lights.each {
    if(it.hasCapability("Switch Level") {blah blah blah for dimmer}
    else {blah blah for switch}
}

In each case, it represents the single switch or dimmer as you iterate. So it.on() or it.setLevel(myLevel).