-
Notifications
You must be signed in to change notification settings - Fork 121
/
Aeon HEM - Xively.app.groovy
168 lines (140 loc) · 5.66 KB
/
Aeon HEM - Xively.app.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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/**
* Aeon HEM - Xively
*
* Copyright 2014 Dan Anghelescu
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*
*
*
* Genesys: Based off of Aeon Smart Meter Code sample provided by SmartThings (2013-05-30), Aeon Home Energy Meter v2 by Barry A. Burke, and Xively Logger by Patrick Stuart Built on US model
* may also work on international versions (currently reports total values only)
*/
// Automatically generated. Make future change here.
definition (
name: "Aeon HEM - Xively",
namespace: "smartthings",
author: "Dan Anghelescu",
description: "Aeon HEM - Xively Logger",
category: "My Apps",
iconUrl: "https://graph.api.smartthings.com/api/devices/icons/st.Electronics.electronics13-icn?displaySize",
iconX2Url: "https://graph.api.smartthings.com/api/devices/icons/st.Electronics.electronics13-icn?displaySize=2x")
preferences {
section("Log devices...") {
input "energymeters", "capability.EnergyMeter", title: "Energy Meter", required: false, multiple: true
input "thermostats", "capability.thermostat", title: "Thermostat", required: false, multiple: true
input "weatherstations", "capability.temperatureMeasurement", title: "Outside Temperature", required: false, multiple: true
}
section ("Xively Info") {
input "xi_apikey", "text", title: "Xively API Key"
input "xi_feed", "number", title: "Xively Feed ID"
}
}
def installed() {
initialize()
}
def updated() {
unsubscribe()
initialize()
}
def initialize() {
state.clear()
unschedule(checkSensors)
schedule("0 */5 * * * ?", "checkSensors")
subscribe(app, appTouch)
}
def appTouch(evt) {
log.debug "appTouch: $evt"
checkSensors()
}
def checkSensors() {
def logitems = []
for (t in settings.energymeters) {
logitems.add([t.displayName, "energymeter.energy", t.latestValue("energy"), "KilowattHours", "kWh"] )
state[t.displayName + ".energy"] = t.latestValue("energy")
}
for (t in settings.thermostats) {
logitems.add([t.displayName, "inside.temperature", t.latestValue("temperature"), "Farenheight", "°F"] )
state[t.displayName + ".temperature"] = t.latestValue("temperature")
/*
if ( t.currentValue("thermostatOperatingState") == "cooling") {
logitems.add([t.displayName, "thermostatState", 1 ])
state[t.displayName + ".thermostatOperatingState"] = 1
}
if ( t.currentValue("thermostatOperatingState") == "heating") {
logitems.add([t.displayName, "thermostatState", 2 ])
state[t.displayName + ".thermostatOperatingState"] = 2
}
if ( t.currentValue("thermostatOperatingState") == "idle") {
logitems.add([t.displayName, "thermostatState", 0 ])
state[t.displayName + ".thermostatOperatingState"] = 0
}
*/
}
for (t in settings.energymeters) {
logitems.add([t.displayName, "energymeter.power", t.latestValue("power"), "Watts", "W"] )
state[t.displayName + ".power"] = t.latestValue("power")
}
for (t in settings.energymeters) {
logitems.add([t.displayName, "energymeter.volts", t.latestValue("volts"), "Volts", "V"] )
state[t.displayName + ".volts"] = t.latestValue("volts")
}
for (t in settings.energymeters) {
logitems.add([t.displayName, "energymeter.amps", t.latestValue("amps"), "Amps", "A"] )
state[t.displayName + ".amps"] = t.latestValue("amps")
}
for (t in settings.weatherstations) {
logitems.add([t.displayName, "outside.temperature", t.latestValue("temperature"), "Farenheight", "°F"] )
state[t.displayName + ".temperature"] = t.latestValue("temperature")
}
logField2(logitems)
}
private getFieldMap(channelInfo) {
def fieldMap = [:]
channelInfo?.findAll { it.key?.startsWith("field") }.each { fieldMap[it.value?.trim()] = it.key }
return fieldMap
}
private logField2(logItems) {
def fieldvalues = ""
log.debug logItems
def xivelyinfo = ""
logItems.eachWithIndex() { item, i ->
def channelname = item[0].replace(" ","_") + "_" + item[1]
xivelyinfo += "{\"id\":\"${channelname}\",\"current_value\":\"${item[2]}\",\"unit\":{\"label\":\"${item[3]}\",\"symbol\":\"${item[4]}\"}}"
if (i.toInteger() + 1 < logItems.size())
{
xivelyinfo += ","
}
}
log.debug xivelyinfo
def uri = "https://api.xively.com/v2/feeds/${xi_feed}.json"
def json = "{\"version\":\"1.0.0\",\"datastreams\":[${xivelyinfo} ]}"
def headers = [
"X-ApiKey" : "${xi_apikey}"
]
def params = [
uri: uri,
headers: headers,
body: json
]
log.debug params.body
httpPutJson(params) {response -> parseHttpResponse(response)}
}
def parseHttpResponse(response) {
log.debug "HTTP Response: ${response}"
}
def captureState(theDevice) {
def deviceAttrValue = [:]
for ( attr in theDevice.supportedAttributes ) {
def attrName = "${attr}"
def attrValue = theDevice.currentValue(attrName)
deviceAttrValue[attrName] = attrValue
}
return deviceAttrValue
}