Problem with evaluation of currentValue()

I am pulling my hair out, trying to do a simple evaluation of a switch. I’ve seen code exactly like this, which I assume works. Mine doesn’t.

def val = checkSwitch.currentValue(‘switch’)
log.debug val
if (val == “off”) {
log.debug “switch is off”
}

The switch is off, but the if never evaluates to true. What am I missing?

Regards,
—Wayne.

follow this example:

def turnOffLights(){
	//Turn off lights everyone is away.
    def offLights = []
	hues.each {
        if (it.currentValue("switch") == "on") {
        	log.debug ("turning off : " + it.label)
        	it.off();
            offLights.add(it.label);
        }
	}
	log.debug "Turned off these lights: $offLights"
    return offLights;
}
1 Like

What does ‘val’ show in the debug log?

Ron,

That’s essentially the same as my example. You are getting the switch property and comparing it to “on”. In my case I am getting the switch property and comparing it to “off”. In my example the switch is off, yet the if statement does not evaluate to true.

Regards,
—Wayne.

Linda,

It shows [off] as the switch is off. I even tried evaluating “[off]” in case the brackets matter, but they don’t.

Regards,
—Wayne.

the space between the equals… just kidding… :wink:

Even this logs false.

def val = checkSwitch.currentValue(‘switch’)
log.debug val == ‘off’

Well, that’s…peculiar.

This is where the atrocious debugging tools in the IDE (i.e. no tools) really leave us in the lurch.

The only thing I can think of is to try something like this:

def val = "foobar"
val = checkSwitch…

and see what happens. It shouldn’t make any difference. The first statement might event get optimized out but it might show some different behavior that sheds some light.

1 Like

Ron’s example shows how to enumerate through a group of switches and return the value of each.
Your code would only work with one specific switch, so if you have multiple: true in your prefs section, probably not going to work as I believe you will get an array back.
Try this for your debug log.debug val.inspect()…

def val = switch2.currentValue('switch')
	log.debug val
	if (val == "off") {
		log.debug "switch is off"
	}

The above works here (aside from the fact that IDE and simulator are so slow they are unusable). Probably just another server side glitch in a long line of them.

Works for me too…

/**
 *  test
 *
 *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 *  in compliance with the License. You may obtain a copy of the License at:
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
 *  on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
 *  for the specific language governing permissions and limitations under the License.
 *
 */
definition(
    name: "test",
    namespace: "test",
    author: "Ron",
    description: "test",
    category: "",
    iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
    iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png",
    iconX3Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png")


preferences {
    section("Control these bulbs...") {
		input "hues", "capability.colorControl", title: "Which Hue Bulbs?", required:true, multiple:true
	}
}

def installed() {
    log.debug "Installed Home Status Monitor with settings: ${settings}";
    initialize();
}

def updated() {
    log.debug "Updated with settings: ${settings}";
    unsubscribe();
    initialize();
    
}

def initialize() {
    //Turn off lights everyone is away.
    def offLights = []
	hues.each {
        if (it.currentValue("switch") == "off") {
            offLights.add(it.label);
        }
	}
	log.debug "These lights are off: $offLights"
}
// TODO: implement event handlers

Response:

Logs

‎10‎:‎55‎:‎57‎ ‎AM: debug These lights are off: [Bedroom, Small Bedroom Lux 1, Family Room 3, Small Bedroom Lux 2, Living Room 2, Living Room 1, Family Room 1, Family Room 2, Living Room 3, Living Room LightStrips, Family Room LightStrips, Dining Room 1, Family Room LivingColors]

‎10‎:‎55‎:‎57‎ ‎AM: trace test is attempting to unsubscribe from all events

‎10‎:‎55‎:‎57‎ ‎AM: debug Updated with settings: [hues:[Bedroom, Small Bedroom Lux 1, Family Room 3, Small Bedroom Lux 2, Living Room 2, Living Room 1, Family Room 1, Family Room 2, Living Room 3, Living Room LightStrips, Family Room LightStrips, Dining Room 1, Family Room LivingColors], zip:8820]

Mike,

Spot on, I think that was it. In the preferences I had copied the switch and it was set to multiple : true.

Unfortunately I haven’t been able to confirm this as I for the last 20 minutes I am not getting any debug messages in either the IDE console or the live logging window. I’ve logged out and in, and restarted the browser so I suspect it is on the cloud side.

Thanks to all that responded, great community.

Wish we had a debugger as missteps like this would be diagnosed in a few seconds.

Regards,
—Wayne.

Console started working again. To close this off, the issue was I had defined the switch preference as multiple and the array that was returned behaved like an individual switch, except for evaluation which was failing. For my specific case I have a single switch I am checking to conditionally run some code so I have defined multiple : false now.

Thanks again.

Regards,
—Wayne

1 Like