Comparison not working as expected in driver

OK, I'm sure this is either me not understanding something, or doing something wrong - but as usual I don't see what... @mike.maxwell Does your driver programming guruness see what the issue is?

I have a preference in a driver, it can be 0 or 1. Nothing especially exotic, and that seems to work fine.

 input (
    name: "invertSwitch",
    title: "Switch Orientation",
    type: "enum",
    options: [
        "0" : "Normal",
        "1" : "Inverted",
    ],
    required: false
)

Then I check for the value later. When I have "Normal" selected, it should be zero.... From the logs, the config value is zero, but the truth comparison always fails.... WHY?

def config = cmd.scaledConfigurationValue
if (cmd.parameterNumber == 5) {
    	def value = config == 0 ? "Normal" : "Inverted"
		log.info "Config = " + config
		log.info "Value = " + value
		
    	result << createEvent([name:"SwitchOrientation", value: value, displayed:true, isStateChange:true])
    } 

From the log. See config=0, but then the truth statement config == 0 is false?? I expected value = Normal...

dev:852019-01-26 10:11:50.224 am infoValue = Inverted

dev:852019-01-26 10:11:50.222 am infoConfig = 0

Luckily the correct value is being read/written from/to the device parameter, so functionally there is no problem. But it is driving me batty that the events are all wrong.

In your situation, are you finding that ==1 is coming up true? Have you tried == "1" to do a text compare instead of a numeric compare?

Why not use a Boolean preference instead? You could have the preference be Inverted? and if it's off, it's always false. Then it also gets a toggle switch in the device edit page.

I thought about that too. No difference though. If I do config == "0" I still get the following. The value of config changes as expected as I change the preference, but the comparison doesn't work... weird.

Maybe time to reboot the hub... Edit: same result after reboot.

dev:852019-01-26 11:06:29.254 am infoValue = Inverted

dev:852019-01-26 11:06:29.253 am infoConfig = 0

dev:852019-01-26 11:06:19.467 am warndescription logging is: true

dev:852019-01-26 11:06:19.466 am warndebug logging is: false

dev:852019-01-26 11:06:19.465 am infoupdated...

dev:852019-01-26 11:06:07.060 am infoValue = Inverted

dev:852019-01-26 11:06:07.058 am infoConfig = 1

dev:852019-01-26 11:06:00.181 am warndescription logging is: true

dev:852019-01-26 11:06:00.179 am warndebug logging is: false

dev:852019-01-26 11:06:00.177 am infoupdated...

dev:852019-01-26 11:05:45.860 am infoValue = Inverted

dev:852019-01-26 11:05:45.859 am infoConfig = 0

Did you try with it set to 1 instead to see if it will match then? I've only ever used alpha responses for enum so I don't know what's goin on. Could try using A and B instead and see if that works. I know it's not a fix but if it gets your there....

Have you tried single quotes instead of double for the comparison?

Might be a dumb suggestion, I'm not a programmer, but this fixed a similar problem I had when trying to modify a node.js script.

You can also simplify it a bit.

Set your input to just have "options:["Normal","Inverted"]"

Then you don't need your comparison. You can just do a "def value = config" assuming your cmd.scaledConfigurationValue is set to whatever settings.invertSwitch is set to.

Try changing your code to

def value = config.toInteger() == 0 ? "Normal" : "Inverted"

I still think it would be easier with a bool preference.

Well, I needed an integer at some point anyway to write it back to the device parameter, so didn't want to make it a boolean only.

Single quote = didn't work.

But... This worked:

def value = config.toInteger() == 0 ? "Normal" : "Inverted"

I could have sworn I tried that, but obviously not!!! Actually, now that I think about it... I think when I tried it I might have forgotten to put the () on the toInteger....

Thanks @ogiewon!

1 Like