Executing code when a mode is changed from another mode

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?

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.

1 Like

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.

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.

1 Like

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…

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:

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