jeudi 30 novembre 2017

Programming a thermostat in Python with “if statement” logic for time scheduling

I am working on a thermostat that can be programmed to be off according to the time of day.

This is my thermostat function:

def thermostat(ac_on, acPower, temp_inside, Temp_desired):
   if temp_inside > Temp_desired:
        ac_on = True
   if temp_inside < Temp_desired:
        ac_on = False
   ac_heatflow = acPower if ac_on == True else 0
   return ac_heatflow, ac_on

Temp_desired is a set integer value, and temp_inside is the changing indoor temperature value that gets compared to Temp_desired (right now every couple of seconds) to see if the air conditioner (A/C) will be “on,” or “off.” ac_heatflow is the power usage when the A/C is on, and acPower is the A/C power value assigned outside of the function.

My issue now is that I want to now add a time component to my thermostat, where the the A/C can be programmed to be off. For example. The A/C will work normally throughout the day, but from 8:00am to 10:00am it must be off, and from 3:00pm to 5:45pm it must be off, so during these intervals ac_on = False, but outside these intervals the thermostat will revert back to the original method where it determines if the air conditioner is on/off based on the room temperature.

This is what I have been trying:

def thermostat(ac_on, acPower, temp_inside, Temp_desired, start, end, t):
    while t >= start or t <= end:
        ac_on = False 
    else:
        if temp_inside > Temp_desired + 2:
            ac_on = True
        if temp_inside < Temp_desired:
            ac_on = False
    ac_heatflow = 400 if ac_on == True else 0
    return ac_heatflow, ac_on

start is a start time of the interval, end is the interval end time and t right now is in seconds. There is a calculation of temperature every couple of seconds over a 24 hour period, so a full day is 86400 seconds. The issue with the above code is that there is no output, it seems as though Python is “hung” up on that initial while statement, so I have to stop the script. I also tried:

if t >= start or t <= end:
        ac_on = False 
else:
    if temp_inside > Temp_desired + 2:
        ac_on = True
    if temp_inside < Temp_desired:
        ac_on = False
ac_heatflow = 400 if ac_on == True else 0
return ac_heatflow, ac_on

But this sets all the values for ac_heatflow to 0.

I am unsure how to code the function so that it can be programmed to also take into account the time of day. Maybe this is a nested loop issue, or perhaps it requires a separate function that focuses on defining the time intervals and feeds those assignments into the thermostat function.

Aucun commentaire:

Enregistrer un commentaire