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: "Rise and Shine",
21 namespace: "smartthings",
22 author: "SmartThings",
23 description: "Changes mode when someone wakes up after a set time in the morning.",
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("When there's motion on any of these sensors") {
31 input "motionSensors", "capability.motionSensor", multiple: true
33 section("During this time window (default End Time is 4:00 PM)") {
34 input "timeOfDay", "time", title: "Start Time?"
35 input "endTime", "time", title: "End Time?", required: false
37 section("Change to this mode") {
38 input "newMode", "mode", title: "Mode?"
40 section("And (optionally) turn on these appliances") {
41 input "switches", "capability.switch", multiple: true, required: false
43 section( "Notifications" ) {
44 input("recipients", "contact", title: "Send notifications to") {
45 input "sendPushMessage", "enum", title: "Send a push notification?", options: ["Yes", "No"], required: false
46 input "phoneNumber", "phone", title: "Send a Text Message?", required: false
52 log.debug "installed, current mode = ${location.mode}, state.actionTakenOn = ${state.actionTakenOn}"
57 log.debug "updated, current mode = ${location.mode}, state.actionTakenOn = ${state.actionTakenOn}"
63 log.trace "timeOfDay: $timeOfDay, endTime: $endTime"
64 subscribe(motionSensors, "motion.active", motionActiveHandler)
65 subscribe(location, modeChangeHandler)
66 if (state.modeStartTime == null) {
67 state.modeStartTime = 0
71 def modeChangeHandler(evt) {
72 state.modeStartTime = now()
75 def motionActiveHandler(evt)
77 // for backward compatibility
78 if (state.modeStartTime == null) {
79 subscribe(location, modeChangeHandler)
80 state.modeStartTime = 0
84 def modeStartTime = new Date(state.modeStartTime)
85 def timeZone = location.timeZone ?: timeZone(timeOfDay)
86 def startTime = timeTodayAfter(modeStartTime, timeOfDay, timeZone)
87 def endTime = timeTodayAfter(startTime, endTime ?: "16:00", timeZone)
88 log.debug "startTime: $startTime, endTime: $endTime, t0: ${new Date(t0)}, modeStartTime: ${modeStartTime}, actionTakenOn: $state.actionTakenOn, currentMode: $location.mode, newMode: $newMode "
90 if (t0 >= startTime.time && t0 <= endTime.time && location.mode != newMode) {
91 def message = "Good morning! SmartThings changed the mode to '$newMode'"
93 setLocationMode(newMode)
96 def dateString = new Date().format("yyyy-MM-dd")
97 log.debug "last turned on switches on ${state.actionTakenOn}, today is ${dateString}"
98 if (state.actionTakenOn != dateString) {
99 log.debug "turning on switches"
100 state.actionTakenOn = dateString
106 log.debug "not in time window, or mode is already set, currentMode = ${location.mode}, newMode = $newMode"
112 if (location.contactBookEnabled) {
113 log.debug("sending notifications to: ${recipients?.size()}")
114 sendNotificationToContacts(msg, recipients)
117 if (sendPushMessage != "No") {
118 log.debug("sending push message")
123 log.debug("sending text message")
124 sendSms(phoneNumber, msg)