Update step-notifier.groovy
[smartapps.git] / official / grannys-faucet.groovy
1 /**
2  *  Granny's Faucet
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
5  *
6  *  Copyright 2015 SmartThings Hack
7  *
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:
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
16  *
17  */
18 definition(
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.",
23     category: "My Apps",
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")
27
28
29 preferences {
30     section("Which Faucets?") {
31         input "faucet", "capability.accelerationSensor", title: "Which faucet?", required:true
32      }
33      section("Who to text") {
34         input "phone", "phone", title: "Phone number?", required: true  
35      }
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
38      }
39 }
40
41 def installed() {
42     log.trace "Installed with settings: ${settings}"
43     initialize()
44 }
45
46 def updated() {
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"
50
51     unsubscribe()
52     initialize()
53 }
54
55 def initialize() {
56      subscribe(faucet, "acceleration.active", faucetActiveHandler)
57      subscribe(faucet, "acceleration.inactive", faucetInactiveHandler)
58 }
59
60 def faucetInactiveHandler(evt) {
61     log.trace "#faucetClosedHandler#"
62     def inputSeconds = 60*minutes.toInteger()
63     log.debug "waiting...$inputSeconds"
64     runIn(inputSeconds, alertMe)
65 }
66
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"
77         } > 1
78     
79     if (alreadySentSms) {
80         log.debug "SMS already sent to $phone1 within the last $minutes minute"
81     } else {
82         //  
83         sendSms(phone, "Grandma opened faucet")
84         state.lastOpened.date = now()
85         log.debug "Grandma Opened Faucet: $state.lastOpened"
86
87     }   
88 }
89
90 def alertMe() {
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")     
97     } else {
98         log.debug "Grandma's aight!"
99     }
100 }