Update step-notifier.groovy
[smartapps.git] / official / smart-turn-it-on.groovy
1 /**
2  *  Smart turn it on
3  *
4  *  Author: sidjohn1@gmail.com
5  *  Date: 2013-10-21
6  */
7
8 // Automatically generated. Make future change here.
9 definition(
10     name: "Smart turn it on",
11     namespace: "sidjohn1",
12     author: "sidjohn1@gmail.com",
13     description: "Turns on selected device(s) at a set time on selected days of the week only if a selected person is present and turns off selected device(s) after a set time.",
14     category: "Convenience",
15     iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
16     iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience%402x.png"
17 )
18
19 preferences {
20         section("Turn on which device?"){
21                 input "switchOne", "capability.switch",title:"Select Light", required: true, multiple: true
22         }
23     section("For Whom?") {
24                 input "presenceOne", "capability.presenceSensor", title: "Select Person", required: true, multiple: true
25         }
26     section("On which Days?") {
27                 input "dayOne", "enum", title:"Select Days", required: true, multiple:true, metadata: [values: ['Mon','Tue','Wed','Thu','Fri','Sat','Sun']]
28         }
29         section("At what time?") {
30                 input name: "timeOne", title: "Select Time", type: "time", required: true
31         }
32         section("For how long?") {
33                 input name: "timeTwo", title: "Number of minutes", type: "number", required: true
34         }
35 }
36
37 def installed() {
38         if (timeOne)
39         {
40                 log.debug "scheduling 'Smart turn it on' to run at $timeOne"
41                 schedule(timeOne, "turnOn")
42         }
43 }
44
45 def updated() {
46         unsubscribe()
47         unschedule()
48         if (timeOne)
49         {
50                 log.debug "scheduling 'Smart turn it on' to run at $timeOne"
51                 schedule(timeOne, "turnOn")
52         }
53 }
54
55 def turnOn(){
56 log.debug "Start"
57         def dayCheck = dayOne.contains(new Date().format("EEE"))
58     def dayTwo = new Date().format("EEE");
59         if(dayCheck){
60         def presenceTwo = presenceOne.latestValue("presence").contains("present")
61                 if (presenceTwo) {
62                 switchOne.on()
63                         def delay = timeTwo * 60
64                         runIn(delay, "turnOff")
65                 }   
66     }
67 }
68
69
70     
71 def turnOff() {
72         switchOne.off()
73 }