Z-wave device with auto off parameter

Hey all,

Wonder if some talented person could assist me here.
I have a plug which supports an "Auto off" parameter, and the code is stopping the switch updating on/off due to this. Its a driver ported from ST and pretty much all is working bar this.

This is my error.
dev:26252019-05-28 04:29:38.740 pm errorjava.lang.NullPointerException: Cannot invoke method and() on null object on line 492 (parse)

and the code for that section. Currently my param is set to 60 seconds (just to test).

/**
 *  zwaveEvent( COMMAND_CLASS_SWITCH_BINARY (0x25) : SWITCH_BINARY_REPORT (0x03) )
 *
 *  The Binary Switch Report command  is used to advertise the status of a device with On/Off or Enable/Disable
 *  capability.
 *
 *  Action: Raise switch event and log an info message if state has changed.
 *    Schedule autoOff() if an autoOffTime is configured.
 *
 *  cmd attributes:
 *    Short   value   0xFF for on, 0x00 for off
 *
 *  Example: SwitchBinaryReport(value: 255)
 **/
def zwaveEvent(hubitat.zwave.commands.switchbinaryv1.SwitchBinaryReport cmd) {
	logger("zwaveEvent(): Switch Binary Report received: ${cmd}","trace")

	def result = []

	def switchValue = (cmd.value ? "on" : "off")
	def switchEvent = createEvent(name: "switch", value: switchValue)
	if (switchEvent.isStateChange) logger("Switch turned ${switchValue}.","info")
	result << switchEvent

	if ( switchEvent.isStateChange & (switchValue == "on") & (state.autoOffTime > 0) ) {
		logger("Scheduling Auto-off in ${state.autoOffTime} seconds.","info")
		runIn(state.autoOffTime,autoOff)
	}

	return result
}

This being line 492
if ( switchEvent.isStateChange & (switchValue == "on") & (state.autoOffTime > 0) ) {

TIA!

Conditions should use && for and.

If state.autoOffTime is null then the > 0 might also cause an error, but I can't remember. Using !state.autoOffTime instead of comparing it to 0 would be safer because that will return false if it's null or 0.

1 Like

Really, one should always use && by default, and only use & if you are explicitly doing bit comparisons and/or are exploiting the "feature" in C# that can check non "1" value true booleans... (I think exploiting that non "1" true boolean in C# is really a bad practice, but I've seen it done many times).

1 Like

Thanks guys, its one ported from ST, thanks very much for the input.
Much appreciated.

I believe 0 is to turn the compare off, so only set if above 0. But even after the && its not setting the runIn. I'll keep trying :+1: