Timer Paho MQTT通过重置设置延迟

Timer Paho MQTT通过重置设置延迟,timer,mqtt,delay,Timer,Mqtt,Delay,我尝试使用Paho mqtt在接收到来自使用mqtt的传感器的“关闭”消息后设置延迟,基本上尝试跟踪15分钟内是否没有移动,然后完全关闭灯光。“开启”信息应重置计时器,并在15分钟不移动后关闭 我遇到的问题是,我只收到一次“关闭”消息,直到有人移动到传感器前面,导致“打开”,然后再次“关闭”(一旦他们离开)。因此,我当前的代码不跟踪时间,因为它只在收到消息后运行(使用on_消息回调)。我尝试过线程.Timer,但也遇到了问题,当前代码如下所示 state_on = {"state&qu

我尝试使用Paho mqtt在接收到来自使用mqtt的传感器的“关闭”消息后设置延迟,基本上尝试跟踪15分钟内是否没有移动,然后完全关闭灯光。“开启”信息应重置计时器,并在15分钟不移动后关闭

我遇到的问题是,我只收到一次“关闭”消息,直到有人移动到传感器前面,导致“打开”,然后再次“关闭”(一旦他们离开)。因此,我当前的代码不跟踪时间,因为它只在收到消息后运行(使用on_消息回调)。我尝试过线程.Timer,但也遇到了问题,当前代码如下所示

state_on = {"state": "on"}
state_off = {"state": "off"} 
now = time.time() 
print("File running at " + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))

def on_connect(client, userdata, flags, rc): 
   print("Connected, subscribing to topic...")
   client.subscribe(subscription)


def on_message(client, userdata, msg):
   global now
   print("received message: " ,str(msg.payload.decode("utf-8")))
   # set the time of the last "ON" if it is more than 15 mins ago we turn off
   if "ON" in str(msg.payload.decode("utf-8")):
      now = time.time()
      print("Movement at: " + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
      client.publish(topic_des, orjson.dumps(state_on))
   else:
      time_passed = time.time() - now
      print("No movement for " + str(time_passed) + ' seconds')    
      if time_passed >= 180.0:
         print('Turning lights off in kitchen... ')
         client.publish(topic_des, orjson.dumps(state_off))
我还看到Asyncio有一个超时函数,但我不确定它是否在这段代码中起作用