How to set input devices as optional?


#1

Hi Guys,

For below code

preferences {
section(“Select Devices”) {
// TODO: put inputs here
input “door”, “capability.contactSensor”, title: "Select a Door sensor"
input “livingMotion”, “capability.motionSensor”, title: "Select Living Room Sensor"
input “bedroomMotion”, “capability.motionSensor”, title: "Select Bedroom Sensor"
input “bathroomMotion”, “capability.motionSensor”, title: "Select Bathroom Sensor"
input “kitchenMotion”, “capability.motionSensor”, title: “Select Kitchen Sensor”
}
}

I defined 5 sensors for this smartApp, but currently i only have 4 sensors with me, can I put 1 of the sensor input as optional without remove my current code?
Currently SmartThings App need user fill all the input, else I will get “Please fill out all required fields” message on my SmartThings App


#2

Got the answer myself :smile:

just simply add required: false

But I got unexpected error on my SmartThings APP :frowning:


(Bruce) #3

If you don’t input anything, then that variable will be null. So in your code, you have to protect against trying to do something with a null value. One way to do that is this very simple test, for example:

if(kitchenMotion) { ....  code that does something with kitchenMotion .... }

If kitchenMotion is null, the test fails, the code doesn’t execute (which is what you want), and you don’t step on the null value.


#4

Thanks Bruce :smile: