How to use range option?

I wrote a smartapp that I want to restrict valid entries from 0.5 through 9.9 in the preferences section for user input. I am using the optional setting of range to do this. I am trying to only allow the user to input an updated value between 0.5 though 9.9

If I use this range: “1…99” it allows me to use any of those valid inputs between 0.5 though 9.9 however also the ones outside of 9.9 like 10 and up to 99. This is what one would expect.

How am I supposed to write the line of code with range to limit the user between 0.5 and 9.9?

If I change it to range: “1…9” I can only use whole integers or else it causes an error and the preference will not be updated.

So the question is did I find a bug or how do I define the range correctly to restrict valid entries to be only within say 0.5 through 9.9

Here is the line of code below that isn’t working right.

section(“Enter the desired differential temp between fan speeds (default=1.0)…”){
input “fanDiffTemp”, “decimal”, title: “Fan Differential Temp”, range: “1…99”, required: false

This is not all the way to 9.9, but it will get you started as it contains .5-9.5


Further adjustments can get you to 10ths or more.


thanks for the reply! Interesting, so I am not sure how I implement this? I put your fix in the parameters and it gives errors

section("Enter the desired differential temp between fan speeds (default=1.0)..."){
	input "fanDiffTemp", "decimal", title: "Fan Differential Temp", range: "(1..19)*.div(2)" required: false

maybe use an enum, you don’t have that many choices, particularly if you do them in .5 degree increments

1 Like

That is a great idea since the range: “x…y” is buggy. Thanks Mike !!

I was hoping to make my ceiling fan thermostat smartapp more user friendly for those outside the US that use °C instead of °F and the default of 1°C for those using Centigrade is going to be more than most would want since every degree Centigrade converts to almost 2 degrees Fahrenheit

you could use something like this to auto generate the enum options based on the users location temp units.
This is from an SA of mine, it may work in a DTH, don’t know.

def zoneFixedOptions(){
	def opts = []
    def start
    if (!state.tempScale) state.tempScale = location.temperatureScale
	if (state.tempScale == "F"){
    	start = 60
        start.step 81, 1, {
    } else {
    	start = 15
        start.step 27, 1, {
	return opts

then: in the input, options : zoneFixedOptions()

That is awesome! I definitely want to make the enum change and will look at your code to make it smarter :relaxed: BTW, which smartapp of yours uses this?

that snippet is from keenectZone…

Well it sure looks great when I set it up as enum but it totally breaks the smartapp. I am wondering if the enum returns only text versus number values which is causing the problem when I try to use it’s value?

Is there a trick to converting enum values from text to number value that anyone knows of?

yea, enums return text, depending on the numeric data type
use myInput.toInteger() or myInput.toBigDecimal()

1 Like

Got it! I will give it a try. :slight_smile: