-
Notifications
You must be signed in to change notification settings - Fork 9
/
teslasmartheater.groovy
69 lines (58 loc) · 2.6 KB
/
teslasmartheater.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
definition(
name: "Tesla Smart Heater",
namespace: "ask4",
author: "JB",
description: "Tesla Smart Heater",
category: "Convenience",
iconUrl: "https://s3.amazonaws.com/smartapp-icons/Partner/tesla-app%402x.png",
iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Partner/tesla-app%403x.png")
preferences {
section {
input "HVACswitch", "capability.Switch", title: "Select the Tesla HVAC switch", required: true, multiple: false
input "presencesensor", "capability.presenceSensor", title: "Select the presence sensor", required: true, multiple: false
input "temperaturesensor", "capability.temperatureMeasurement", title: "Select the temperature sensor", required: true, multiple: false
input "targettime", "time", title: "Time to execute every (week) day"
input "minimumtemperature", "number", title: "Enter minimum temperature", defaultValue:10
input "days", "enum", title: "Select Days of the Week", required: true, multiple: true, options: ["Monday": "Monday", "Tuesday": "Tuesday", "Wednesday": "Wednesday", "Thursday": "Thursday", "Friday": "Friday", "Saturday": "Saturday", "Sunday": "Sunday"]
}
}
def installed() {
log.debug "Installed with settings: ${settings}"
initialize()
}
def updated() {
log.debug "Updated with settings: ${settings}"
unsubscribe()
initialize()
}
def initialize() {
schedule(targettime, eventHandler)
}
def eventHandler() {
log.debug "event handler"
log.debug temperaturesensor.currentValue("temperature")
log.debug presencesensor.currentValue("presence")
//If it is a target day and temperature less than or equal to target temperature and Tesla present then turn on heater
def df = new java.text.SimpleDateFormat("EEEE")
// Ensure the new date object is set to local time zone
df.setTimeZone(location.timeZone)
def day = df.format(new Date())
//Does the preference input Days, i.e., days-of-week, contain today?
def dayCheck = days.contains(day)
if (dayCheck) {
log.debug "Today is one of the target days"
if (temperaturesensor.currentValue("temperature") <= minimumtemperature){
log.debug "Temperature less than minimum"
if (presencesensor.currentValue("presence") == "present"){
log.debug "Car is present"
log.debug "Turn on the HVAC"
HVACswitch.on()
} else
log.debug "Car not present"
} else {
log.debug "Temperature more than minimum"
}
} else {
log.debug "Today is not one of the target days. Nothing to do."
}
}