Ok, after discovering you can use state[varname] = varname to store variable values during smartapp running, and check the values in the ide hub -> smartthings -> application state list, I’ve run into a bit of an issue.
I’ve tried resetting state using state.clear() and state = [:] and nothing clears application state.
Is there a way, besides removing the smartapp and reinstalling it to reset application state?
What if you put the debug info in a map that’s not at the base level of state? Like state.debug = [:]. Maybe there is some kind of restriction with clearing the whole state as there may be some system level data stored there too?
Good timing (and thanks for tagging me. While I may not be able to answer every question or update all the docs immediately, I am watching the forums ).
Seems like a problem. Garbage collection does not seem to work, and there is no .delete() or .finalize() operating method…
@slagle@jody.albritton@Jim can we get a method to remove state or atomicState variables that we don’t need. The remove the SmartApp / device is very painful, especially in parent / child apps.
For atomicState, there is no way to clear everything from what I can tell right now. You could set each value you want to remove to null; that won’t remove the key but it will set the value to null.
state.remove('your_key') should work. That will remove the entry from state once the app is finished executing.
And actually, I think state.clear() will clear atomicState too (once the app is done executing). But I don’t know of a way off-hand to remove individual entries from atomicState.
Interesting. It worked in my attempt else I wouldn’t have put that down, but maybe my quick test missed something. When my app executed again and logged the contents of state, the removed key was gone.
/**
* state example
*
* Copyright 2015 james anderson
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
* for the specific language governing permissions and limitations under the License.
*
*/
definition(
name: "state example",
namespace: "",
author: "james anderson",
description: "state",
category: "",
iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png",
iconX3Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png")
preferences {
section("Title") {
// TODO: put inputs here
}
}
def installed() {
log.debug "Installed with settings: ${settings}"
initialize()
}
def updated() {
log.debug "Updated with settings: ${settings}"
unsubscribe()
initialize()
}
def initialize() {
state.clear()
log.debug "in initialize, state is: $state"
runIn(20, addStuff)
runIn(40, testClearSingle)
// scheduled execution to show final state
runIn(60, logIt)
}
def addStuff() {
state.s1 = "test"
state.s2 = "test again"
logIt()
}
def testClearSingle() {
log.debug "testClearSingle, state: $state"
// remove single item, will be gone from state next execution
state.remove("s1")
logIt()
}
def logIt() {
log.debug "STATE: $state"
}