What is wrong with my simple code below?
I have a null pointer exception on the first line I try to subscribe for the sunrise event (subscribe(location, “sunrise”, sunriseHandler))
Here it is:
definition(
name: “Curtains Control”,
namespace: “iker.pryszo”,
author: “Iker Pryszo”,
description: “Allows a virtual switch to control the curtains and provides an option to open and close automatically the curtains for sunrise and sunset”,
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 the switch that controls the curtains”) {
input “mySwitch”, “capability.switch”, multiple: false
}
section(“Open and Close automatically for sunrise and sunset ?”) {
input “autoMode”, “bool”, title: “Auto Mode”, defaultValue: true
}
}
def installed() {
log.debug "Installed with settings: ${settings}"
initialize()
}
def updated() {
log.debug "Updated with settings: ${settings}"
unsubscribe()
initialize()
}
def initialize() {
subscribe(mySwitch, “switch”, switchHandler)
subscribe(location, “sunrise”, sunriseHandler)
subscribe(location, “sunset”, SunsetHandler)
}
def switchHandler(evt) {
log.trace "The event is: $evt.value: $evt"
if (evt.value == ‘on’) {
log.trace “Open Curtains”
//TO DO
} else {
if (evt.value == ‘off’) {
log.trace “Close Curtains”
//TO DO
}
}
}
def sunriseHandler(evt) {
log.info "Executing sunrise handler"
if (autoMode) {
log.info "Open Curtain Automatically"
mySwitch.on()
}
}
def sunsetHandler(evt) {
log.info "Executing sunset handler"
if (autoMode) {
log.info "Close Curtain"
mySwitch.off()
}
}
Thanks a lot!