-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathecobee_influxdb.py
executable file
·276 lines (227 loc) · 11.1 KB
/
ecobee_influxdb.py
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/bin/python3.6
# *************************************
#
# script to pull data from the Ecobee API and write to influxDB
# useful for making visualizations with Grafana, etc
#
#
# for now, you need to go to https://www.ecobee.com/home/developer/api/examples/ex1.shtml
# and follow the instructions to generate an API key, authorize the app with a PIN, and finally
# get a "refresh code" The refresh code needs to be written to file ~/.ecobee_refresh_token on the first line
#
# you also need to enter your API key in the variables below
#
# **************************************
from influxdb import InfluxDBClient
import datetime
import requests
import json
import pytz
import sys
from pathlib import Path
import logging
import logging.handlers
#setup logging
log_file_path = 'ecobee.log'
days_of_logs_to_keep = 7
# set to DEBUG, INFO, ERROR, etc
logging_level = 'DEBUG'
#ecobee API Key
APIKey = "YOUR_API_KEY"
#influxDB info
influxdb_server = '192.168.1.2'
influxdb_port = 8086
influxdb_database = 'ecobee'
#runtime report time since last report query
runtime_difference = 60
#setup logger
logger = logging.getLogger('ecobee')
logger.setLevel(getattr(logging, logging_level))
handler = logging.handlers.TimedRotatingFileHandler(log_file_path, when="d", interval=1, backupCount=days_of_logs_to_keep)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
def to_bool(value):
valid = {'true': True, 't': True, '1': True,
'false': False, 'f': False, '0': False,
}
if isinstance(value, bool):
return value
lower_value = value.lower()
if lower_value in valid:
return valid[lower_value]
else:
raise ValueError('invalid literal for boolean: "%s"' % value)
def api_request(url, method, header=''):
try:
if method == 'post':
#post to url
return requests.post(url).json()
if method == 'get':
#get method
return requests.get(url, headers=headers).json()
except:
logger.critical("error connecting to " + url)
sys.exit()
#access token needs to be refreshed at least every hour
# get refresh code from file
token_file = str(Path.home()) + '/.ecobee_refresh_token'
with open(token_file) as f:
refreshToken = f.readline().replace("\n","")
token_url = "https://api.ecobee.com/token?grant_type=refresh_token&code=" + refreshToken + "&client_id=" + APIKey
r = api_request(token_url, 'post')
access_token = r['access_token']
new_refresh_token = r['refresh_token']
with open(token_file, 'w') as f:
f.write(new_refresh_token)
logger.debug("old refresh token = " + refreshToken)
logger.debug("access token = " + access_token)
logger.debug("new refresh token = " + new_refresh_token)
def logPoint(sensorName=None, thermostatName=None, sensorValue=None, sensorType=None):
return {
"measurement": sensorType,
"tags": {
"thermostat_name": thermostatName,
"sensor": sensorName
},
"fields": {
"value": sensorValue
}
}
client = InfluxDBClient(host=influxdb_server,
port=influxdb_port,
database=influxdb_database,
verify_ssl=False)
points = []
payload = {
"selection": {
"selectionType": "registered",
"selectionMatch": "",
"includeRuntime": True,
"includeEquipmentStatus": True,
"includeWeather": True,
"includeSensors": True,
"includeExtendedRuntime": True,
"includeDevice": True,
"includeEvents": True,
"includeProgram": True
}
}
payload = json.dumps(payload)
url = 'https://api.ecobee.com/1/thermostat?format=json&body=' + payload
headers = {'content-type': 'text/json', 'Authorization': 'Bearer ' + access_token}
response = api_request(url, 'get', headers)
point_count = 0
for thermostat in response['thermostatList']:
thermostatName = thermostat['name']
sensors = thermostat['remoteSensors']
current_weather = thermostat['weather']['forecasts'][0]
current_program = thermostat['program']['currentClimateRef']
if len(thermostat['events']) > 0:
current_program = thermostat['events'][0]['name']
for sensor in sensors:
for capability in sensor['capability']:
if capability['type'] == 'occupancy':
value = bool(to_bool(capability['value']))
points.append(logPoint(sensorName=sensor['name'], thermostatName=str(thermostatName), sensorValue=bool(value), sensorType="occupancy"))
if capability['type'] == 'temperature':
if str.isdigit(capability['value']) > 0:
temp = int(capability['value']) / 10
else:
temp = 0
points.append(logPoint(sensorName=sensor['name'], thermostatName=str(thermostatName), sensorValue=float(temp), sensorType="temp"))
if capability['type'] == 'humidity':
points.append(logPoint(sensorName=sensor['name'], thermostatName=str(thermostatName), sensorValue=float(capability['value']), sensorType="humidity"))
runtime = thermostat['runtime']
temp = int(runtime['actualTemperature']) / 10
heatTemp = int(runtime['desiredHeat']) / 10
coolTemp = int(runtime['desiredCool']) / 10
outside_temp = current_weather['temperature'] / 10
outside_wind = current_weather['windSpeed']
outside_humidity = current_weather['relativeHumidity']
points.append(logPoint(sensorName=thermostatName, thermostatName=str(thermostatName), sensorValue=float(temp), sensorType="actualTemperature"))
points.append(logPoint(sensorName=thermostatName, thermostatName=str(thermostatName), sensorValue=float(runtime['actualHumidity']), sensorType="actualHumidity"))
points.append(logPoint(sensorName=thermostatName, thermostatName=str(thermostatName), sensorValue=float(heatTemp), sensorType="desiredHeat"))
points.append(logPoint(sensorName=thermostatName, thermostatName=str(thermostatName), sensorValue=float(coolTemp), sensorType="desiredCool"))
points.append(logPoint(sensorName=thermostatName, thermostatName=str(thermostatName), sensorValue=float(outside_temp), sensorType="outsideTemp"))
points.append(logPoint(sensorName=thermostatName, thermostatName=str(thermostatName), sensorValue=float(outside_wind), sensorType="outsideWind"))
points.append(logPoint(sensorName=thermostatName, thermostatName=str(thermostatName), sensorValue=float(outside_humidity), sensorType="outsideHumidity"))
points.append(logPoint(sensorName=thermostatName, thermostatName=str(thermostatName), sensorValue=str(current_program), sensorType="currentProgram"))
point_count += 1
client.write_points(points)
logger.info("sensor readings written: " + str(point_count))
#get historical runtime data
#clear points
points = []
#redefine logPoint
def logPoint(sensorName=None, sensorValue=None, sensorType=None,recordedTime=None):
return {
"measurement": sensorType,
"time": recordedTime,
"tags": {
"sensor": sensorName
},
"fields": {
"value": sensorValue
}
}
#set start date for runtime report to yesterday to cover querying right after midnight
#set end date to today to get most recent data
end_date = datetime.date.today()
start_date = end_date - datetime.timedelta(days=1)
for thermostat in response['thermostatList']:
thermostatName = thermostat['name']
#we only want to get runtime report every so often as it only updates about every 15-30 minutes
#set variable "runtime_difference", recommend no quicker than 45 minute
# ex. if last data recorded is from 10:05, and data isn't updated until 10:35, then needs to be processed on
# the server it doesn't help to query the data again until ~10:45
query = client.query("SELECT * FROM fantime WHERE sensor='" + thermostatName + "' ORDER BY DESC LIMIT 1")
query_response = list(query.get_points())
response_time_stamp = datetime.datetime.strptime(query_response[0]['time'], '%Y-%m-%dT%H:%M:%SZ')
difference_minutes = (datetime.datetime.now() - response_time_stamp).seconds / 60
logger.info("last runtime report timestamp from query for " + thermostatName + ": " + response_time_stamp.strftime("%Y-%m-%d %H:%M:%S"))
logger.debug("difference in minutes: " + str(difference_minutes))
if runtime_difference < difference_minutes:
point_count = 0
payload = {
"startDate": start_date.strftime('%Y-%m-%d'),
"endDate": end_date.strftime('%Y-%m-%d'),
"columns": "auxHeat1,compCool1,fan,outdoorTemp,zoneAveTemp",
"selection": {
"selectionType": "thermostats",
"selectionMatch": thermostat['identifier']
}
}
payload = json.dumps(payload)
url = 'https://api.ecobee.com/1/runtimeReport?format=json&body=' + payload
headers = {'content-type': 'text/json', 'Authorization': 'Bearer ' + access_token}
data = api_request(url, 'get', headers)
for row in data['reportList'][0]['rowList']:
myday,mytime,auxHeat1,compCool1,fan,outdoorTemp,zoneAveTemp,eol = row.split(",")
date_str = str(myday) + " " + str(mytime)
datetime_obj = datetime.datetime.strptime(date_str, "%Y-%m-%d %H:%M:%S")
builttime = datetime_obj.strftime ("%Y-%m-%d %H:%M:%S")
if datetime_obj > response_time_stamp:
#we hit a row in the response that is newer than that last one recorded in influx
#rows are returned up until the current time, but have empty strings in the columns if there is no data
#rows with data, but with zero runtime have a 0
if auxHeat1 is not '':
points.append(logPoint(sensorName=thermostatName, sensorValue=float(auxHeat1), sensorType="heattime", recordedTime=builttime))
point_count += 1
last_recorded_time = datetime_obj
logger.debug(thermostatName + " heat ran for " + auxHeat1 + " - " + builttime)
if compCool1 is not '':
points.append(logPoint(sensorName=thermostatName, sensorValue=float(compCool1), sensorType="cooltime", recordedTime=builttime))
point_count += 1
last_recorded_time = datetime_obj
logger.debug(thermostatName + " cool ran for " + auxHeat1 + " - " + builttime)
if fan is not '':
points.append(logPoint(sensorName=thermostatName, sensorValue=float(fan), sensorType="fantime", recordedTime=builttime))
point_count += 1
last_recorded_time = datetime_obj
logger.debug(thermostatName + " fan ran for " + auxHeat1 + " - " + builttime)
logger.info(str(point_count) + " points written for runtime data from " + thermostatName)
else:
logger.info(thermostatName + " not queried as last query was less than " + str(runtime_difference) + " minutes ago")
logger.info("-----------------------------------")
client.write_points(points)