Code: Select all
from datetime import datetime
import time
Code: Select all
def UpdateDevice(Unit, nValue, sValue):
# Make sure that the Domoticz device still exists (they can be deleted) before updating it
if (Unit in Devices):
# Update the device before it times out even if the values are the same
SensorTimeout = Settings['SensorTimeout']
# try/catch due to http://bugs.python.org/issue27400
try:
timeDiff = datetime.now() - datetime.strptime(Devices[Unit].LastUpdate,'%Y-%m-%d %H:%M:%S')
except TypeError:
timeDiff = datetime.now() - datetime(*(time.strptime(Devices[Unit].LastUpdate,'%Y-%m-%d %H:%M:%S')[0:6]))
timeDiffMinutes = (timeDiff.seconds/60)%60
if (Devices[Unit].nValue != nValue) or (Devices[Unit].sValue != sValue or timeDiffMinutes+5 > int(SensorTimeout)):
Devices[Unit].Update(nValue=nValue, sValue=str(sValue))
Domoticz.Log("Update "+str(nValue)+":'"+str(sValue)+"' ("+Devices[Unit].Name+")")
return