Variable Passed Between Methods/Events?

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?


//Defination provides metadata about the SmartApp
name: “Arrive After Sunset”,
namespace: “smartthings”,
author: “Bob Wagenhoffer”,
description: “Momentarily turn something on only if you arrive after sunset.”,
category: “Convenience”,
iconUrl: “”,
iconX2Url: “

// 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)

// When a user updates a SmartApp the defined installed() method will be called
def updated()
log.debug "Updated with settings: ${settings}"
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)) {
    log.debug "Lights On"
runIn(TimeDelay*60, LightsOff)


// Turn off Lights
def LightsOff() {
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”

“state” is only shared between invocations of the DTH after the invocation has fully exited.

Try using “atomicState” which is supposed to be written immediately.

Also; check the Device Instance details (under My Devices) to see the current value of state while doing debugging.

Thank you for your help, my problem was with how I defined the Presence Sensor. In the examples they use presence1, I changed It to presence. For whatever reason this worked in the simulator but not is real life. I found a Device Handler that allowed me change the state of my iPhone and was able to see and debug my problem (Force Change status of mobile presence sensor?). Your tip on obtaining information from the Device Instance made all the difference, was able to see the value of my variables on the server, did not know you could to this.

1 Like