Skip to content

Commit

Permalink
Adjust thermostat setpoint by difference between avg room temperature
Browse files Browse the repository at this point in the history
and current thermostat reading. Move some logic inside the read()
function to make it synchronous.
  • Loading branch information
gtfierro committed Aug 14, 2014
1 parent 5a894a1 commit bc8b69c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
2 changes: 1 addition & 1 deletion smap-control-tutorial/schedulerZoneDemo.ini
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Metadata/HVACZone = Zone1
rate = 10
heatSPwhere = Path="/scheduler/heatSetpoint" and Metadata/Site/id = "2c8ed966-146a-11e4-9e46-000c29b778da"
coolSPwhere = Path="/scheduler/coolSetpoint" and Metadata/Site/id = "2c8ed966-146a-11e4-9e46-000c29b778da"
thermwhere = Path = "/building/thermostat1" and Metadata/Site/id = "2c8ed966-146a-11e4-9e46-000c29b778da"
thermwhere = Path = "/buildinghvac/thermostat1/temp" and Metadata/Site/id = "2c8ed966-146a-11e4-9e46-000c29b778da"
tempwhere = Metadata/HVACZone = "Zone1" and Metadata/Site/id = "2c8ed966-146a-11e4-9e46-000c29b778da" and Metadata/Type = "Sensor" and Metadata/Kind = "Temperature" and Metadata/Form = "Air"


Expand Down
33 changes: 27 additions & 6 deletions smap-control-tutorial/zoneControllerService.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@
from smap import actuate, driver
from smap.util import periodicSequentialCall
from smap.archiver.client import RepublishClient
from smap.archiver.client import SmapClient
import time
from pprint import pprint
from smap.contrib import dtutil

class ZoneController(driver.SmapDriver):
def setup(self, opts):
Expand All @@ -41,11 +44,13 @@ def setup(self, opts):
self.heatSP=int(opts.get('defaultHeatSetpoint',68))
self.coolSP=int(opts.get('defaultCoolSetpoint',76))

self.therm_temp = 70

self.trim = int(opts.get('trim',0)) # dummy zoneCtrl action

# create timeseries for zone controller actions
heatSetPoint = self.add_timeseries('/heatSetpoint', 'F', data_type='long')
coolSetPoint = self.add_timeseries('/coolSetpoint', 'F', data_type='long')
heatSetPoint = self.add_timeseries('/heatSetpoint', 'F', data_type='double')
coolSetPoint = self.add_timeseries('/coolSetpoint', 'F', data_type='double')
# add actuators to them
heatSetPoint.add_actuator(setpointActuator(controller=self, range=(40,90)))
coolSetPoint.add_actuator(setpointActuator(controller=self, range=(40,90)))
Expand All @@ -62,24 +67,39 @@ def setup(self, opts):
print "ZoneController: thermostat where = ", self.thermwhere
print "ZoneController: temp sensor where = ", self.tempwhere

self.client = SmapClient(self.archiver_url)

self.heatSPclient = RepublishClient(self.archiver_url, self.heatSPcb, restrict=self.heatSPwhere)
self.coolSPclient = RepublishClient(self.archiver_url, self.coolSPcb, restrict=self.coolSPwhere)
self.tempclient = RepublishClient(self.archiver_url, self.tempcb, restrict=self.tempwhere)
#self.tempclient = RepublishClient(self.archiver_url, self.tempcb, restrict=self.tempwhere)
self.thermclient = RepublishClient(self.archiver_url, self.thermcb, restrict=self.thermwhere)


def start(self):
print "zone controller start: ", self.rate
self.heatSPclient.connect() # activate subscription scheduler setpoints
self.coolSPclient.connect()
self.tempclient.connect()
#self.tempclient.connect()
self.thermclient.connect()
periodicSequentialCall(self.read).start(self.rate)

def read(self):
all_readings = self.client.latest(self.tempwhere)
for p in all_readings:
print '-'*20
md = self.client.tags('uuid = "'+p['uuid']+'"')[0]
print 'Room:', md['Metadata/Room']
print 'Reading:', p['Readings'][0][1]
ts = dtutil.ts2dt(p['Readings'][0][0]/1000)
print 'Time:', dtutil.strftime_tz(ts, tzstr='America/Los_Angeles')
avg_room_temp = sum([x['Readings'][0][1] for x in all_readings]) / float(len(all_readings))

# get difference between avg room temperature and thermostat temperature
new_diff = self.therm_temp - avg_room_temp

# periodically update output streams. Here a bogus adjustment
self.add('/heatSetpoint', self.heatSP - self.trim)
self.add('/coolSetpoint', self.coolSP + self.trim)
self.add('/heatSetpoint', self.heatSP + new_diff)
self.add('/coolSetpoint', self.coolSP + new_diff)
print "zone controller publish: ", self.heatSP, self.coolSP

# Event handler for publication to heatSP stream
Expand All @@ -103,6 +123,7 @@ def tempcb(self, _, data):
def thermcb(self, _, data):
# list of arrays of [time, val]
print "ZoneController thermcb: ", data
self.therm_temp = data[-1][-1][1]



Expand Down

0 comments on commit bc8b69c

Please sign in to comment.