[Plugin] - Device Updates
Posted: Tuesday 14 March 2017 19:10
@Dnpwwo has a nice little function in his examples for only updating devices when there is something different. This is a great idea, but for devices that may go a while without anything changing, they will go offline. Here's a small update to that function which will make an update before the sensor timeout is reached.
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