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?