- Code: Select all
timeDelta = datetime.now() - lamp.lastChanged
if not lamp.onState and timeDelta.seconds > 60:
I presume that "lamp" is what in C would be a struct, but then what is "timeDelta"? It looks like a struct, too, but (a) how can you subtract two numbers and get a struct? and (b) ".seconds" looks like a call to a conversion function rather than accessing one element of a structure. Python sure does use a lot of periods. (Clearly it would help if I sat down and learned the language properly!)
Anyway, here is my attempt at Pythonizing my AppleScript. The first script, triggered when my boiler turns on, logs the time. The second, triggered when the boiler turns off, logs the time, calculates the run time, and resets the relevant Indigo variable. I tried substituting the datetime.now() function for indigo.server.getTime(), but the result was the same.
Thanks for your help!
- Code: Select all
import os
# set up log file name
data_path = os.path.expanduser('~') + "/Documents/BoilerData-TEST.txt"
try:
# open log file
with open(data_path, 'a+') as outfile:
# set Indigo variable to current time
now = indigo.server.getTime()
indigo.variable.updateValue(79472523, value=unicode(now))
# write the boiler startup time to the log file
decimal = (now.hour * 3600.0 + now.minute * 60.0 + now.second) / (3600.0*24.0)
outfile.write((u"{0:%m/%d/%Y}\t{1}\ton\r".format(now, decimal)))
except Exception as err:
indigo.server.log("boiler monitor error: {0}".format(err))
- Code: Select all
import os
# set up log file name
data_path = os.path.expanduser('~') + "/Documents/BoilerData-TEST.txt"
try:
# open log file
with open(data_path, 'a+') as outfile:
# write the boiler shutdown time to the log file
now = indigo.server.getTime()
decimal = (now.hour * 3600.0 + now.minute * 60.0 + now.second) / (3600.0*24.0)
outfile.write((u"{0:%m/%d/%Y}\t{1}\toff".format(now, decimal)))
# if we can calculate an elapsed time, do so
startup = indigo.variables[79472523].getValue(int)
if startup > 0:
elapsed = now - startup
# write elapsed time (in seconds) to the log file
outfile.write((u"\t{0}\r".format(elapsed)))
# reset variable to indicate the boiler is now off
indigo.variable.updateValue(79472523, value=unicode(0))
else:
outfile.write((u"\r"))
except Exception as err:
indigo.server.log("boiler monitor error: {0}".format(err))