3 * Notifies you when Granny is up and running
4 * Let you know when she hasn't been around so you can check in on her
6 * Copyright 2015 SmartThings Hack
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
9 * in compliance with the License. You may obtain a copy of the License at:
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
14 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
15 * for the specific language governing permissions and limitations under the License.
19 name: "Granny's Faucet",
20 namespace: "com.firstbuild",
21 author: "SmartThings Hack",
22 description: "Check to see if Granny used the faucet in a 24 hour period and send a notification if she does.",
24 iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
25 iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png",
26 iconX3Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png")
30 section("Which Faucets?") {
31 input "faucet", "capability.accelerationSensor", title: "Which faucet?", required:true
33 section("Who to text") {
34 input "phone", "phone", title: "Phone number?", required: true
36 section("How often do you want grandma to check in?") {
37 input "minutes", "number", title: "Delay in minutes before we notify you", defaultValue: 1
42 log.trace "Installed with settings: ${settings}"
47 log.trace "Updated with settings: ${settings}"
48 state.lastOpened= [date:now()] // now() is ms time built into SmartApps
49 log.debug "Last Updated Date: $state.lastOpened.date"
56 subscribe(faucet, "acceleration.active", faucetActiveHandler)
57 subscribe(faucet, "acceleration.inactive", faucetInactiveHandler)
60 def faucetInactiveHandler(evt) {
61 log.trace "#faucetClosedHandler#"
62 def inputSeconds = 60*minutes.toInteger()
63 log.debug "waiting...$inputSeconds"
64 runIn(inputSeconds, alertMe)
67 def faucetActiveHandler(evt) {
68 // Don't send a continuous stream of text messages
69 def inputSeconds = 60*minutes.toInteger()
70 def deltaSeconds = inputSeconds
71 def timeAgo = new Date(now() - (1000 * deltaSeconds)) // 61 seconds ago
72 def recentEvents = faucet.eventsSince(timeAgo)
73 log.trace "Found ${recentEvents?.size() ?: 0} events in the last $deltaSeconds seconds"
74 log.debug "Recent Events $recentEvents.value"
75 def alreadySentSms = recentEvents.count {
76 it.value && it.value == "active"
80 log.debug "SMS already sent to $phone1 within the last $minutes minute"
83 sendSms(phone, "Grandma opened faucet")
84 state.lastOpened.date = now()
85 log.debug "Grandma Opened Faucet: $state.lastOpened"
91 log.trace "#alerting...#"
92 def targetTime = state.lastOpened.date + minutes.toInteger()*60*1000
93 log.debug "#alertMe: last: ${state.lastOpened.date} , now: ${now()}, targetTime: ${targetTime}} "
94 if ( now() > targetTime ){
95 log.debug "Grandma needs water badly"
96 sendSms(phone, "Grandma needs water badly")
98 log.debug "Grandma's aight!"