completely rewritten and a compact name.
ObjectTracker gives you the option to see if youre sensors are up and running and how long ago they were last updated.
and that all trough the normal frontend.
but you can also check all other object you would like. (for example: see how long ago a switch was used)
there are 2 apps. 1 app with some general functions (which i will use in other apps) and 1 app which listens to HA.
###########################################################################################
# #
# some general function which can be called from other Apps #
# update_object_time: sets the state from objects in HA. object will look like: #
# 'controle.friendly_name_from_entity' #
# state can be last time updated or time gone by since last update #
# check_last_update_time: checks when the last time was that a sensor was updated #
# save_last_update_time: saves the time that an object is updated to a file in a dir #
# you can define. the file wil have the name: lut_entity_id.py #
# reformat_time: changed a timeformat from H:M:S to %H:%M:%S #
# #
# Rene Tode ( [email protected] ) #
# version 2.0 #
# 2016/09/04 Germany #
# #
###########################################################################################
import appdaemon.appapi as appapi
import datetime as datetime
import platform
class general_fnc(appapi.AppDaemon):
def initialize(self):
return
def update_object_time(self, object_name, object_friendly_name, dir_name, time_gone_by, time_format, object_type):
update_time = datetime.datetime.now()
new_time_format = self.reformat_time(time_format)
str_complete_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
if time_gone_by == "true" or time_gone_by == "True":
str_old_time = self.check_last_update_time(dir_name, object_name)
try:
old_time = datetime.datetime.strptime(str_old_time, "%Y-%m-%d %H:%M:%S")
except:
self.log( "strptime gives error on: " + str_old_time, level = "INFO")
return
gone_by_time = update_time - old_time
str_update_time = str(gone_by_time)[:-7] + " ago"
else:
str_update_time = update_time.strftime(new_time_format)
new_entity=object_friendly_name.replace(" ","_")
new_entity=new_entity.replace(".","")
new_entity=new_entity.replace("(","")
new_entity=new_entity.replace(")","")
self.set_state("controle." + new_entity, state = str_update_time)
def check_last_update_time(self, objects_dir, object_name):
if platform.system()=="windows":
complete_file_name = objects_dir + "\\lut_" + object_name + ".py"
else:
complete_file_name = objects_dir + "/lut_" + object_name + ".py"
try:
set_object = open(complete_file_name, "r")
old_time= set_object.readline()
set_object.close()
return old_time
except:
return "2000-01-01 00:00:00"
def save_last_update_time(self, objects_dir, object_name):
if platform.system()=="windows":
complete_file_name = objects_dir + "\\lut_" + object_name + ".py"
else:
complete_file_name = objects_dir + "/lut_" + object_name + ".py"
str_complete_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
try:
set_object = open(complete_file_name, "w")
set_object.write(str_complete_time)
set_object.close()
except:
self.log( "couldnt save the time from: " + object_name + " in " + complete_file_name, level = "INFO")
def reformat_time(self, time_format):
new_time_format = ""
for counter in range(0,len(time_format)):
if time_format[counter]!=" " and time_format[counter]!="-" and time_format[counter]!=":" and time_format[counter]!="\\":
new_time_format=new_time_format + "%" + time_format[counter]
else:
new_time_format=new_time_format + time_format[counter]
return new_time_format
save this one as general_app_functions.py in your app diectrory
###########################################################################################
# #
# ObjectTracker 2.0 #
# #
###########################################################################################
# #
# with ObjectTracker you can track the last updated time from any object in HA #
# options are to give the last time an object was updated or the time that has gone by #
# you have to set the following options in the appdaemon.cfg: #
# #
# object_type = the type you like to track (switch, input_boolean, sensor, etc) #
# time_gone_by = True or False (false for showing last updated time) #
# dir_name = the name of the directory you want the files with times saved #
# time_format = any timeformat you like (python strftime type) without % #
# H:M gives 01:27, Y-m-d H:M:S gives 2016-09-04 01:27:25, etc. #
# total_objects = the amount off object you want to track #
# object1 = HA entity_ID without the platform part. (for switch.light1 use light1) #
# object2 = ... #
# object3 = untill you reached youre total_object amount #
# #
# note that you need to set a new sections in the cfg for each type of object you like #
# to track. if you want to track 1 switch and 1 sensor you need to make to sections. #
# #
# ObjectTracker depends on general_app_functions.py set as app and set in the cfg as #
# [generalvars] #
# #
# Rene Tode ( [email protected] ) #
# version 2.0 #
# 2016/09/04 Germany #
# #
###########################################################################################
import appdaemon.appapi as appapi
import datetime
class objectcontrole(appapi.AppDaemon):
def initialize(self):
self.listen_state(self.object_controle, self.args["object_type"])
if self.args["time_gone_by"] == "true" or self.args["time_gone_by"] == "True":
time = datetime.time(0, 0, 0)
self.run_minutely(self.object_controle_minutely, time)
def object_controle(self, entity, attribute, old, new, kwargs):
fnc = self.get_app("generalvars")
device, entity_name = self.split_entity(entity)
for counter in range(1,int(self.args["total_objects"])+1):
device, entity_name = self.split_entity(entity)
object_name=self.args["object" + str(counter)]
if entity_name == object_name:
fnc.update_object_time(object_name, self.friendly_name(entity), self.args["dir_name"], self.args["time_gone_by"], self.args["time_format"], self.args["object_type"])
fnc.save_last_update_time(self.args["dir_name"], object_name)
def object_controle_minutely(self, kwargs):
fnc = self.get_app("generalvars")
for counter in range(1,int(self.args["total_objects"])+1):
object_name=self.args["object" + str(counter)]
fnc.update_object_time(object_name, self.friendly_name(self.args["object_type"] + "." + object_name), self.args["dir_name"], self.args["time_gone_by"], self.args["time_format"], self.args["object_type"])
save this 1 as objectcontrole.py in your app directory
and then edit your appdaemon.cfg file like this:
[generalvars]
module = general_app_functions
class = general_fnc
[controle]
module = objectcontrole
class = objectcontrole
object_type = sensor (give here ant object type you like, switch, input_boolean, etc)
dir_name = (the dir you like to have the files saved, no default)
time_gone_by = True or False
time_format = H:M:S ( an strftime format without %)
total_objects = 3 (the amount of object you like to track)
object1 = light1 (the part from the HA entity_ID after the dot)
object2 = light_2
object3 = anything_3
if you like to track more objecttypes make more sections like:
[controle1]
module = objectcontrole
class = objectcontrole
object_type = sensor
dir_name = c:\
time_gone_by = True
time_format = H:M:S
total_objects = 3
object1 = temp_1
object2 = rain_3
object3 = heat_2
[controle2]
module = objectcontrole
class = objectcontrole
object_type = switch
dir_name = c:\
time_gone_by = True
time_format = H:M:S
total_objects = 3
object1 = light1
object2 = light_2
object3 = anything_3
I hope you have fun with it.
tell me if anything is wrong and i will change it to.
it should be platform independant, but only tested on windows enviroment.