How do you properly define a Global/Instance variable in ST/Groovy?
I’m trying to :
Send a notification when a temperature sensor is past a maximum
Then continue sending reminders every 5 minutes until…
The temperature falls 3 degrees under the maximum
send a final text saying temperature is OK.
I could do this in a dozen different languages in 5 seconds but I’m new to Groovy and even newer to SmartThings implementation of Groovy.
Here’s the logic I’m trying to translate into groovy:
tooHot is always null, and if I set tooHot at the beginning of the temperaturehandler I get a missing property exception. How do I get this logic to work in SmartThings?
Hey @Milsoft, that’s a great question, and one that comes up often when new to SmartThings.
It’s not a Groovy thing, but rather a function of the fact that SmartApps don’t continuously run, but instead are executed according to certain events or schedules. To store data across those executions, you can use the state variable available in every SmartApp:
state.myvar = "myvalue"
And then get it back like this: state.mybar // returns "myvalue"
Sorry, a little late to the convo, but having this same issue. Is there really no way to have constant global variables? I want a global “debug” variable that disables code that activates devices, routines, and push notifications. Reading this post (link below) about groovy global vars, you’re supposed to be able to have global vars that aren’t "def"ed. However it doesn’t work in ST. I am using the def debug() { return true } but then if (!debug) always returns false. Have to use if(!debug())… just a little annoying…