Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# maximum allowable delta (°F) before we start the fans
TEMP_DELTA = 5.0
def check_temp():
global VENTILATION_STATE
c = db.cursor()
# get the last ten minutes of data only
c.execute("SELECT temp, room FROM temps WHERE (julianday('now') - julianday(timestamp))*86400 < 600 ORDER BY timestamp")
inside_temps, outside = [], 0.0
for temp, room in c.fetchall():
if room == 'outside':
outside = temp
elif room == 'inside':
inside_temps.append(temp)
# average all inside temps seen
inside = sum(inside_temps)/len(inside_temps)
if inside - TEMP_DELTA > outside:
# it's hot inside. let's cool it off
if VENTILATION_STATE != 'on':
print("Cooler outside (%f) than inside (%f). Turning on ventilation" % (outside, inside))
VENTILATION_STATE == 'on'
mqtt_client.publish('ventilate', payload='on')
elif inside <= outside:
# we're already cooler than outside
if VENTILATION_STATE == 'on':
# turn off ventilation
print("Hotter outside (%f) than inside (%f). Turning off ventilation" % (outside, inside))
VENTILATION_STATE == 'off'
mqtt_client.publish('ventilate', payload='off')
else:
print("warmer outside, but not terrible")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.