Smartapp thinks state = 1 (on) after installation? (Dimmable Nightlight)


(Tuffcalc) #1

I’ve made some slight revisions to @chrisb 's nightlight app. When I run it in the IDE simulator, after its first installation, it always seems to think that the lights are on (state = 1). Once I switch the lights on and off it works correctly.

Does anyone know why this would be?


/**
 *  Hallway light at night.
 *
 *  Author: chrisb
 *  Borrowing pieces of code by: SmartThings & CoryS from "Lights Follow Me Dimmable"
 *
 *  Turn a dimmer light on at a low level if it isnt on already for a few minutes 
 *  when motion is detected during a given time period.
 *
 *  Example use would to be turn on a hall light at a low percentage at time when 
 *  someone gets up to use the bathroom at night.
 */


// Automatically generated. Make future change here.
definition(
    name: "Nightlight (Dimmable)",
    namespace: "",
    author: "seateabee@gmail.com",
    description: "Turn a dimmer light on at a low level if it isnt on already for a few minutes when motion is detected.",
    category: "My Apps",
    iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
    iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience%402x.png",
    oauth: true
)

preferences {
	section("Turn on when there's movement..."){
		input "motion1", "capability.motionSensor", title: "Where?", required: true, multiple: true
	}
	section("And off when there's been no movement for..."){
		input "minutes1", "number", title: "Minutes?", required: true
	}
	section("Turn on this dimmer switch...") {
    	input "MultilevelSwitch", "capability.switchLevel", title: "Which?", required: true, multiple: true
    }
    section("How Bright?"){
    	input "number", "number", title: "Percentage, 0-99", required: true
   
	}
}



def installed()
{
	subscribe(motion1, "motion", motionHandler)
	subscribe(MultilevelSwitch, "switch.on", switchOn)
    subscribe(MultilevelSwitch, "switch.off", switchOff)
}

def updated()
{
	unsubscribe()
	subscribe(motion1, "motion", motionHandler)
    subscribe(MultilevelSwitch, "switch.on", switchOn)
    subscribe(MultilevelSwitch, "switch.off", switchOff)
}

def motionHandler(evt) {
	log.debug "$evt.name: $evt.value"
    if (evt.value == "active") {  									// We're seeing motion, so go to next test...
    	log.debug "Seeing motion..."
       	     	if (state.switch == 0) {										// Passed last test, light is off.  Let's go dimming!
				log.debug "Light is off, let's go dimming!"            
        		unschedule(reset)													//Turn off any scheduled resets.
                settings.MultilevelSwitch.setLevel(number)
				def timeDelay = minutes1 * 60										//runIn uses seconds. Multiply by 60 to convert to seconds.
            	runIn (timeDelay, reset)
			}																
            else {
            	log.debug "Light is on... I'm not touching it."
            }																// Completes the "if the light is on" test.    
        }  																// Completes the "if it's the right time" test.  
																// Completes the "there's motion" test.
}

def switchOn(evt) {
	log.debug "Switch on, state = 1"
    state.switch = 1				// When switch is on we're not going to run the dimmer function.  1 = on
}    

def switchOff(evt) {
	log.debug "Switch off, state = 0"
	state.switch = 0				// When switch is off, we'll run the dimmer function.  0 = off
}

def reset() {						// The dimming event will leave the light level very low, so reset to full level
	settings.MultilevelSwitch.setLevel(99)
    settings.MultilevelSwitch.setLevel(0)
    //MultilevelSwitch.off()
    }


(Mike Maxwell) #2

yea, state.switch = null when the app is first installed, since it doesn’t = 0, it’s being evaluated by the else as on…
the if should be something like (state.switch ?: 0 == 0), though I’ve not tested that specific form
another one you could use is (state.switch == 0 || state.switch == null)


(Tuffcalc) #3

@Mike_Maxwell - thank you! (state.switch == 0 || state.switch == null) works great!