I’ve just purchased a SmartThings Hub and a trying to write some fundamental programs. I have some programming experience but by no means am an expert. The app I am working on is simple; If a selected presence sensor arrive home after sunset, turn selected lights on for a period of time. I have the presence sensor portion of the code working fine but am having problems with sunrise and sunset. I’m using subscribe(location, “sunset”, sunsetHandler)
subscribe(location, “sunrise”, sunriseHandler) to trigger two methods that set a common variable “DarkOutSide” to true of false depending on what method is called. The problem I am having is that “DarkOutSide” is not being updated or passed back to" def presenceHandler(evt)". I’ve used “state” because I know I need to store that variable between events and still had problems. Can somebody tell me what I am doing wrong and how to correct this?
Thank…
//Defination provides metadata about the SmartApp
definition(
name: “Arrive After Sunset”,
namespace: “smartthings”,
author: “Bob Wagenhoffer”,
description: “Momentarily turn something on only if you arrive after sunset.”,
category: “Convenience”,
iconUrl: “https://s3.amazonaws.com/smartapp-icons/Meta/light_presence-outlet.png”,
iconX2Url: “https://s3.amazonaws.com/smartapp-icons/Meta/light_presence-outlet@2x.png”
)
// Preferences define what information our SmartApp needs from the user
preferences {
section(“Select Presence Sensors:”){
input “presence”, “capability.presenceSensor”, title: “Presence Sensors to Monitor:”, multiple: true
}
section("Switches to Activate:"){
input "switches", "capability.switch", title: "Switches to activate:", multiple: true
}
input "TimeDelay", "number", title: "Mins to wait before turning off:"
}
// When a user installs a SmartApp the defined installed() method will be called
def installed()
{
log.debug "Installed with settings: ${settings}"
subscribe(presence, “presence”, presenceHandler)
subscribe(location, “sunset”, sunsetHandler)
subscribe(location, “sunrise”, sunriseHandler)
initialize()
}
// When a user updates a SmartApp the defined installed() method will be called
def updated()
{
log.debug "Updated with settings: ${settings}"
unsubscribe()
initialize()
subscribe(people, “presence”, presenceHandler)
subscribe(location, “sunset”, sunsetHandler)
subscribe(location, “sunrise”, sunriseHandler)
}
// Initialize is called when a a SmartApp is installed or updated
def initialize() {
state.DarkOutSide = true
log.debug “Initialize has run, DarkOutSide is: $state.DarkOutSide”
}
def presenceHandler(evt)
{
//Is Presence Sensor Arriving or Departing??
def RoleCall = "$evt.value"
log.debug "Role Call: $RoleCall"
log.debug “Dark Out Side: $state.DarkOutSide”
//Turn Lights on, then off if Presence Sensor arrives after Sunset
if ((RoleCall == "present") && (state.DarkOutSide)) {
switches.on()
log.debug "Lights On"
runIn(TimeDelay*60, LightsOff)
}
}
// Turn off Lights
def LightsOff() {
switches.off()
log.debug “Lights Off”
}
def sunsetHandler(evt)
{
state.DarkOutSide = false
log.debug “sunsetHandler Dark Outside: $state.DarkOutSide”
}
def sunriseHandler(evt)
{
state.DarkOutside = false
log.debug “sunriseHandler Dark Outside: $state.DarkOutSide”
}