Update Light_Rule.groovy
[smartapps.git] / official / when-its-going-to-rain.groovy
1 /**
2  *  Copyright 2015 SmartThings
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  *  in compliance with the License. You may obtain a copy of the License at:
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  *  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
10  *  on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
11  *  for the specific language governing permissions and limitations under the License.
12  *
13  *  When It's Going To Rain
14  *
15  *  Author: SmartThings
16  */
17 definition(
18     name: "When It's Going to Rain",
19     namespace: "smartthings",
20     author: "SmartThings",
21     description: "Is your shed closed? Are your windows shut? Is the grill covered? Are your dogs indoors? Will the lawn and plants need to be watered tomorrow?",
22         category: "Convenience",
23     iconUrl: "https://s3.amazonaws.com/smartapp-icons/Meta/text.png",
24     iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Meta/text@2x.png"
25 )
26
27 preferences {
28         section("Zip code..."){
29                 input "zipcode", "text", title: "Zipcode?"
30         }
31         // TODO: would be nice to cron this so we could check every hour or so
32         section("Check at..."){
33                 input "time", "time", title: "When?"
34         }
35         section("Things to check..."){
36                 input "sensors", "capability.contactSensor", multiple: true
37         }
38         section("Text me if I anything is open..."){
39         input("recipients", "contact", title: "Send notifications to") {
40             input "phone", "phone", title: "Phone number?"
41         }
42         }
43 }
44
45
46 def installed() {
47         log.debug "Installed: $settings"
48         schedule(time, "scheduleCheck")
49 }
50
51 def updated() {
52         log.debug "Updated: $settings"
53         unschedule()
54         schedule(time, "scheduleCheck")
55 }
56
57 def scheduleCheck() {
58         def response = getWeatherFeature("forecast", zipcode)
59         if (isStormy(response)) {
60                 def open = sensors.findAll { it?.latestValue("contact") == 'open' }
61                 if (open) {
62             if (location.contactBookEnabled) {
63                 sendNotificationToContacts("A storm is a coming and the following things are open: ${open.join(', ')}", recipients)
64             }
65             else {
66                 sendSms(phone, "A storm is a coming and the following things are open: ${open.join(', ')}")
67             }
68                 }
69         }
70 }
71
72 private isStormy(json) {
73         def STORMY = ['rain', 'snow', 'showers', 'sprinkles', 'precipitation']
74
75         def forecast = json?.forecast?.txt_forecast?.forecastday?.first()
76         if (forecast) {
77                 def text = forecast?.fcttext?.toLowerCase()
78                 if (text) {
79                         def result = false
80                         for (int i = 0; i < STORMY.size() && !result; i++) {
81                                 result = text.contains(STORMY[i])
82                         }
83                         return result
84                 } else {
85                         return false
86                 }
87         } else {
88                 log.warn "Did not get a forecast: $json"
89                 return false
90         }
91 }