4 * Copyright 2014 Christopher Boerma
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: "Goodnight Ubi",
20 description: "An app to coordinate bedtime activities between Ubi and SmartThings. This app will activate when a Virtual Tile is triggers (Setup custom behavior in Ubi to turn on this tile when you say goodnight to ubi). This app will then turn off selected lights after a specified number of minutes. It will also check if any doors or windows are open. If they are, Ubi will tell you which ones are open. Finally, the app will say goodnight to hello home if requested.",
21 category: "Safety & Security",
22 iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
23 iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png")
27 section("Enter Ubi information:") {
28 input "behaviorToken", "text", title: "What is the Ubi Token?", required: true, autoCorrect:false
29 // Get token from the Ubi Portal. Select HTTP request as trigger and token will be displayed.
30 input "trigger", "capability.switch", title: "Which virtual tile is the trigger?", required: true
31 // Create a Virtual on/off button tile for this.
34 section("Which doors and windows should I check?"){
35 input "doors", "capability.contactSensor", multiple: true
38 section("Which light switches will I be turning off?") {
39 input "theSwitches", "capability.switch", Title: "Which?", multiple: true, required: false
40 input "minutes", "number", Title: "After how many minutes?", required: true
42 section("Should I say 'Goodnight' to Hello Home?") {
43 input "sayPhrase", "enum", metadata:[values:["Yes","No"]]
49 log.debug "Installed with settings: ${settings}"
55 log.debug "Updated with settings: ${settings}"
62 subscribe(trigger, "switch.on", switchOnHandler) // User should set up on Ubi that when the choosen
63 } // trigger is said, Ubi turns on this virtual switch.
65 def switchOnHandler(evt) {
66 log.debug "trigger turned on!"
68 def timeDelay = minutes * 60 // convert minutes to seconds.
69 runIn (timeDelay, lightsOut) // schedule the lights out procedure
71 def phrase = "" // Make sure Phrase is empty at the start of each run.
73 doors.each { doorOpen -> // cycles through all contact sensor devices selected
74 if (doorOpen.currentContact == "open") { // if the current selected device is open, then:
75 log.debug "$doorOpen.displayName" // echo to the simulator the device's name
76 def toReplace = doorOpen.displayName // make variable 'toReplace' = the devices name.
77 def replaced = toReplace.replaceAll(' ', '%20') // make variable 'replaced' = 'toReplace' with all the space changed to %20
78 log.debug replaced // echo to the simulator the new name.
80 phrase = phrase.replaceAll('%20And%20', '%20') // Remove any previously added "and's" to make it sound natural.
82 if (phrase == "") { // If Phrase is empty (ie, this is the first name to be added)...
83 phrase = "The%20" + replaced // ...then add "The%20" plus the device name.
84 } else { // If Phrase isn't empty...
85 phrase = phrase + ",%20And%20The%20" + replaced // ...then add ",%20And%20The%20".
88 log.debug phrase // Echo the current version of 'Phrase'
89 } // Closes the IF statement.
90 } // Closes the doors.each cycle
93 phrase = "The%20house%20is%20ready%20for%20night."
96 phrase = "You%20have%20left%20" + phrase + "open"
99 httpGet("https://portal.theubi.com/webapi/behaviour?access_token=${behaviorToken}&variable=${phrase}")
100 // send the http request and push the device name (replaced) as the variable.
101 // On the Ubi side you need to setup a custom behavior (which you've already done to get the token)
102 // and have say something like: "Hold on! The ${variable} is open!" Ubi will then take 'replaced'
103 // from this http request and insert it into the phrase that it says.
105 if (sayPhrase == "Yes") { // If the user selected to say Goodnight...
106 location.helloHome.execute("Good Night!") // ...say goodnight to Hello Home.
108 } // Close the switchOnHandler Process
111 log.debug "Turning off trigger"
112 trigger.off() // Turn off the trigger tile button for next run
113 if (theSwitches == "") {} else { // If the user didn't enter any light to turn off, do nothing...
114 log.debug "Turning off switches" // ...but if the user did enter lights, then turn them
115 theSwitches.off() // off here.