Any way to use a variable when using state to preserve status?

I’m trying to write some code that tracks weather a device was activated via a smart app or via a something else. I know about the isPhysical property - but that doesn’t show as ‘physical’ if I use my phone or tablet to turn the device on or off.

The idea is to suppress any further automatic control of the device until some condition is met (like everyone left the house).

The code needs to support multiple devices and track their control state individually (perhaps someone turned a light on in the afternoon to cut shadows - so we don’t want the illuminance sensor turning it off just because it’s bright in the room, however the other light in the room should remain on automatic control and come on if the room gets too dark.)

My ‘big idea’ was to use state.controlOf$theswitch - with the expected result being if theswitch is an array of “Living Room Light 1” and “Living Room Light 2” then what would get written to the state database would be “controlOfLiving Room Light 1” for example. What is actually being written to the database is “controlOf$theswitch”.

Is there a way to force groovy to evaluate $theswitch and append it to controlState rather than using the literal of “controlState$theswitch”?

I’ve only had a smartthings hub for about 2 weeks and am trying to learn groovy and the smartthings APIs - but I can’t find a way around this one - any help is greatly appreciated.

If it helps - here’s some of the code setting this up. Pretty standard stuff -

preferences {
section(“Turn on lights”) {
input “theswitch”, “capability.switch”, required: false, title:“Lights”, multiple: true
}
}

def installed() {
log.debug “Installed with settings: ${settings}”

initialize()

}

def updated() {
log.debug “Updated with settings: ${settings}”

unsubscribe()
initialize()

}

def initialize() {
// TODO: subscribe to attributes, devices, locations, etc.
subscribe(themotion, “motion.active”, motionDetectedHandler)
subscribe(themotion, “motion.inactive”, motionStoppedHandler)
subscribe(theilluminance, “illuminance”, illuminanceHandler)
state.controlOf$theswitch = “automatic”
}

The code sort of looks like it works - until you test the app with more than one switch - and then it became really obvious that it was only writing to a single variable.

Using:
state.each {key, val ->
log.debug “state key: $key, value: $val”

revealed that sure enough - what got written to the database was:

state key: controlOf$theswitch, value: automatic

Any thoughts or suggestions?

The switch names aren’t being saved to state because controlOf$theswitch isn’t in quotes, but even within quotes it wouldn’t do what you’re expecting.

Using the following would probably achieve your goal:

theswitch?.each {
 state["$it"] = "automatic"
}

FYI, if you select all the code after pasting it into a post and click the < / > toolbar button it makes it a lot easier to read.

The SmartApps CoRE and WebCoRE should be able to handle this logic so it might be a lot faster to use one of those instead of writing something custom…

2 Likes

Thanks for all 3 tips! The syntax you offered is working for me, I will keep the preformatted text button in mind and use it going forward, and I will check out CoRE and WebCoRE sometime soon.

At the moment I’m still trying to explore what all the platform can do - and I tend to like the sense of accomplishment I get from doing it myself rather than relying too much on someone else’s work!