AppDaemon Q&A

Let me know which AppDaemon call had the problem and I can add some checking so you get a more informative error next time :slight_smile:

in this initialize i had 1 line in the configfile where there was no constaintdays set.


class switches(appapi.AppDaemon):

  def initialize(self):
    file_name = self.args["settingsfile"]
    sunriseon = False
    sunseton = False
    sunriseoff = False
    sunsetoff = False
    try:
      settings_file = open(file_name, "r")
      for line in settings_file:
        splitline = line.split(";")
        if not "#" in splitline[0]:
          switch = splitline[0]
          constraindays = splitline[3].rstrip('\n')
          sunseton = False
          sunriseon = False
          sunriseoff = False
          sunsetoff = False
          ontime = ""
          if splitline[1]!="":
            if "sunset" in splitline[1]:
              sunsetparts =splitline[1].split(":")
              offset1 = int(sunsetparts[1])
              sunseton = True
            elif "sunrise" in splitline[1]:
              sunsetparts =splitline[1].split(":")
              offset1 = int(sunsetparts[1])
              sunriseon = True
            else:
              ontime = self.parse_time(splitline[1])             
          else:
            ontime = ""
          if splitline[2]!="":
            if "sunset" in splitline[2]:
              sunsetparts =splitline[2].split(":")
              offset2 = int(sunsetparts[1])
              sunsetoff = True
            elif "sunrise" in splitline[2]:
              sunsetparts =splitline[2].split(":")
              offset2 = int(sunsetparts[1])
              sunriseoff = True
            else:
              offtime = self.parse_time(splitline[2])
          else:
            offtime = ""

          if sunriseon:
            self.log(switch + " tijd: sunrise " + str(offset1) + " dag: " + constraindays + " aan")
            self.run_at_sunrise(self.set_lights_on,offset=offset1,constrain_days=constraindays,switch=switch)
          if sunriseoff:
            self.log(switch + " tijd: sunrise " + str(offset2) + " dag: " + constraindays + " uit")
            self.run_at_sunrise(self.set_lights_off,offset=offset2,constrain_days=constraindays,switch=switch)
          if sunseton:
            self.log(switch + " tijd: sunset " + str(offset1) + " dag: " + constraindays + " aan")
            self.run_at_sunset(self.set_lights_on,offset=offset1,constrain_days=constraindays,switch=switch)
          if sunsetoff:
            self.log(switch + " tijd: sunset " + str(offset2) + " dag: " + constraindays + " uit")
            self.run_at_sunset(self.set_lights_off,offset=offset2,constrain_days=constraindays,switch=switch)            
          if type(ontime) is datetime.time:
            self.log(switch + " tijd: " + ontime.strftime("%H:%M:%S") + " dag: " + constraindays + " aan")
            self.run_daily(self.set_lights_on,ontime,constrain_days=constraindays,switch=switch)
          if type(offtime) is datetime.time:
            self.log(switch + " tijd: " + offtime.strftime("%H:%M:%S") + " dag: " + constraindays + " uit")
            self.run_daily(self.set_lights_off,offtime,constrain_days=constraindays,switch=switch)            
    except:
      self.log( "fout in lezen van verlichtingsfile")

it made me crazy before i found it out.

Hi Rene!

Just a suggestion of a more condensed way of achieving

In my scheduling I’ve set it up like this:

config: time = 06:00:00 on, sunrise+00:45:00 off, sunset-00:45:00 on, 23:00:00 off

  def initialize(self):
    self.populate_schedules()
    self.schedule_callback()

  def populate_schedules(self):
      for schedule in self.args["time"].split(", "):
        time, action = schedule.split(" ")
        self.schedules[time] = action

  def schedule_callback(self):
    for time, action in self.schedules.items():
      time = self.parse_time(time)
      self.run_daily(self.run_daily_callback, time, action = action)

  def run_daily_callback(self, kwargs):
    for light in self.lights:
      self.call_service("light/turn_"+action, entity_id = light)

