From d7127320a98fb287ccdfeb76d9f2d24be23c0e45 Mon Sep 17 00:00:00 2001 From: napalmcsr Date: Wed, 30 Mar 2022 08:37:34 -0700 Subject: [PATCH] Remove minute polling for inconsistencies I removed the polling and made it based on presence changes. if a presence changes, it sets a timer for 30 min for all of the incosistencies to resolve or it sends the notification. --- standardCombinedPresenceInstance.groovy | 70 +++++++++++-------------- 1 file changed, 30 insertions(+), 40 deletions(-) diff --git a/standardCombinedPresenceInstance.groovy b/standardCombinedPresenceInstance.groovy index ef485a8..396b363 100644 --- a/standardCombinedPresenceInstance.groovy +++ b/standardCombinedPresenceInstance.groovy @@ -147,7 +147,7 @@ def initialize() { state.lastConsistentTime = new Date() } - runEvery1Minute(checkForInconsistencies) + state.SentInconsistentMessage = false } @@ -166,52 +166,36 @@ def checkForInconsistencies() { inputsAreAllPresent = false } } - def inputsAreInconsistent = !(inputsAreAllPresent || inputsAreAllNotPresent) - + log "inputsAreAllPresent ${inputsAreAllPresent}" log "inputsAreAllNotPresent ${inputsAreAllNotPresent}" log "inputsAreInconsistent ${inputsAreInconsistent}" - - def currentTime = new Date() - if (inputsAreInconsistent) { - def lastConsistentTime = new Date() - if (state.lastConsistentTime) { - lastConsistentTime = Date.parse("yyyy-MM-dd'T'HH:mm:ssZ", state.lastConsistentTime) - } - - def lastInconsistencyWarningTime = new Date() - if (state.lastInconsistencyWarningTime) { - lastInconsistencyWarningTime = Date.parse("yyyy-MM-dd'T'HH:mm:ssZ", state.lastInconsistencyWarningTime) - } - - def timeSinceConsistency = TimeCategory.minus(currentTime, lastConsistentTime) - def timeSinceLastWarning = TimeCategory.minus(currentTime, lastInconsistencyWarningTime) - - log "timeSinceConsistency.minutes: ${timeSinceConsistency.minutes}" - log "timeSinceConsistency.hours: ${timeSinceConsistency.hours}" - log "timeSinceConsistency.days: ${timeSinceConsistency.days}" - log "timeSinceLastWarning.hours ${timeSinceLastWarning.hours}" - log "timeSinceLastWarning.days ${timeSinceLastWarning.days}" - - if ((timeSinceConsistency.minutes > 30 || timeSinceConsistency.hours > 0 || timeSinceConsistency.days > 0) && (timeSinceLastWarning.hours > 18 || timeSinceLastWarning.days > 0)) { + if (!state.AlreadyInconsistant&&!state.SentInconsistentMessage){ + state.AlreadyInconsistant = true + runIn(1800, SendInconsistentMessage) + } + } + else { + state.AlreadyInconsistant = false + unschedule(SendInconsistentMessage) + unschedule(ResetNotify) + } +} + +def SendInconsistentMessage() { def msg = "Input sensors for ${outputSensor.displayName} have been inconsistent for 30 minutes. This may mean one of your presence sensors is not updating." - log(msg) - - if (notifyAboutInconsistencies) { - sendNotification(msg) - } - - state.lastInconsistencyWarningTime = currentTime - } - } - else { - state.lastConsistentTime = currentTime - } + sendNotification(msg) + state.SentInconsistentMessage = true + runIn(60*60*23.5,ResetNotify) } +def ResetNotify() { + state.SentInconsistentMessage = false + checkForInconsistencies() +} def sendNotification(msg) { if (msg && msg.size() > 0) { @@ -238,6 +222,10 @@ def arrivedHandler(evt) { sendNotification("Arrived: ${outputSensor.displayName}") } } + + if (notifyAboutInconsistencies){ + checkForInconsistencies() + } } @@ -257,6 +245,10 @@ def departedHandler(evt) { sendNotification("Departed: ${outputSensor.displayName}") } } + + if (notifyAboutInconsistencies){ + checkForInconsistencies() + } } @@ -274,5 +266,3 @@ def log(msg) { } - -