ContactSensor -- Reading Status

Hi All,

I am a newbie. I have started to write my first app, but I have run into some questions and I was hoping some one can help answer my questions.

My app is to “warn if z-wave lock is locked BUT the Contact Sensor is Open” I can’t figure out how to read the state of the Contact Sensor. I am handling the door lock or unlock event and then want to check the contact Sensor to see if the door is really closed.

I tried this def sensorState = thesensor(“open”), but no cigar.

Here is my app: thanks, Sal

preferences {
section(“Check Door Closed When Locked”) {
input “thesensor”, “capability.contactSensor”, required: true, title: “Which Open/Close Sensor?”
section(“Doors Locked”){
input “thelock”, “capability.lock”, title:“door lock”, required: true, multiple: false



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



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



def initialize() {
// TODO: subscribe to attributes, devices, locations, etc.
subscribe(thelock, “lock.locked”, lockLockedHandler)
subscribe(thelock, “lock.unlocked”, lockLockedHandler)

// TODO: implement event handlers

def lockLockedHandler(evt){
def sensorState = thesensor(“open”)
if (evt.value == “locked”) {
// locked detected
log.debug "lockLockedHandler called: LOCK $evt"
if (sensorState.value == “open”) {
log.debug “lockLockedHandler detected $thesensor is OPEN”
if (evt.value == “unlocked”) {
// unlocked detected
log.debug “lockLockedHandler called: UNLOCK $evt”


Maybe try this:

def sensorState = thesensor.currentState("contact")
if sensorState.......

you also don’t need two device subscriptions for “thelock”, as both are using the same handler, and you are checking for the event value state. just use “lock”


Or you could use this,

if(contact1.latestValue(“contact”) == “open”)

Thank you:
Johnconstantelo — that did the trick.
Mike_Maxwell – I learned something new. Just using lock and one handler worked.
ashutosh1982 – can I use such a construct to check all my sensors?

1 Like

The value statement you have is within an event handler, these are only executed when a device creates an event, so one event from your device = 1 method execution.
Now, when some given device emits an event, you can certainly check a list of devices to see what current state they’re in.
There are several forms of this depending on what you want to do with the result:
This form returns true only if all the motion detectors are inactive:

def state = motionSensors.currentState("motion").every{ s -> s.value == "inactive"}

You can also use a simpler form of test, namely motionSensors.currentMotion. Or, if(contact1.currentContact == "open").

currentAttribute is the same as currentValue("attribute")