4 * Copyright 2014 Jeff's Account
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: "Step Notifier",
18 namespace: "smartthings",
19 author: "SmartThings",
20 description: "Use a step tracker device to track daily step goals and trigger various device actions when your goals are met!",
21 category: "SmartThings Labs",
22 iconUrl: "https://s3.amazonaws.com/smartapp-icons/Partner/jawbone-up.png",
23 iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Partner/jawbone-up@2x.png"
27 page(name: "setupNotifications")
28 page(name: "timeIntervalInput", title: "Only during a certain time") {
30 input "starting", "time", title: "Starting", required: false
31 input "ending", "time", title: "Ending", required: false
36 def setupNotifications() {
38 dynamicPage(name: "setupNotifications", title: "Configure Your Goal Notifications.", install: true, uninstall: true) {
40 section("Select your Jawbone UP") {
41 input "jawbone", "capability.stepSensor", title: "Jawbone UP", required: true, multiple: false
44 section("Notify Me When"){
45 input "thresholdType", "enum", title: "Select When to Notify", required: false, defaultValue: "Goal Reached", options: [["Goal":"Goal Reached"],["Threshold":"Specific Number of Steps"]], submitOnChange:true
46 if (settings.thresholdType) {
47 if (settings.thresholdType == "Threshold") {
48 input "threshold", "number", title: "Enter Step Threshold", description: "Number", required: true
53 section("Via a push notification and/or an SMS message"){
54 input("recipients", "contact", title: "Send notifications to") {
55 input "phone", "phone", title: "Phone Number (for SMS, optional)", required: false
56 input "notificationType", "enum", title: "Select Notification", required: false, defaultValue: "None", options: ["None", "Push", "SMS", "Both"]
60 section("Flash the Lights") {
61 input "lights", "capability.switch", title: "Which Lights?", required: false, multiple: true
62 input "flashCount", "number", title: "How Many Times?", defaultValue: 5, required: false
65 section("Change the Color of the Lights") {
66 input "hues", "capability.colorControl", title: "Which Hue Bulbs?", required:false, multiple:true
67 input "color", "enum", title: "Hue Color?", required: false, multiple:false, options: ["Red","Green","Blue","Yellow","Orange","Purple","Pink"]
68 input "lightLevel", "enum", title: "Light Level?", required: false, options: [10,20,30,40,50,60,70,80,90,100]
69 input "duration", "number", title: "Duration in Seconds?", defaultValue: 30, required: false
72 section("Play a song on the Sonos") {
73 input "sonos", "capability.musicPlayer", title: "On this Sonos player", required: false, submitOnChange:true
75 input "song","enum",title:"Play this track or radio station", required:true, multiple: false, options: songOptions()
76 input "resumePlaying", "bool", title: "Resume currently playing music after notification", required: false, defaultValue: true
77 input "volume", "number", title: "Temporarily change volume", description: "0-100%", required: false
78 input "songDuration", "number", title: "Play for this many seconds", defaultValue: 60, description: "0-100%", required: true
85 private songOptions() {
87 // Make sure current selection is in the set
89 def options = new LinkedHashSet()
90 if (state.selectedSong?.station) {
91 options << state.selectedSong.station
93 else if (state.selectedSong?.description) {
94 // TODO - Remove eventually? 'description' for backward compatibility
95 options << state.selectedSong.description
98 // Query for recent tracks
99 def states = sonos.statesSince("trackData", new Date(0), [max:30])
100 def dataMaps = states.collect{it.jsonValue}
101 options.addAll(dataMaps.collect{it.station})
103 log.trace "${options.size()} songs in list"
104 options.take(20) as List
107 private saveSelectedSong() {
110 log.info "Looking for $thisSong"
111 def songs = sonos.statesSince("trackData", new Date(0), [max:30]).collect{it.jsonValue}
112 log.info "Searching ${songs.size()} records"
114 def data = songs.find {s -> s.station == thisSong}
115 log.info "Found ${data?.station}"
117 state.selectedSong = data
118 log.debug "Selected song = $state.selectedSong"
120 else if (song == state.selectedSong?.station) {
121 log.debug "Selected existing entry '$song', which is no longer in the last 20 list"
124 log.warn "Selected song '$song' not found"
127 catch (Throwable t) {
133 log.debug "Installed with settings: ${settings}"
139 log.debug "Updated with settings: ${settings}"
147 log.trace "Entering initialize()"
150 state.steps = jawbone.currentValue("steps").toInteger()
151 state.goal = jawbone.currentValue("goal").toInteger()
153 subscribe (jawbone,"goal",goalHandler)
154 subscribe (jawbone,"steps",stepHandler)
160 log.trace "Exiting initialize()"
163 def goalHandler(evt) {
165 log.trace "Entering goalHandler()"
167 def goal = evt.value.toInteger()
171 log.trace "Exiting goalHandler()"
174 def stepHandler(evt) {
176 log.trace "Entering stepHandler()"
178 log.debug "Event Value ${evt.value}"
179 log.debug "state.steps = ${state.steps}"
180 log.debug "state.goal = ${state.goal}"
182 def steps = evt.value.toInteger()
184 state.lastSteps = state.steps
188 if (settings.thresholdType == "Goal")
189 stepGoal = state.goal
191 stepGoal = settings.threshold
193 if ((state.lastSteps < stepGoal) && (state.steps >= stepGoal)) { // only trigger when crossing through the goal threshold
195 // goal achieved for the day! Yay! Lets tell someone!
197 if (settings.notificationType != "None") { // Push or SMS Notification requested
199 if (location.contactBookEnabled) {
200 sendNotificationToContacts(stepMessage, recipients)
205 method: settings.notificationType.toLowerCase(),
206 phone: settings.phone
209 sendNotification(stepMessage, options)
213 if (settings.sonos) { // play a song on the Sonos as requested
215 // runIn(1, sonosNotification, [overwrite: false])
220 if (settings.hues) { // change the color of hue bulbs ras equested
222 // runIn(1, hueNotification, [overwrite: false])
227 if (settings.lights) { // flash the lights as requested
229 // runIn(1, lightsNotification, [overwrite: false])
236 log.trace "Exiting stepHandler()"
241 def lightsNotification() {
243 // save the current state of the lights
245 log.trace "Save current state of lights"
247 state.previousLights = [:]
250 state.previousLights[it.id] = it.currentValue("switch")
253 // Flash the light on and off 5 times for now - this could be configurable
255 log.trace "Now flash the lights"
257 for (i in 1..flashCount) {
265 // restore the original state
267 log.trace "Now restore the original state of lights"
270 it."${state.previousLights[it.id]}"()
276 def hueNotification() {
278 log.trace "Entering hueNotification()"
283 else if(color == "Green")
285 else if(color == "Yellow")
287 else if(color == "Orange")
289 else if(color == "Purple")
291 else if(color == "Pink")
295 state.previousHue = [:]
298 state.previousHue[it.id] = [
299 "switch": it.currentValue("switch"),
300 "level" : it.currentValue("level"),
301 "hue": it.currentValue("hue"),
302 "saturation": it.currentValue("saturation")
306 log.debug "current values = ${state.previousHue}"
308 def newValue = [hue: hueColor, saturation: 100, level: (lightLevel as Integer) ?: 100]
309 log.debug "new value = $newValue"
311 hues*.setColor(newValue)
314 log.trace "Exiting hueNotification()"
320 log.debug "runIn ${duration}, resetHue"
321 runIn(duration, resetHue, [overwrite: false])
327 log.trace "Entering resetHue()"
329 it.setColor(state.previousHue[it.id])
331 log.trace "Exiting resetHue()"
334 def sonosNotification() {
336 log.trace "sonosNotification()"
340 if (settings.resumePlaying) {
342 sonos.playTrackAndResume(state.selectedSong, settings.songDuration, settings.volume)
344 sonos.playTrackAndResume(state.selectedSong, settings.songDuration)
347 sonos.playTrackAtVolume(state.selectedSong, settings.volume)
349 sonos.playTrack(state.selectedSong)
352 sonos.on() // make sure it is playing
356 log.trace "Exiting sonosNotification()"