Code critique request for slightly smarter night light

I’m still fairly new to the SmartThings ecosystem and the Groovy language. My goal with this was to take the Smart Night Light SmartApp and make it work for my needs. The original version by ST is a good concept, but has some very annoying behaviors. I used the Smart Night Light Dimmable version as a starting point.

Would anyone be willing to take a look at the code and critique? I’m happy with what I have from a conceptual standpoint, less sure about my actual code execution.

Big changes:
Using runIn() to schedule the shut off
Checking to see if someone changed the dim level, and skipping the scheduled shut off if so.
Checking to see if the light was already on when motion was detected. If it was, don’t reset the dim level.
Assuming this will only be run during the middle of the night mode.
Dimmers only.
No sunrise/sunset.

If anyone else would find this SmartApp useful, please use it, modify it, etc.

Thanks!

2 Likes

Looks good, @JoeC. A few thoughts:

  1. I think I understand what you are doing with state.lastStatus. It might be slightly cleaner if you just add a unschedule(inactiveHandler) when there is any motion.active event.

  2. Looks like the only way to recover from someone manually adjusting the dimmer is for someone to manually turning off the dimmer. Maybe you could turn off all the lights at sunrise or some arbitrary time during the morning in case any are left on. Maybe you’re already doing this with another app.

  3. It’s great that you want people to use/modify it, etc. You are asserting a copyright claim in the comments section. You might want to eliminate this especially if you started with some existing code. The IDE automatically puts that line in the comments so you may not even realize you are asserting a copyright claim.

FWIW.

1 Like

Thanks so much for your response. I was unaware of the unschedule method, that does seem better for this situation. I’ll remove the copyright, thank you for pointing that out.

Making progress with the Groovy language and the ST implementation of it! It’s always helpful to get another set of eyes on your work.

1 Like

This is exactly what a Slightly Smarter Night Light needs. It worked as expected…

For my own use, I just added an on/off switch (since its annoying when the light turns on when I’m sleeping on the couch - and slight motion is detected)

section(“What do I use for the on/off switch?”) {
input “powerSwitch”, “capability.switch”
}

}

def installed() {
log.debug “Installed with settings: ${settings}”

initialize()

}

def updated() {
log.debug “Updated with settings: ${settings}”

unsubscribe()
initialize()

}

def initialize() {
subscribe(motionSensors, “motion”, motionHandler)
}

def motionHandler(evt) {
log.debug “$evt.name: $evt.value”

    	if (powerSwitch.currentSwitch == "on") {
			log.debug "turning on lights due to motion"
            
			
            	if (evt.value == "active") {
					for (dimmer in dimmers) {
     			   if (dimmer.currentSwitch == "off") {
       			     dimmer.setLevel(level)
					state.lastStatus = "active"
					log.debug "motion detected, light is off, turning light on"
                    sendNotificationEvent( "Nightlight turned on" )
                    
     		   } else {
					log.debug "motion detected, light is on, doing nothing"
      		  }
    }
}
else {
    state.lastStatus = "inactive"
    runIn(60 * delayMinutes ?: 0, inactiveHandler)
}
            
        }
        else {
        log.debug "Control switch is off, not turning on light."
        }

}