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: "chooseTrack", title: "Select a song or station")
29 page(name: "timeIntervalInput", title: "Only during a certain time") {
31 input "starting", "time", title: "Starting", required: false
32 input "ending", "time", title: "Ending", required: false
37 def setupNotifications() {
39 dynamicPage(name: "setupNotifications", title: "Configure Your Goal Notifications.", install: true, uninstall: true) {
41 section("Select your Jawbone UP") {
42 input "jawbone", "capability.stepSensor", title: "Jawbone UP", required: true, multiple: false
45 section("Notify Me When"){
46 input "thresholdType", "enum", title: "Select When to Notify", required: false, defaultValue: "Goal Reached", options: [["Goal":"Goal Reached"],["Threshold":"Specific Number of Steps"]], submitOnChange:true
47 if (settings.thresholdType) {
48 if (settings.thresholdType == "Threshold") {
49 input "threshold", "number", title: "Enter Step Threshold", description: "Number", required: true
54 section("Via a push notification and/or an SMS message"){
55 input("recipients", "contact", title: "Send notifications to") {
56 input "phone", "phone", title: "Phone Number (for SMS, optional)", required: false
57 input "notificationType", "enum", title: "Select Notification", required: false, defaultValue: "None", options: ["None", "Push", "SMS", "Both"]
61 section("Flash the Lights") {
62 input "lights", "capability.switch", title: "Which Lights?", required: false, multiple: true
63 input "flashCount", "number", title: "How Many Times?", defaultValue: 5, required: false
66 section("Change the Color of the Lights") {
67 input "hues", "capability.colorControl", title: "Which Hue Bulbs?", required:false, multiple:true
68 input "color", "enum", title: "Hue Color?", required: false, multiple:false, options: ["Red","Green","Blue","Yellow","Orange","Purple","Pink"]
69 input "lightLevel", "enum", title: "Light Level?", required: false, options: [10,20,30,40,50,60,70,80,90,100]
70 input "duration", "number", title: "Duration in Seconds?", defaultValue: 30, required: false
73 section("Play a song on the Sonos") {
74 input "sonos", "capability.musicPlayer", title: "On this Sonos player", required: false, submitOnChange:true
76 input "song","enum",title:"Play this track or radio station", required:true, multiple: false, options: songOptions()
77 input "resumePlaying", "bool", title: "Resume currently playing music after notification", required: false, defaultValue: true
78 input "volume", "number", title: "Temporarily change volume", description: "0-100%", required: false
79 input "songDuration", "number", title: "Play for this many seconds", defaultValue: 60, description: "0-100%", required: true
87 dynamicPage(name: "chooseTrack") {
89 input "song","enum",title:"Play this track", required:true, multiple: false, options: songOptions()
94 private songOptions() {
96 // Make sure current selection is in the set
98 def options = new LinkedHashSet()
99 if (state.selectedSong?.station) {
100 options << state.selectedSong.station
102 else if (state.selectedSong?.description) {
103 // TODO - Remove eventually? 'description' for backward compatibility
104 options << state.selectedSong.description
107 // Query for recent tracks
108 def states = sonos.statesSince("trackData", new Date(0), [max:30])
109 def dataMaps = states.collect{it.jsonValue}
110 options.addAll(dataMaps.collect{it.station})
112 log.trace "${options.size()} songs in list"
113 options.take(20) as List
116 private saveSelectedSong() {
119 log.info "Looking for $thisSong"
120 def songs = sonos.statesSince("trackData", new Date(0), [max:30]).collect{it.jsonValue}
121 log.info "Searching ${songs.size()} records"
123 def data = songs.find {s -> s.station == thisSong}
124 log.info "Found ${data?.station}"
126 state.selectedSong = data
127 log.debug "Selected song = $state.selectedSong"
129 else if (song == state.selectedSong?.station) {
130 log.debug "Selected existing entry '$song', which is no longer in the last 20 list"
133 log.warn "Selected song '$song' not found"
136 catch (Throwable t) {
142 log.debug "Installed with settings: ${settings}"
148 log.debug "Updated with settings: ${settings}"
156 log.trace "Entering initialize()"
159 state.steps = jawbone.currentValue("steps").toInteger()
160 state.goal = jawbone.currentValue("goal").toInteger()
162 subscribe (jawbone,"goal",goalHandler)
163 subscribe (jawbone,"steps",stepHandler)
169 log.trace "Exiting initialize()"
172 def goalHandler(evt) {
174 log.trace "Entering goalHandler()"
176 def goal = evt.value.toInteger()
180 log.trace "Exiting goalHandler()"
183 def stepHandler(evt) {
185 log.trace "Entering stepHandler()"
187 log.debug "Event Value ${evt.value}"
188 log.debug "state.steps = ${state.steps}"
189 log.debug "state.goal = ${state.goal}"
191 def steps = evt.value.toInteger()
193 state.lastSteps = state.steps
197 if (settings.thresholdType == "Goal")
198 stepGoal = state.goal
200 stepGoal = settings.threshold
202 if ((state.lastSteps < stepGoal) && (state.steps >= stepGoal)) { // only trigger when crossing through the goal threshold
204 // goal achieved for the day! Yay! Lets tell someone!
206 if (settings.notificationType != "None") { // Push or SMS Notification requested
208 if (location.contactBookEnabled) {
209 sendNotificationToContacts(stepMessage, recipients)
214 method: settings.notificationType.toLowerCase(),
215 phone: settings.phone
218 sendNotification(stepMessage, options)
222 if (settings.sonos) { // play a song on the Sonos as requested
224 // runIn(1, sonosNotification, [overwrite: false])
229 if (settings.hues) { // change the color of hue bulbs ras equested
231 // runIn(1, hueNotification, [overwrite: false])
236 if (settings.lights) { // flash the lights as requested
238 // runIn(1, lightsNotification, [overwrite: false])
245 log.trace "Exiting stepHandler()"
250 def lightsNotification() {
252 // save the current state of the lights
254 log.trace "Save current state of lights"
256 state.previousLights = [:]
259 state.previousLights[it.id] = it.currentValue("switch")
262 // Flash the light on and off 5 times for now - this could be configurable
264 log.trace "Now flash the lights"
266 for (i in 1..flashCount) {
274 // restore the original state
276 log.trace "Now restore the original state of lights"
279 it."${state.previousLights[it.id]}"()
285 def hueNotification() {
287 log.trace "Entering hueNotification()"
292 else if(color == "Green")
294 else if(color == "Yellow")
296 else if(color == "Orange")
298 else if(color == "Purple")
300 else if(color == "Pink")
304 state.previousHue = [:]
307 state.previousHue[it.id] = [
308 "switch": it.currentValue("switch"),
309 "level" : it.currentValue("level"),
310 "hue": it.currentValue("hue"),
311 "saturation": it.currentValue("saturation")
315 log.debug "current values = ${state.previousHue}"
317 def newValue = [hue: hueColor, saturation: 100, level: (lightLevel as Integer) ?: 100]
318 log.debug "new value = $newValue"
320 hues*.setColor(newValue)
323 log.trace "Exiting hueNotification()"
329 log.debug "runIn ${duration}, resetHue"
330 runIn(duration, resetHue, [overwrite: false])
336 log.trace "Entering resetHue()"
338 it.setColor(state.previousHue[it.id])
340 log.trace "Exiting resetHue()"
343 def sonosNotification() {
345 log.trace "sonosNotification()"
349 if (settings.resumePlaying) {
351 sonos.playTrackAndResume(state.selectedSong, settings.songDuration, settings.volume)
353 sonos.playTrackAndResume(state.selectedSong, settings.songDuration)
356 sonos.playTrackAtVolume(state.selectedSong, settings.volume)
358 sonos.playTrack(state.selectedSong)
361 sonos.on() // make sure it is playing
365 log.trace "Exiting sonosNotification()"