I think this would simplify your initialisation a lot. I have removed all but the core that was relevant in my example to you.

you have a few problems in that.

  1. your sunset/sunrise are wrong if you havent initialized your app for a longer period of time.
  2. you have no possibility to contrain a single light.
  3. i dont see self.lights get filled.
  4. the light get all set at the same time.

take a look at my schedule:

#verlichting;#aan;#uit;#dagen
################################ buitenverlichting ##################################;;;
switch.waterloop;sunset:10;00:00:05;mon,tue,wed,thu,fri
switch.waterloop;sunset:10;01:45:00;sat,sun
switch.jacuzzi;sunset:20;00:00:10;mon,tue,wed,thu,fri
switch.jacuzzi;sunset:20;01:45:05;sat,sun
switch.aquariums;;01:30:00;mon,tue,wed,thu,fri,sat,sun
################################ leefruimtes ##################################;;;
switch.hboog;sunset:-3600;;mon,tue,wed,thu,fri,sat,sun
switch.hboog;;20:25:00;fri,sat
switch.hboog;00:30:00;;sat,sun
switch.hbank;sunset:-3605;;mon,tue,wed,thu,fri,sat,sun
switch.hbank;;20:25:05;fri,sat
switch.hbank;00:30:05;;sat,sun
switch.hrozenhoek;sunset:-3610;;mon,tue,wed,thu,fri,sat,sun
switch.hrozenhoek;;20:25:10;fri,sat
switch.hrozenhoek;00:30:10;;sat,sun
switch.eraamhoek;sunset:-3615;;mon,tue,wed,thu,fri,sat,sun
switch.eraamhoek;;20:25:15;fri,sat
switch.eraamhoek;00:30:15;;sat,sun
switch.epchoek;sunset:-3620;;mon,tue,wed,thu,fri,sat,sun
switch.epchoek;;20:25:20;fri,sat
switch.epchoek;00:30:20;;sat,sun
switch.ewandmeubel;sunset:-3625;;mon,tue,wed,thu,fri,sat,sun
switch.ewandmeubel;;20:25:25;fri,sat
switch.ewandmeubel;00:30:25;;sat,sun
switch.kachel_9_1;sunset:-3630;;mon,tue,wed,thu,fri,sat,sun
switch.kachel_9_1;;20:25:30;fri,sat
switch.kachel_9_1;00:30:30;;sat,sun
################################ slaapkamer ##################################;;;
switch.dekkieolinde;23:30:00;07:00:00;mon,tue,wed,thu,sun
switch.dekkieolinde;;07:00:00;fri,sat
switch.dekkieolinde;00:30:00;;sat,sun
################################ weergave switches ##################################;;;
input_boolean.donkeredag;;sunset:-3600;mon,tue,wed,thu,fri,sat,sun
input_boolean.avondverlichting;sunset:-3600;;mon,tue,wed,thu,fri,sat,sun
switch.kerstboom;sunrise:10;;mon,tue,wed,thu,fri,sat,sun
switch.kerstboom;;20:25:00;fri,sat

and you can read that a lot better if you use a spreadsheet.

Thanks for the tip on the problem with sunset/sunrise, my thought was on how Openhab was working, will look into solving that further down the line.

It was mostly suggestions to simplify that large stack of ifs at the end taken directly from my code, changes would be necessary. To add the constraints wouldn’t be much work.

I can do a full third party post with my implementation if you want that instead. Didn’t want to bloat the post with unnecessary details.

Anyway, my intent was to give you a suggestion on how to make the code easier to debug by minimising duplication, instead I got some ideas on how to solve future problems for me. Look how that turned out, thanks! :wink:

/R

i like your part with calling the service instead off turn_on and turn_of.

it would take out half of the if statements.
but it cant be less then 3 if statements because you need sunrise, sunset and time :wink:

