2 * Copyright 2015 SmartThings
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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 name: "Sunrise/Sunset",
21 namespace: "smartthings",
22 author: "SmartThings",
23 description: "Changes mode and controls lights based on local sunrise and sunset times.",
24 category: "Mode Magic",
25 iconUrl: "https://s3.amazonaws.com/smartapp-icons/ModeMagic/rise-and-shine.png",
26 iconX2Url: "https://s3.amazonaws.com/smartapp-icons/ModeMagic/rise-and-shine@2x.png"
30 section ("At sunrise...") {
31 input "sunriseMode", "mode", title: "Change mode to?", required: false
32 input "sunriseOn", "capability.switch", title: "Turn on?", required: false, multiple: true
33 input "sunriseOff", "capability.switch", title: "Turn off?", required: false, multiple: true
35 section ("At sunset...") {
36 input "sunsetMode", "mode", title: "Change mode to?", required: false
37 input "sunsetOn", "capability.switch", title: "Turn on?", required: false, multiple: true
38 input "sunsetOff", "capability.switch", title: "Turn off?", required: false, multiple: true
40 section ("Sunrise offset (optional)...") {
41 input "sunriseOffsetValue", "text", title: "HH:MM", required: false
42 input "sunriseOffsetDir", "enum", title: "Before or After", required: false, options: ["Before","After"]
44 section ("Sunset offset (optional)...") {
45 input "sunsetOffsetValue", "text", title: "HH:MM", required: false
46 input "sunsetOffsetDir", "enum", title: "Before or After", required: false, options: ["Before","After"]
48 section ("Zip code (optional, defaults to location coordinates)...") {
49 input "zipCode1", "text", required: false
51 section( "Notifications" ) {
52 input("recipients", "contact", title: "Send notifications to") {
53 input "sendPushMessage", "enum", title: "Send a push notification?", options: ["Yes", "No"], required: false
54 input "phoneNumber", "phone", title: "Send a text message?", required: false
66 //unschedule handled in astroCheck method
71 subscribe(location, "position", locationPositionChange)
72 subscribe(location, "sunriseTime", sunriseSunsetTimeHandler)
73 subscribe(location, "sunsetTime", sunriseSunsetTimeHandler)
78 def locationPositionChange(evt) {
79 log.trace "locationChange()"
80 schedule("someTime", sunriseHandler)
81 schedule("someTime", sunsetHandler)
85 def sunriseSunsetTimeHandler(evt) {
86 log.trace "sunriseSunsetTimeHandler()"
87 schedule("someTime", sunriseHandler)
88 schedule("someTime", sunsetHandler)
93 def s = getSunriseAndSunset(zipCode: zipCode1, sunriseOffset: sunriseOffset, sunsetOffset: sunsetOffset)
96 def riseTime = s.sunrise
97 def setTime = s.sunset
98 log.debug "riseTime: $riseTime"
99 log.debug "setTime: $setTime"
101 if (state.riseTime != riseTime.time) {
102 unschedule("sunriseHandler")
104 if(riseTime.before(now)) {
105 riseTime = riseTime.next()
108 state.riseTime = riseTime.time
110 log.info "scheduling sunrise handler for $riseTime"
111 schedule(riseTime, sunriseHandler)
114 if (state.setTime != setTime.time) {
115 unschedule("sunsetHandler")
117 if(setTime.before(now)) {
118 setTime = setTime.next()
121 state.setTime = setTime.time
123 log.info "scheduling sunset handler for $setTime"
124 schedule(setTime, sunsetHandler)
128 def sunriseHandler() {
129 log.info "Executing sunrise handler"
136 changeMode(sunriseMode)
139 def sunsetHandler() {
140 log.info "Executing sunset handler"
147 changeMode(sunsetMode)
150 def changeMode(newMode) {
151 if (newMode && location.mode != newMode) {
152 if (location.modes?.find{it.name == newMode}) {
153 setLocationMode(newMode)
154 send "${label} has changed the mode to '${newMode}'"
157 send "${label} tried to change to undefined mode '${newMode}'"
163 if (location.contactBookEnabled) {
164 log.debug("sending notifications to: ${recipients?.size()}")
165 sendNotificationToContacts(msg, recipients)
168 if (sendPushMessage != "No") {
169 log.debug("sending push message")
174 log.debug("sending text message")
175 sendSms(phoneNumber, msg)
183 app.label ?: "SmartThings"
186 private getSunriseOffset() {
187 sunriseOffsetValue ? (sunriseOffsetDir == "Before" ? "-$sunriseOffsetValue" : sunriseOffsetValue) : null
190 private getSunsetOffset() {
191 sunsetOffsetValue ? (sunsetOffsetDir == "Before" ? "-$sunsetOffsetValue" : sunsetOffsetValue) : null