4 * Copyright 2015 SmartThings
6 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
7 * in compliance with the License. You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
12 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
13 * for the specific language governing permissions and limitations under the License.
17 name: "Turn on by ZIP code",
18 namespace: "examples",
19 author: "SmartThings",
20 description: "Turn on lights based on ZIP code",
22 iconUrl: "https://s3.amazonaws.com/smartapp-icons/ModeMagic/rise-and-shine.png",
23 iconX2Url: "https://s3.amazonaws.com/smartapp-icons/ModeMagic/rise-and-shine@2x.png",
24 iconX3Url: "https://s3.amazonaws.com/smartapp-icons/ModeMagic/rise-and-shine@2x.png")
28 input "switches", "capability.switch", title: "Which lights to turn on?"
30 section("Sunset offset (optional)...") {
31 input "sunsetOffsetValue", "text", title: "HH:MM", required: false
34 input "zipCode1", "text", required: false
51 def scheduleNextSunset(date = null) {
52 /*def s = getSunriseAndSunset(zipCode: zipCode1, sunsetOffset: getSunsetOffset(), date: date)
54 def setTime = s.sunset
55 log.debug "setTime: $setTime"
57 // use state to keep track of sunset times between executions
58 // if sunset time has changed, unschedule and reschedule handler with updated time
59 if(state.setTime != setTime.time) {
60 unschedule("sunsetHandler")
62 if(setTime.before(now)) {
63 setTime = setTime.next()
66 state.setTime = setTime.time
68 log.info "scheduling sunset handler for $setTime"
69 schedule(setTime, sunsetHandler)
71 // schedule("someTime", sunsetHandler)
75 log.debug "turning on lights"
78 // schedule for tomorrow
79 scheduleNextSunset(new Date() + 1)
82 private getSunsetOffset() {
83 //if there is an offset, make negative since we only care about before
84 sunsetOffsetValue ? "-$sunsetOffsetValue" : null