-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathThermostatLogger.groovy
More file actions
108 lines (89 loc) · 3.03 KB
/
ThermostatLogger.groovy
File metadata and controls
108 lines (89 loc) · 3.03 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/**
* ThermostatLogger. Logs thermostat temperature and operating state to Xively feed.
* See http://build.smartthings.com/projects/xively/ for more information.
*
* Author: @kernelhack
* Date: 2014-01-21
*/
// Automatically generated. Make future change here.
definition(
name: "Log to Xively",
namespace: "docwisdom",
author: "Brian Critchlow",
description: "Logging thermostat to Xively",
category: "Green Living",
iconUrl: "https://mbed.org/media/thumbs/f5/1b/f51b2107e58df9cffd95cfdec1a9bdec.jpg",
iconX2Url: "https://mbed.org/media/thumbs/f5/1b/f51b2107e58df9cffd95cfdec1a9bdec.jpg")
preferences {
section("Configure") {
input "xi_apikey", "text", title: "Xively API Key"
input "xi_feed", "number", title: "Xively Feed ID"
input "thermostat1", "capability.thermostat", multiple: true, title: "Select thermostats"
}
}
def installed() {
log.debug "Installed with settings: ${settings}"
initialize()
}
def updated() {
log.debug "Updated with settings: ${settings}"
unsubscribe()
initialize()
}
def initialize() {
// Subscribe to attributes, devices, locations, etc.
subscribe(thermostat1, "temperature", handleThermostatTemperature)
subscribe(thermostat1, "thermostatOperatingState", handleThermostatOperatingState)
subscribe(location, "mode", handlePeriodic)
handlePeriodic()
}
// read device info every hour, push data
def handlePeriodic() {
log.debug "polling thermostats"
thermostat1.each {
writeChannelData(xi_feed, "Temperature_" + it.displayName, it.currentValue("temperature"))
def mode = it.currentValue("thermostatMode")
def opState = 0
if (mode == "heating") {
opState = 1
} else if (mode == "cooling") {
opState = -1
}
writeChannelData(xi_feed, "OperatingState_" + it.displayName, opState)
}
// run again in 10 min
runIn(60*10, handlePeriodic)
}
def parseHttpResponse(response) {
log.debug "HTTP Response: ${response}"
}
def writeChannelData(feed, channel, value) {
channel = channel.replaceAll(' ', '')
def uri = "https://api.xively.com/v2/feeds/${feed}.json"
def json = "{\"version\":\"1.0.0\",\"datastreams\":[{\"id\":\"${channel}\",\"current_value\":\"${value}\"}]}"
def headers = [
"X-ApiKey" : "${xi_apikey}"
]
def params = [
uri: uri,
headers: headers,
body: json
]
httpPutJson(params) {response -> parseHttpResponse(response)}
}
// Handle temperature event
def handleThermostatTemperature(evt) {
log.debug "Tempreature event: $evt.value"
writeChannelData(xi_feed, "Temperature_" + evt.device.displayName, evt.value)
}
// Handle thermostatOperatingState event
def handleThermostatOperatingState(evt) {
log.debug "OperatingState event: $evt.value"
def opState = 0
if (evt.value == "heating") {
opState = 1
} else if (evt.value == "cooling") {
opState = -1
}
writeChannelData(xi_feed, "OperatingState_" + evt.device.displayName, opState)
}