-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetYesterdaysUsage.py
executable file
·53 lines (39 loc) · 1.62 KB
/
getYesterdaysUsage.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
#!/usr/bin/python
# Be careful running multiple times a day if you are running near midnight.
# if you are the clock and thermostat are not in sync we could get data that is not from the day we are
# expecting
from TStat import *
import sqlite3
from datetime import datetime, timedelta
import time
import os.path
def touch(fname, times=None):
with file(fname,'a'):
os.utime(fname,times)
# get last succesful run time
# we will touch this file if we succesfully run
# if file has not yet been touched in the current day,
# we will get the previous days usage
FILE = 'getYesterdaysUsage'
getUsage = False
if not os.path.exists(FILE):
open(FILE,'w').close()
getUsage=True # getUsage if we have to create the file
# get last time FILE was modified
lastSuccess = time.ctime(os.path.getmtime(FILE))
dateLastSuccess = datetime.strptime(lastSuccess,'%a %b %d %H:%M:%S %Y')
#If file was modified on a day that is not today, update usage stats
if dateLastSuccess.day != datetime.now().day:
getUsage = True
if getUsage == True:
t = TStat('thermostat-FD-BB-6F.chiefmarley.local')
heatUsage = t.getHeatUsageYesterday()
coolUsage = t.getCoolUsageYesterday()
heatMinutes = heatUsage['hour'] * 60 + heatUsage['minute']
coolMinutes = coolUsage['hour'] * 60 + coolUsage['minute']
currentTime = int(round(time.time()))
touch(FILE)
conn = sqlite3.connect('/home/andy/djangoProjects/leeHouseSite/sqlite/db.sql3')
c = conn.cursor()
c.execute("insert into restInterface_hvac_runtime values (NULL," + str(currentTime) + "," + str(heatMinutes) +"," + str(coolMinutes) + ")")
conn.commit()