and yeah i could decide to set on and off in the csv file, but i decided to make 2 columns 1 for off and 1 for on(and that isnt enough also, but didnt want to make it even more), to get a better overview when i read it in excel.

edit:

you can discuss about it in:

@aimc:

@renetode pointed out a “possibly” major flaw with the run_in callbacks when using sunset or sunrise, are they hard set the minute you generate the date time object or will they automatically adapt through out the year?

/R

they do adapt.
i have used then for quite some time now and they Always go off at the right sunset time.
thats why i splitt out my code to 3 different type of state listenings.

Almost looks like a crontab file. LOL

i guess the process is a lot the same.
i just looked up what a crontab file is, but yeah you could see it as such.

Well now that I understand better what you were explaining earlier, I will rephrase it

when using run_daily together with sunset or sunrise, will the time be set based on the day the date time object got generated or will it follow the changes in sun elevation over the year?

Will I have to move over to using run_at_sunset/sunrise to get that dynamic behaviour?

/R

sunset is a time that will be calculated at the time the code runs. so at init from the app.
run_daily will then run on the time you have set there.

so:

runtime=self.parse_time("sunset + 0:45:00")
# now the time is fixed until you run the init again.
self.run_daily(callback,runtime)

will run every day on 45 minutes after todays sunset.
so yeah if you want dynamic behaviour you need run_at_sunset instead.

1 Like

When I use run_at, it returns a handle that can be used to cancel the scheduled event. Can I store that handle in a list? I have a list of alarms where each alarm is composed of a room and a time. If someone changes their alarm time though, I need to cancel the existing scheduled event and schedule the new alarm time.

that is possible.
if i am correct the handle is nothing more then a long.

Yes, run_daily() will run at the same time each day. run_at_sunset/sunrise() will change every day to match relative to sunrise and sunset. Two different usecases, just pick the one you want.

It’s actually a UUID which is just a unique sequence of letters and numbers, but yes, you can store it in a list or anywhere else for that matter.

1 Like

Guys,
I am looking for suggestion/tips in order to find out issue of my AppDaemon stops working in few hours. When I say stops working it is not showing any error on terminal console (Ubuntu) but not triggering any action of any AppDaemon apps which should have taken action as Home Assistant has something changed/triggered. Another restart of AppDaemon makes it work for few more hours.

This started happening more frequent since week or two while my HADashboard project’s HAPush works without any issue on the same Ubuntu machine.

So far I have tried:

  1. Rebooting Ubuntu machine
  2. Forcing SSEClient timeout for 15 seconds, still after few hours of restart of AppDaemon all apps stops updating/taking actions

Any idea/tips? How do I add more debug info to terminal console to see what’s going on in AppDaemon?
Side notes I am running HA with secure connection using duckdns certificate.

I wish I can setup AppDaemon and HAPush to connect non-secure way (all are on same Ubuntu machine) so it doesn’t have to leave my local network to resolve name/IP to duckdns.

Help please.
Thanks a lot for your time guys.

have you tried if appdaemon keeps running when you dont start hapush?
i am afraid thats not the problem, because you state “more frequently”

could it be a memoryproblem?

I have not tried stopping (not running) HAPush to see if AppDaemon runs as expected. I will try it and report back.
About memory, no, it’s Ubuntu 16.04 with I5 processor + 16GB + 500GB, so resource is not an issue at all.
Machine runs:

  1. Home Assistant (secured)
  2. AppDaemon
  3. HADashboard
  4. HAPush
  5. OpenHAB (for Insteon hub as I don’t have API key from Insteon and they are not providing one :frowning: )
  6. HA-Bridge from bwssystem (To support Google Home and Amazon Echo)
  7. Python simple http server (port 8080) to serve mp3 files to Google cast devices for audio notifications from HA

OK, what apps are you running? Is it possible that you are using up all the threads?

What version of SSEClient are you running?