Create empty_app.groovy
[smartapps.git] / third-party / WindowOrDoorOpen.groovy
index 2bb4901b5c3a5d4fe7573510fba6c2f4b3be6b53..5bb1f9c0aec3827968d925b501a2dd8ab0b32f90 100755 (executable)
@@ -82,15 +82,19 @@ def updated() {
 }
 
 def initialize() {     
+       // Adjustment for saved state
+       frequency = frequency + 1
        def MAX_CONTACT=30
        state?.lastThermostatMode = null
-       // subscribe to all contact sensors to check for open/close events
+       // subscribe-to all contact sensors to check for open/close events
        state?.status=[]    
        state?.count=[]    
        state.lastThermostatMode = ""
     
        int i=0    
        theSensor.each {
+               //subscribe(theSensor, "contact.closed", sensorTriggered0)
+               //subscribe(theSensor, "contact.open", sensorTriggered0)
                subscribe(theSensor[i], "contact.closed", "sensorTriggered${i}")
                subscribe(theSensor[i], "contact.open", "sensorTriggered${i}")
                state?.status[i] = " "
@@ -271,8 +275,8 @@ def sensorTriggered(evt, indice=0) {
                clearStatus(indice)
        } else if ((evt.value == "open") && (state?.status[indice] != "scheduled")) {
                def takeActionMethod= "takeAction${indice}"       
-               runIn(freq, "${takeActionMethod}",[overwrite: false])
                state?.status[indice] = "scheduled"
+               runIn(freq, "${takeActionMethod}",[overwrite: false])
                log.debug "${theSensor[indice]} is now open and will be checked every ${delay} minute(s) by ${takeActionMethod}"
        }
 }
@@ -473,10 +477,13 @@ def takeAction(indice=0) {
        def maxNotif = (givenMaxNotif) ?: 5
        def max_open_time_in_min = maxOpenTime ?: 5 // By default, 5 min. is the max open time
        def msg
+       def open = "open"
+       def closed = "closed"
     
        def contactState = theSensor[indice].currentState("contact")
        log.trace "takeAction>${theSensor[indice]}'s contact status = ${contactState.value}, state.status=${state.status[indice]}, indice=$indice"
-       if ((state?.status[indice] == "scheduled") && (contactState.value == "open")) {
+       //if ((state?.status[indice] == "scheduled") && (contactState.value == "open")) {
+       if ((state?.status[indice] == "scheduled") && (contactState == open)) {
                state.count[indice] = state.count[indice] + 1
                log.debug "${theSensor[indice]} was open too long, sending message (count=${state.count[indice]})"
                def openMinutesCount = state.count[indice] * delay
@@ -491,7 +498,7 @@ def takeAction(indice=0) {
                        theVoice.speak(msg)
                }
 
-               if ((tstats) && (openMinutesCount > max_open_time_in_min)) {
+               if ((tstats) /*&& (openMinutesCount > max_open_time_in_min)*/) {
                                                        
                        save_tstats_mode()        
                        tstats.off()
@@ -511,8 +518,9 @@ def takeAction(indice=0) {
                takeActionMethod= "takeAction${indice}"       
                msg = "contact still open at ${theSensor[indice]}, about to reschedule $takeActionMethod"
                log.debug msg            
-               runIn(freq, "${takeActionMethod}", [overwrite: false])
-       } else if (contactState.value == "closed") {
+               //runIn(freq, "${takeActionMethod}", [overwrite: false])
+       //} else if (contactState.value == "closed") {
+       } else if (contactState == closed) {
                restore_tstats_mode()
                clearStatus(indice)
                takeActionMethod= "takeAction${indice}"       
@@ -534,7 +542,7 @@ private void save_tstats_mode() {
                return    
        } 
        tstats.each {
-               it.poll() // to get the latest value at thermostat            
+               //it.poll() // to get the latest value at thermostat            
                state.lastThermostatMode = state.lastThermostatMode + "${it.currentThermostatMode}" + ","
        }    
        log.debug "save_tstats_mode>state.lastThermostatMode= $state.lastThermostatMode"