Groovy Help: Retrieve Level of Dimmer and Retrieve State of Contact Sensor

I have just started working with Groovy and have some questions. I have only pasted in excerpts from sections. If the entire Groovy script is needed to recommend a solution, pelase let me know.

Everything below works with the exception of those I have marked Not Working. Others marked Help

preferences {
section(“Turn on lights when door or window opens:”) {
input “theDoorWindow”, “capability.contactSensor”, required: true, title: “Door/Window Sensor”

def initialize() { // with each of these, the appropriate handler is called as expected
subscribe(theDoorWindow, “”, doorWindowOpenHandler)
subscribe(theDoorWindow, “contact.closed”, doorWindowClosedHandler)

def doorWindowOpenHandler(evt) { // turn on the light and set the dimmer to X% on the other light. wait y minutes, if still on, flash both lights at 100% x times (could make 100% parameter)

log.debug "doorWindowOpenHandler called: $evt"

def theOriginalSwitchState = theSwitch.currentState("switch").getValue()     // save original level
log.debug "The Original Switch State: $theOriginalSwitchState"			// This works fine, output is the text and either true or valse
def theOriginalLevel = theDimmerSwitch.getLevel("Switch Level")           	 // save original level
log.debug "The Original Dimmer Level: $theOriginalLevel" 				// This is Not Working.  Output is null.  I have tried multiple variations, and this is just the last attempt


// theDimmerSwitch.setLevel(theBrightness, theChangeLevelRate) // first is brightness, second paramer is how fast to change brightness. Second parameter seems ignored
theDimmerSwitch.setLevel(theBrightness) // first is brightness, second paramer is how fast to change brightness. Second parameter seems ignored
// the previous line is working, other than the rate of change. Help - Would be nice to know what the units are (seconds, millisecons, etc)
runIn(5,checkDoorStateHandler) // TODO: for testing we are using a constant number of seconds, later will use whatever is input
// The above line is working fine

def doorWindowClosedHandler(evt) { // wait 5 minutes (using 10 seconds for testing), set lights back to previous state
log.debug “doorWindowClosedHandler called: $evt”					// TODO: should return to original state
theDimmerSwitch.setLevel(10)			// TODO: this should be changed to original dimmer level once we can get it


def checkDoorStateHandler(evt) {
log.debug “checkDoorStateHandler called: $evt”
// subscribe(theDoorWindow, “contact.closed”, doorWindowClosedHandler) // Not Working, probably not appropriate here
if (theDoorWindow(“”)) { // Example from switch - def theOriginalSwitchState = theSwitch.currentState(“switch”).getValue()
// The above line is Not Working
// Logged error “groovy.lang.MissingMethodException: No signature of method: script_app_040e80f46205590bfca961eb8579c52c90a77dda21eaa467aae55faa1a071bf7.theDoorWindow() is applicable for argument types: (java.lang.String) values: [] @line 127 (checkDoorStateHandler)”
theDimmerSwitch.setLevel(100) // TODO: 0-100 use a variable later for the warning level
runIn(1,flashDimmableLightHandler) // TODO: 1-alot this is in seconds
} else {
// TODO: not sure if we need to go back to previous state here or in door closed. This path will not set up the delay again, so door closed handler should be correct

def flashDimmableLightHandler(evt) { // Pretty sure this will work, but due to the above error, have not tested.
log.debug “flashDimmableLightHandler called: $evt”

theDimmerSwith.setLevel(0)				// TODO: 0-100 use a variable later for the warning low light level
runIn(1,checkDoorStateHandler)			// TODO: 1-alot this is in seconds


You get the attribute value of a device like:


1 Like

Thank you so much, that was it! Also, thank you for going through my huge code snippet to figure it out.

I noticed that when I call it works fine, but only theSwitch.on() will work. Is that normal? If so, do you know why that is (with/without ())?

Commands like on/off should always include the ().

1 Like