Hi Andrew,
trying to use oneshot feature for listen_state but getting below error:
2018-05-20 13:29:31.884222 WARNING AppDaemon: ------------------------------------------------------------
2018-05-20 13:29:31.910179 WARNING AppDaemon: ------------------------------------------------------------
2018-05-20 13:29:31.911813 WARNING AppDaemon: Unexpected error during state_update()
2018-05-20 13:29:31.913343 WARNING AppDaemon: ------------------------------------------------------------
2018-05-20 13:29:31.916119 WARNING AppDaemon: Traceback (most recent call last):
File “/usr/lib/python3.6/site-packages/appdaemon/appdaemon.py”, line 2333, in state_update
self.process_state_change(namespace, data)
File “/usr/lib/python3.6/site-packages/appdaemon/appdaemon.py”, line 2309, in process_state_change
removes.append({“name”: callback[“name”], “uuid”: callback[“kwargs”][“handle”]})
KeyError: ‘handle’
2018-05-20 13:29:31.917726 WARNING AppDaemon: ------------------------------------------------------------
below is my script:
> import appdaemon.plugins.hass.hassapi as hass
> from datetime import datetime
> from myglobals import logger as logger
> import myglobals
> #
> # Tracker App
> #
> # Args:
> #
>
> class Tracker3(hass.Hass):
>
> def initialize(self):
> self.handler = None
> self.message = None
> self.tracker = self.args["tracker"]
> self.geomap = self.args["geomap"]
> self.proximity_home = self.args["proximity_home"]
> self.proximity_work = self.args["proximity_work"]
> self.home_towards = self.args["home_towards"]
> self.work_towards = self.args["work_towards"]
> self.time_to_home = self.args["time_to_home"]
> self.time_to_work = self.args["time_to_work"]
> self.pname = self.args["name"]
> self.work_zone = self.args["work_zone"]
> self.interval = self.args["ttls"]
>
> # global flags
> self._last_location = None
> self._left_home = False
> self._left_work = False
> self._reached_home = False
> self._reached_work = False
>
> #self._home_towards_handler = None
> #self._work_towards_handler = None
>
>
> self.log("##############################################################################################")
> for a in self.args:
> self.message="{}={}={}".format(a, self.args[a], self.get_state(self.args[a]))
> logger(self)
>
> #print myglobals
> self.message="myglobals.notify_service={}".format(myglobals.notify_service)
> logger(self)
> self.log("##############################################################################################")
>
> self.listen_state(self.track_state, self.tracker, old="home", new="not_home", duration=120)
> self.listen_state(self.track_state, self.tracker, old="not_home", new="home", duration=120)
> self.listen_state(self.track_state, self.tracker, old=self.work_zone, new="not_home", duration=120)
> self.listen_state(self.track_state, self.tracker, old="not_home", new=self.work_zone, duration=120)
>
> #self.listen_state(self.track_state, self.tracker)
> self.listen_state(self.towards_callback, self.home_towards, old="off", new="on", duration=60, oneshot=True, to="home")
> self.listen_state(self.towards_callback, self.work_towards, old="off", new="on", duration=60, oneshot=True, to="work")
>
> ##############################################################################################
> def track_state (self, entity, attribute, old, new, kwargs):
> self.message = "old={},new={}".format(old,new)
> logger(self)
>
> if old == new:
> return
>
> #reachead or left home or work
> if old == "home" and new == "not_home":
> self._left_home = True
> self.message = "{} has left home".format(self.pname)
> logger(self,flag=1)
> #self.handler = self.run_every (self.notify_travel, datetime.now(), int(float(self.get_state(self.interval))*60))
> elif old == self.work_zone and new == "not_home":
> self._left_work = True
> self.message = "{} has left work".format(self.pname)
> logger(self,flag=1)
> #self.handler = self.run_every (self.notify_travel, datetime.now(), int(float(self.get_state(self.interval))*60))
> elif old == "not_home" and new == self.work_zone:
> self._reached_work = True
> self.message = "{} has reached work".format(self.pname)
> self.cancel_tracker()
> logger(self,flag=1)
> elif old == "not_home" and new == "home":
> self._reached_home = True
> self.message = "{} has reached home".format(self.pname)
> logger(self,flag=1)
> self.cancel_tracker()
> else:
> self.message = "{} has reached {} now".format(self.pname, new)
> logger(self,flag=1)
>
> #####################################################################################################################################
> def notify_travel (self, kwargs):
> #to = "somewhere"
> to = kwargs["to"]
> #if self.get_state(self.home_towards) == "on" and self.get_state(self.work_towards) == "on":
> # return
> #elif self.get_state(self.home_towards) == "on" and self.get_state(self.work_towards) == "off":
> # to = "home"
> #elif self.get_state(self.work_towards) == "on" and self.get_state(self.home_towards) == "off":
> # to = "work"
>
> state = self.get_tracker_state(self.tracker)
> self.message = "to={} and state={}".format(to,state)
> logger(self)
> location = self.get_state(self.geomap).split(",")[1]
> zone = self.get_tracker_state(self.tracker)
> #annouce zone name if available
>
> if zone != "not_home":
> location = zone
>
> if location != self._last_location:
> distance_to_home = self.get_state(self.proximity_home)
> time_to_home = self.get_state(self.time_to_home)
> time_to_work = self.get_state(self.time_to_work)
> distance_to_work = self.get_state(self.proximity_work)
>
> if to == "home":
> self.message = "{} is traveling {}. Estimated distance & time is {}km and {}min. {} has reached {} now".format(self.pname,to,distance_to_home,time_to_home,self.pname,location)
> elif to == "work":
> self.message = "{} is traveling {}. Estimated distance & time is {}km and {}min. {} has reached {} now".format(self.pname,to,distance_to_work,time_to_work,self.pname,location)
> else:
> self.message = "{} has reached {} now".format(self.pname,location)
>
> logger(self,flag=1)
> self._last_location = location
> #####################################################################################################################################
> def cancel_tracker (self):
> try:
> self.message="cancelling tracker now"
> logger(self)
> self.cancel_timer(self.handler)
> self.handler =None
>
> self.message="done"
> logger(self)
> except KeyError:
> self.message='Tried to cancel a timer for {}, but none existed!'.format(self.tracker)
> logger(self)
>
> #####################################################################################################################################
> def towards_callback (self, entity, attribute, old, new, kwargs):
> self.handler = self.run_every (self.notify_travel, datetime.now(), int(float(self.get_state(self.interval))*60), to=kwargs["to"])