Created
January 15, 2018 00:19
-
-
Save theinventor/b6051ed7a35dc86af5e8a0d70ce04a05 to your computer and use it in GitHub Desktop.
laundry monitor II
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import groovy.time.* | |
definition( | |
name: "Laundry Monitor II", namespace: "HDFLucky", author: "Mr. Lucky", | |
description: "This application is a modification of the SmartThings 'Laundry Monitor' SmartApp. Instead of using a vibration sensor, it utilizes Power (wattage) sensing.", | |
category: "My Apps", | |
iconUrl: "https://dl.dropboxusercontent.com/u/52901840/laundry.png", | |
iconX2Url: "https://dl.dropboxusercontent.com/u/52901840/[email protected]", | |
iconX3Url: "https://dl.dropboxusercontent.com/u/52901840/[email protected]") | |
preferences { | |
section("Notify When Cycle Has Stopped"){ | |
input "sensor1", "capability.powerMeter", multiple: false, required: true | |
} | |
section("Notifications") { | |
input "sendPushMessage", "bool", title: "Push notification(s)?" | |
input "phone1", "phone", title: "Send a text message (enter tel. #)?", required: false | |
input "phone2", "phone", title: "Also send a text message to:", required: false | |
input "speech", "capability.speechSynthesis", title:"Speak message via:", multiple: true, required: false | |
input "message", "text", title: "Notification message:", required: true | |
} | |
section("Flash or Turn On These Lights") { | |
input "switches", "capability.switch", title: "Which?", multiple: true, required: false | |
input "lightMode", "enum", options: ["Flash Lights", "Turn On Lights"], title: "Action?", required: false | |
} | |
section("Appliance Settings") { | |
input "minimumWattage", "decimal", title: "Minimum running wattage", defaultValue: 10, required: false | |
} | |
} | |
def installed() { | |
log.debug "Installed with settings: ${settings}" | |
initialize() | |
} | |
def updated() { | |
log.debug "Updated with settings: ${settings}" | |
unsubscribe() | |
initialize() | |
} | |
def initialize() { | |
subscribe(sensor1, "power", powerInputHandler) | |
} | |
def powerInputHandler(evt) { | |
def latestPower = sensor1.currentValue("power") | |
log.trace "Power: ${latestPower}W" | |
if (!atomicState.isRunning && latestPower > minimumWattage) { | |
atomicState.isRunning = true | |
atomicState.startedAt = now() | |
atomicState.stoppedAt = null | |
log.trace "Cycle started." | |
} else if (atomicState.isRunning && latestPower < minimumWattage) { | |
atomicState.isRunning = false | |
atomicState.stoppedAt = now() | |
log.debug "startedAt: ${atomicState.startedAt}, stoppedAt: ${atomicState.stoppedAt}" | |
log.info message | |
if (phone1) { | |
sendSms phone1, message | |
} else { | |
sendPush message | |
} | |
if (phone2) { | |
sendSms phone2, message | |
} | |
if (speech) { | |
speechAlert(message) | |
} | |
if (switches) { | |
if (lightMode?.equals("Turn On Lights")) { | |
switches.on() | |
} else { | |
flashLights() | |
} | |
} | |
} else { | |
// Do Nothing, no change in either direction | |
} | |
} | |
private speechAlert(msg) { | |
speech.speak(msg) | |
} | |
private flashLights() { | |
def doFlash = true | |
def onFor = onFor ?: 1001 | |
def offFor = offFor ?: 999 | |
def numFlashes = numFlashes ?: 3 | |
log.debug "LAST ACTIVATED IS: ${atomicState.lastActivated}" | |
if (atomicState.lastActivated) { | |
def elapsed = now() - atomicState.lastActivated | |
def sequenceTime = (numFlashes + 1) * (onFor + offFor) | |
doFlash = elapsed > sequenceTime | |
// log.debug "DO FLASH: $doFlash, ELAPSED: $elapsed, LAST ACTIVATED: ${atomicState.lastActivated}" | |
} | |
if (doFlash) { | |
log.debug "FLASHING $numFlashes times" | |
atomicState.lastActivated = now() | |
log.debug "LAST ACTIVATED SET TO: ${atomicState.lastActivated}" | |
def initialActionOn = switches.collect{it.currentSwitch != "on"} | |
def delay = 1L | |
numFlashes.times { | |
log.trace "Switch on after $delay msec" | |
switches.eachWithIndex {s, i -> | |
if (initialActionOn[i]) { | |
s.on(delay:delay) | |
} else { | |
s.off(delay:delay) | |
} | |
} | |
delay += onFor | |
log.trace "Switch off after $delay msec" | |
switches.eachWithIndex {s, i -> | |
if (initialActionOn[i]) { | |
s.off(delay: delay) | |
} else { | |
s.on(delay:delay) | |
} | |
} | |
delay += offFor | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment