Executing code when a mode is changed from another mode


(Dav Glass) #1

I’m trying to have my main garage door when I come home. So I’m wanting to hook into the mode change for “Home”, but I only want to do it when the previous mode was “Away”. If I just trigger it on “Home” it will open with the “Good Morning, Hello Home”. Is there a way in the SmartApp that I can see what mode the location was previously set to before it was changed to the one that I am listening on?


(Bruce) #2

You could subscribe in your app to every mode change: subscribe(location, modeChangeHandler), and then keep track of the mode in a state variable, such as state.lastMode. Then your app would know what the previous mode was and apply logic accordingly. You’d have to check the previous mode before updating state.lastMode to the new mode.


(Dav Glass) #3

Thanks! I was looking around for something like that but didn’t see any docs on what global variables are available inside the SmartApp. Like location, it (seen this in other apps but not sure what it is) and others.


(Bruce) #4

You can iterate through an array using .each, e.g., switches.each { some code }. Inside there it refers to the current array element in the iteration.


(Barry) #5

You might consider instead using a presence sensor to recognize your return home as the initiator for opening your garage door. You could use your own personal presence sensor (phone) or put a SmartSense Presence in your car. Then you simply set the “Open when someone arrives” option on the garage door switch.

Just a thought…


(Dav Glass) #6

Oh, I am doing that too. But I want it to do different things for different mode states.

Here’s what I ended up with:


(Tony Gutierrez) #7

See this bug: Mode change subscription bug, at least in docs