How to I act on a preference when it is multiple true?


(Ron) #1

I have a need for a preference in my smart app which will allow multiple selection.

I can’t figure out from the documentation how I am supposed to act on this once it is set.

If I create
input(name: “modes”, multiple: true, type: “enum”, title: “Make Mode buttons”, options: [“Cool”,“Dry”,“Heat”]), required: true

How do I code a check to see if “Cool” was selected ?

Will modes be a string with comma’s, an array, a list of some type ?


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

What is the result if you log.debug “$settings”?

Or log.debug modes

Etc. For a start anyway. You should be able to tell if the result is a Groovy List (essentially an array, of course), since printas will show square brackets, right??

If not shown as a List, then it’s likely a string, but you can check this by running string methods against it (instead of Collection / List operations…).


(Ron) #3

I wasn’t sure if debugging output would work because a list might format out as a,b,c

Anyway I found code in the new git repo ( much nicer to search…grep multiple:true ) LOVE IT !
it uses
if (modes.contains(“Heat”)) …

So I am trying that.


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

I’d still like to see the output of settings.

Of course log.debug converts everything to a string but the brackets and colons are indicative of Lists and Maps.

The implied “toString” of Java / Groovy objects is a great tool if the method uses consistent delimiters.

I presume “contains” is a List method though. I have too poor memory to know offhand if it does anything with a String.


(Ron) #5

It prints as follows
initialize: modes=[Dry, Cool],basename=Bedroom

unfortunately contains is a string method meaning the string contains substring.
So even with this code we don’t know if it is a string or a list
the debug output looks like a list

I have the code working with contains.


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

It’s a List… [ ]

contains is also a method of the Class “List”.


(Tim Slagle) #7

So you can do something like this.

modes.contains("cool")

This will return a boolean for whether or not the input “modes” contains “cool”

So in an if statement you could do this:

if(modes.contains("cool")){
    //do something of modes contains cool
}
else if (!modes.contains("cool")){
    //if modes does not contain cool
    //do something
}

Also, this may help some :slight_smile: