Update groveStreams.groovy
[smartapps.git] / official / safe-watch.groovy
1 /**
2  *  Safe Watch
3  *
4  *  Author: brian@bevey.org
5  *  Date: 2013-11-17
6  *
7  *  Watch a series of sensors for any anomalies for securing a safe or room.
8  */
9
10 definition(
11   name: "Safe Watch",
12   namespace: "imbrianj",
13   author: "brian@bevey.org",
14   description: "Watch a series of sensors for any anomalies for securing a safe.",
15   category: "Safety & Security",
16   iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
17   iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience%402x.png"
18 )
19
20 preferences {
21   section("Things to secure?") {
22     input "contact", "capability.contactSensor",      title: "Contact Sensor",    required: false
23     input "motion",  "capability.motionSensor",       title: "Motion Sensor",     required: false
24     input "knock",   "capability.accelerationSensor", title: "Knock Sensor",      required: false
25     input "axis",    "capability.threeAxis",          title: "Three-Axis Sensor", required: false
26   }
27
28   section("Temperature monitor?") {
29     input "temp",    "capability.temperatureMeasurement", title: "Temperature Sensor", required: false
30     input "maxTemp", "number",                            title: "Max Temperature (°${location.temperatureScale})",   required: false
31     input "minTemp", "number",                            title: "Min Temperature (°${location.temperatureScale})",   required: false
32   }
33
34   section("When which people are away?") {
35     input "people", "capability.presenceSensor", multiple: true
36   }
37
38   section("Notifications?") {
39     input "sendPushMessage", "enum", title: "Send a push notification?", metadata: [values: ["Yes", "No"]], required: false
40     input "phone", "phone", title: "Send a Text Message?", required: false
41   }
42
43   section("Message interval?") {
44     input name: "messageDelay", type: "number", title: "Minutes (default to every message)", required: false
45   }
46 }
47
48 def installed() {
49   init()
50 }
51
52 def updated() {
53   unsubscribe()
54   init()
55 }
56
57 def init() {
58   subscribe(contact, "contact.open",         triggerContact)
59   subscribe(motion,  "motion.active",        triggerMotion)
60   subscribe(knock,   "acceleration.active",  triggerKnock)
61   subscribe(temp,    "temperature",          triggerTemp)
62   subscribe(axis,    "threeAxis",            triggerAxis)
63 }
64
65 def triggerContact(evt) {
66   if(everyoneIsAway()) {
67     send("Safe Watch: ${contact.label ?: contact.name} was opened!")
68   }
69 }
70
71 def triggerMotion(evt) {
72   if(everyoneIsAway()) {
73     send("Safe Watch: ${motion.label ?: motion.name} sensed motion!")
74   }
75 }
76
77 def triggerKnock(evt) {
78   if(everyoneIsAway()) {
79     send("Safe Watch: ${knock.label ?: knock.name} was knocked!")
80   }
81 }
82
83 def triggerTemp(evt) {
84   def temperature = evt.doubleValue
85
86   if((maxTemp && maxTemp < temperature) ||
87      (minTemp && minTemp > temperature)) {
88     send("Safe Watch: ${temp.label ?: temp.name} is ${temperature}")
89   }
90 }
91
92 def triggerAxis(evt) {
93   if(everyoneIsAway()) {
94     send("Safe Watch: ${axis.label ?: axis.name} was tilted!")
95   }
96 }
97
98 private everyoneIsAway() {
99   def result = true
100
101   if(people.findAll { it?.currentPresence == "present" }) {
102     result = false
103   }
104
105   log.debug("everyoneIsAway: ${result}")
106
107   return result
108 }
109
110 private send(msg) {
111   def delay = (messageDelay != null && messageDelay != "") ? messageDelay * 60 * 1000 : 0
112
113   if(now() - delay > state.lastMessage) {
114     state.lastMessage = now()
115     if(sendPushMessage == "Yes") {
116       log.debug("Sending push message.")
117       sendPush(msg)
118     }
119
120     if(phone) {
121       log.debug("Sending text message.")
122       sendSms(phone, msg)
123     }
124
125     log.debug(msg)
126   }
127
128   else {
129     log.info("Have a message to send, but user requested to not get it.")
130   }
131 }