在Python中使用“ IF语句”逻辑编程恒温器,以进行时间调度



我正在处理可以根据一天中的时间进行编程的恒温器。

这是我的恒温器功能:

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是一个集合整数值,temp_inside是与Temp_desired相比(目前每几秒钟)相比的变化室内温度值,以查看空调(A/C)是" ON"或"或"离开。"ac_heatflow是A/C打开时的功率用法,而acPower是在功能之外分配的A/C功率值。

我现在的问题是,我现在想在我的恒温器中添加一个时间组件,可以将A/C编程为关闭。例如,A/C全天正常工作,但是从8:00 am到10:00 AM必须关闭,从3:00 pm到下午5:45,它必须关闭,因此在这些间隔ac_on = False中,但是在外面这些间隔恒温器将恢复回原始方法,在该方法基于室温下确定空调是否打开/关闭的原始方法。

上述时间间隔在功能中使用时将在几秒钟内,因此8:00 AM将为28800,10:00 AM-> 36000,3:00 pm-> 54000,5:45 pm-> 63900。<<<<<<

这就是我一直在尝试的:

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是间隔的开始时间, end是间隔末端时间,而 t现在为几秒钟。在24小时内,每两秒钟的温度计算,整天为86400秒,因此startend可以是0到86400之间的任何值,例如start = 1000end = 3000。上述代码的问题是没有输出,似乎Python在语句中的初始文章中被"悬挂",因此我必须停止脚本。我也尝试了:

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

但这将ac_heatflow的所有值设置为0。

我不确定如何编码该功能,以便可以对其进行编程以考虑一天中的时间。也许这是一个嵌套的循环问题,或者也许需要一个单独的功能,该功能重点是定义时间间隔并将这些作业馈送到恒温器功能中。

问题是表达式t >= start or t <= end。首先是什么开始?您在两个时间段中描述了这种情况,但是您只是在一个潜在时期的函数参数中转到函数论点?

假设start=100end =700。如果t在开始之前,请说t=5t仍然小于700,因此此语句将是正确的。或者,如果t在结束之后,例如t=705,则t仍然大于100,因此再次将此表达式评估为true(这就是为什么ac_on始终是false的原因)。基本上,无论t的价值如何,此语句始终是正确的。我认为您想要的是t >= start and t <= end

尽管我仍然对您描述的两个时间段有点混淆,也许通过4个参数,start1,end1,start2和end2,然后使用:

if (t >= start1 and t <= end1) or (t >= start2 and t <= end2):

关键是使用Pythons datetime库。一些指针:

  1. 您在功能中不使用ac_on的先前值,因此无需将其传递。
  2. 当前时间可以使用datetime lib
  3. 在功能体中找到
  4. 由于您习惯了有条件分配的蟒蛇,因此我们可以简洁而pythonic,并进行一些重构。

    import datetime
    def ithermostat(temp, ac_power, desired_temp,
       off_times={'morning':(datetime.time(8, 0), datetime.time(10, 0)),
                  'afternoon':(datetime.time(15, 0), datetime.time(17, 45))}):
        """(num, num, num, dict) -> (num, bool)
    Return the tuple (ac_power_usage per unit time, ac_on status) of the thermostat
    The ac is turned on only when the current time is not in off times AND
    the tempreture is higher than the desired tempreture
    """
    off_period = False # this flag will be set when the system is in the off period
    current_time = datetime.datetime.now().time()
    for k, v in off_times.items():
        print(v)
        if v[0] <= current_time <= v[1]:
            print("system in mandatory off period")
            off_period = True
            break
    ac_on = False if off_period else temp > desired_temp   
    ac_power_usage = ac_power if ac_on else 0
    return ac_power_usage, ac_on
    

同一功能的更简洁的更可读性和更多的Pythonic版本如下:

def ithermostat2(temp, ac_power, desired_temp,
           off_times = {'morning':(datetime.time(8, 0), datetime.time(10, 0)),
                        'afternoon':(datetime.time(15, 0), datetime.time(17, 45))}):
    """(num, num, num, dict) -> (num, bool)
    Return the tuple (ac_power_usage per unit time, ac_on status) of the thermostat
    The ac is turned on only when the current time is not in off times AND
    the tempreture is higher than the desired tempreture
    """
    current_time = datetime.datetime.now().time()
    off_period = any(v[0] <= current_time <= v[1] for k, v in off_times.items()) #check if the current time is in off_times
    ac_on = False if off_period else temp > desired_temp   
    ac_power_usage = ac_power if ac_on else 0
    return ac_power_usage, ac_on

两个功能都已测试并运行良好。

最新更新