Preferences input display

I am trying to display certain settings when a value is greater than a specific amount

 preferences {
    section("INFO HERE")
        input("Address", "string", title:"IP Address", required:true, displayDuringSetup:true)
        input("Port", "string", title:"Port",  required:true, displayDuringSetup:true)
        input("numValue", "string", title:"How many things to display in settings?", required:true, displayDuringSetup:true)
        input("ID", "string", title:"ID", required:true, displayDuringSetup:true)
    if (numValue > "0"){
        input("Thing1", "string", title:"Thing 1 Name", displayDuringSetup:true)
        }
    if (numValue > "1"){
        input("Thing2", "string", title:"Thing 2 Name", displayDuringSetup:true)
        }

I only want Thing1 to display in settings if numValue is greater than or equal to 1.
I want Thing2 to display in settings if numValue is greater than or equal to 2.

I’ve tried >=, == and a multiple of other things but I cannot get it to display correctly. I can put numValue to = 1 and both Thing1 and Thing2 shows up!!! AHHHHHHH

Any ideas?

Is there a reason why you’re not using a number input type for numValue?.

If you use a string input then you’d need to cast it to a number like below, but you’d want to add some logic to verify that it it’s a valid number before doing that:

if (numValue.toInteger() > 1) {

If you change the input type to “number” then you can just use:

if (numValue > 1) {

You also need to add the following to the numValue input if you want that code to execute immediately after changing numValue.

submitOnChange: true

1 Like

Thank you for the suggestions!!!

I changed input(“numValue”,“number”. . . .

I tried if (numValue > 1){… and it still isn’t displaying correctly.

I have numValue entered as 2 and it’s not displaying in settings.

If you didn’t add the submitOnChange attribute then the only way you can determine if it worked is to save and reload that page.

If that’s how you were testing it then try numValue.toInteger(), but last time I checked (a few years ago with classic app) that wasn’t necessary with a number input type.

All that being said, all of my apps/handlers have a function like the one below so I can use (safeToInt(numValue) > 1) without having to worry about whether or not numValue is actually a number.

Integer safeToInt(val, Integer defaultVal=0) {
	if ("${val}"?.isInteger()) {
		return "${val}".toInteger()
	}
	else if ("${val}".isDouble()) {
		return "${val}".toDouble()?.round()
	}
	else {
		return  defaultVal
	}
}