Having issue Understanding Variable Types

UPDATE I'm guessing this may be a scope issue as another of my questions was answered by my not fully understanding scope. I understand the concept but not the rules :slight_smile:

I have a variable "tempUnitsDegC" that gets set using an "input name" function. If I assign it a (what I think is numeric) a following "if" test does not work (i.e. is not processed) and I get the below error.
However when I change the Type to String by assigning the variable "0" or "1" all is fine.

So clearly I am missing some subtlety but I've not been able to find it.

Any insights would be helpful?

I have this line to define "tempUnitsDegC"

    input name: "tempUnitsDegC", type: "enum", title: "Temperature Units", options: [1:"°C",0:"°F"], defaultValue: 1, required: false

Then this "if" statement did not resolve to "true" in the below

if (tempUnitsDegC ==0 ){
                    SensorValue = SensorValue * 9 / 5 + 32
                    sendEvent(name: "temperature", value: SensorValue, unit:"F", isStateChange: true)
                }

image

I changed the numbers to strings and all worked.

    input name: "tempUnitsDegC", type: "enum", title: "Temperature Units", options: ["1" : " °C","0" : "°F"], defaultValue: "1", required: false

and

if (tempUnitsDegC =="0" ){

Works OK.

All inputs of type enum in apps (and drivers) store the chosen value as a String, regardless of what you might want the underlying type to be. If this is related to your other post, it might actually be some issue with automatic type conversion not working as expected rather than a possible scope issue (which was a total guess on my part and unlikely unless something else is acting on the value or there's some name "collision" that is resolving to a different object entirely).

So, what you can do is one of two things: compare to a String like you're doing in that snippet, or convert the value to a numeric type before doing comparisons/operations.

2 Likes

Download the Hubitat app