Update influxdb-logger.groovy
[smartapps.git] / official / hall-light-welcome-home.groovy
1 /**
2  *  Hall Light: Welcome Home
3  *
4  *  Author: brian@bevey.org
5  *  Date: 9/25/13
6  *
7  *  Turn on the hall light if someone comes home (presence) and the door opens.
8  */
9
10 definition(
11   name: "Hall Light: Welcome Home",
12   namespace: "imbrianj",
13   author: "brian@bevey.org",
14   description: "Turn on the hall light if someone comes home (presence) and the door opens.",
15   category: "Convenience",
16   iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
17   iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience%402x.png"
18 )
19
20 preferences {
21   section("People to watch for?") {
22     input "people", "capability.presenceSensor", multiple: true
23   }
24
25   section("Front Door?") {
26     input "sensors", "capability.contactSensor", multiple: true
27   }
28
29   section("Hall Light?") {
30     input "lights", "capability.switch", title: "Switch Turned On", multilple: true
31   }
32
33   section("Presence Delay (defaults to 30s)?") {
34     input name: "presenceDelay", type: "number", title: "How Long?", required: false
35   }
36
37   section("Door Contact Delay (defaults to 10s)?") {
38     input name: "contactDelay", type: "number", title: "How Long?", required: false
39   }
40 }
41
42 def installed() {
43   init()
44 }
45
46 def updated() {
47   unsubscribe()
48   init()
49 }
50
51 def init() {
52   state.lastClosed = now()
53   subscribe(people, "presence.present", presence)
54   subscribe(sensors, "contact.open", doorOpened)
55 }
56
57 def presence(evt) {
58   def delay = contactDelay ?: 10
59
60   state.lastPresence = now()
61
62   if(now() - (delay * 1000) < state.lastContact) {
63     log.info('Presence was delayed, but you probably still want the light on.')
64     lights?.on()
65   }
66 }
67
68 def doorOpened(evt) {
69   def delay = presenceDelay ?: 30
70
71   state.lastContact = now()
72
73   if(now() - (delay * 1000) < state.lastPresence) {
74     log.info('Welcome home!  Let me get that light for you.')
75     lights?.on()
76   }
77 }