Using "mode" input as an if condition always returning 'null'


(DeWayne) #1

Trying to create my first SmartApp so forgive me in advance. I’m trying to allow the user to select a mode, possible modes’ when installing the app. Then I set a schedule to watch for mode changes, and if the current mode is one of the selected modes, then continue. But it seems like the input mode is always null.

Trying to troubleshoot my idea, I simplified my code to a very basic scenario:

preferences {
    section("At this time every day") {
                  input "time", "time", title: "Time of Day"
        }
    section("Change to this mode") {
        input "newMode", "mode", title: "Mode?"
    }
}
...
def changeMode() {
    	log.debug "changeMode, location.mode = $location.mode, newMode = $newMode, location.modes = $location.modes"
	if (location.currentMode == newMode) {
    	log.debug "currentMode equals newMode"
		}
    if(location.modes.find {it == newMode}) {
    	log.debug "Newmode is a valid mode"
    }
    if (!(location.currentMode == newMode))
    { log.debug "current Mode not equal to New Mode"
    }
}

When I run this, and my user selected ‘Mode’ is the same as my current mode (Current mode is ‘HomeDaytime’, user selected mode is ‘HomeDaytime’), the first two ‘IF’ statements are returning false and the 3rd is executing. I would expect the 1st two to execute, but it seems that the input mode is always null.

Log:

7e735c0b-9289-4f0c-bb70-49d341294c0c 11:28:12 AM: debug current Mode not equal to New Mode
7e735c0b-9289-4f0c-bb70-49d341294c0c 11:28:12 AM: debug changeMode, location.mode = HomeDaytime, newMode = null, location.modes = [HomeEvening, AwayNight, HomeDaytime, SleepingDay, AwayDaytime, Sleeping]
7e735c0b-9289-4f0c-bb70-49d341294c0c 11:27:58 AM: debug changeMode, location.mode = HomeDaytime, newMode = null, location.modes = [HomeEvening, AwayNight, HomeDaytime, SleepingDay, AwayDaytime, Sleeping]
7e735c0b-9289-4f0c-bb70-49d341294c0c 11:27:58 AM: debug Installed with settings: [time:2016-09-03T11:30:00.000-0500, newMode:null]

(Alex) #2

Try to change this:
location.modes.find {it == newMode}

to this:
location.modes.find {it.id == newMode.id}


(DeWayne) #3

That returns an error in the log:
> java.lang.NullPointerException: Cannot get property 'id' on null object @ line 42


(Alex) #4

Looking at the stack trace you provided above, looks like newMode is null, is the preference selected? You may want to make it a required field.


(DeWayne) #5

I did choose a mode when I Installed, but I went ahead and tested it again after making the value of newMode required and it still shows up as null in the log.


(James Murphy) #6

The same thing is happening to me, at least in the simulator. If the last input is a mode, then it always comes across as null.

What happens if you switch the order of the two preferences, like this?

preferences {
    section("Change to this mode") {
       input "newMode", "mode", title: "Mode?"
    }
    section("At this time every day") {
         input "time", "time", title: "Time of Day"
    }
}

(Jim Anderson) #7

I’d advise to avoid the simulator and just install on the mobile app. The simulator just isn’t consistent with the real (mobile) experience, and can cause frustration if trying to use it for anything non-trivial.


(Brian Warner) #8

I am also seeing this issue where I want to set a mode, and the simulator reports I’ve selected a null mode upon installation.

Are there any workarounds (besides installing on the mobile app)?