Can't seem to get this script to fire on switch handler: on

hey guys,

I am new to the custom code with Groovy/Smartthings. I am using this in the simulator and when I select a virtual switch, set it to on, I don’t even get the log.debug “switch turned on” to show up.

For some reason it just seems the onHandler is not being triggered by the subscribed switch.

Perhaps it’s a syntax error or maybe I’m just a noob.

Thanks guys!!

definition(
name: “Testing Dim During Day/Night”,
namespace: “Martin”,
author: “Martin”,
description: “TBA”,
category: “Convenience”,
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(“Select Switch to monitor”){
input “Switch”, “capability.switchLevel”
}
section(“Set Brightness-day”) {
input “brightness-day”, “number”, title: “Light Level”
}
section(“Set Brightness-night”) {
input “brightness-night”, “number”, title: “Light Level”
}
}

def initialize() {
subscribe(Switch, “switch.on”, onHandler)

}

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

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

def onHandler(evt) {

log.debug “switch turned on”

if(getSunriseAndSunset().sunrise.time < now() && getSunriseAndSunset().sunset.time > now()){
log.debug “Daytime”
Switch.setLevel(brightness-day)
}
else {
log.debug “Nighttime”
Switch.setLevel(brightness-night)
}
log.debug “Received on from ${Switch}”

}

Try this:

definition(
name: "Testing Dim During Day/Night",
namespace: "Martin",
author: "Martin",
description: "TBA",
category: "Convenience",
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("Select Switch to monitor"){
input "switch1", "capability.switch"
}
section("Set Brightness-day") {
input "brightness-day", "number", title: "Light Level"
}
section("Set Brightness-night") {
input "brightness-night", "number", title: "Light Level"
}
}

def initialize() {
subscribe(switch1, "switch.on", onHandler)

}

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

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

def onHandler(evt) {

log.debug "switch turned on"

if(getSunriseAndSunset().sunrise.time < now() && getSunriseAndSunset().sunset.time > now()){
log.debug "Daytime"
Switch.setLevel(brightness-day)
}
else {
log.debug "Nighttime"
Switch.setLevel(brightness-night)
}
log.debug "Received on from ${Switch}"

}
2 Likes

Okay that worked, but I am not getting any info for the getSunriseAndSunset (new problem).

I also can’t set dimmer levels since we set it to switch capability only.

Any idea what else is wrong?

Bump if allowed.

Make sure the collection your setting level on is correct. Also for sunrise sunset does the IDE have these times listed?

It does work, I’ve had it work on other scripts written by others.

But in this one, nothing happens when the virtual switch is turned on.