Appdaemon schedule callback error

Is this the correct forum to ask about app daemon issue?

I am having some trouble with the scheduling. Please excuse my python, I only started learning it last week. So im trying to set modes for the house based on the time of day, below is a subset of the code im using. but I cant get the scheduler to work. Its just erroring with

2016-12-01 17:28:34.005890 WARNING Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/appdaemon/appdaemon.py", line 418, in worker
    function(ha.sanitize_timer_kwargs(args["kwargs"]))
TypeError: evening() takes 1 positional argument but 2 were given
import appdaemon.appapi as appapi
import datetime

class HouseMode(appapi.AppDaemon):

    def initialize(self):
        self.log('************* HouseMode Init *******************')
        self.mode = self.get_state("input_select.housemode")

        if self.sun_down():
            self.evening()

        #Setup triggers based on Time
        self.run_in(self.evening, seconds = 5)

    def evening(self):
        """Set the house up for evening"""
        if not self.mode == "Evening":
            self.mode = "Evening"
            self.log("Switching mode to Evening")
            self.select_option("input_select.housemode", "Evening")
        else:
            self.log('Already in Evening mode, not resetting mode')

Any help would be appreciated.

Hi there - you missed out a parameter in your callback, it should be:

 def evening(self, **kwargs):

thanks for the quick response. I have tried that, forgot to include the error.

Error

2016-12-01 18:14:54.012491 WARNING Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/appdaemon/appdaemon.py", line 418, in worker
    function(ha.sanitize_timer_kwargs(args["kwargs"]))
TypeError: evening() takes 1 positional argument but 2 were given

code

import appdaemon.appapi as appapi
import datetime

class HouseMode(appapi.AppDaemon):

    def initialize(self):
        self.log('************* HouseMode Init *******************')
        self.mode = self.get_state("input_select.housemode")

        #Setup triggers based on Time
        self.run_in(self.evening, seconds = 5)

    def evening(self, **kwargs):
        """Set the house up for evening"""
        if not self.mode == "Evening":
            self.mode = "Evening"
            self.log("Switching mode to Evening")
            self.select_option("input_select.housemode", "Evening")
        else:
            self.log('Already in Evening mode, not restting mode')

Your call to run_in() isn’t right - it should be:

self.run_in(self.evening, 5)

This still produces the same error.

2016-12-01 20:00:44.018598 WARNING Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/appdaemon/appdaemon.py", line 418, in worker
    function(ha.sanitize_timer_kwargs(args["kwargs"]))
TypeError: evening() takes 1 positional argument but 2 were given
import appdaemon.appapi as appapi
import datetime

class HouseMode(appapi.AppDaemon):

    def initialize(self):
        self.log('************* HouseMode Init *******************')
        self.mode = self.get_state("input_select.housemode")

        #Setup triggers based on Time
        self.run_in(self.evening, 5)

    def evening(self):
        """Set the house up for evening"""
        if not self.mode == "Evening":
            self.mode = "Evening"
            self.log("Switching mode to Evening")
            self.select_option("input_select.housemode", "Evening")
        else:
            self.log('Already in Evening mode, not restting mode')

You still don;t have the kwargs argument, try this:

def evening(self, kwargs):

cool, so that works with the triggers. Would you mind showing me howto call evening outside the timer. So as part of initialize I have

        if self.sun_down():
            self.evening()

this works without kwargs in the def for evening

but once I put kwargs in I get

TypeError: evening() missing 1 required positional argument: 'kwargs'

(Thanks for your patience, and sorry my noob questions)

You could try using {} for the kwargs argument - it is expecting a dictionary and {} is an empty dictionary.

if self.sun_down():
            self.evening({})

Or maybe cleaner, have a 3rd procedure called do_evening() or something then call it from your evening() function as welll as directly.

THANK YOU! Ive been trying to understand what ive been doing wrong for a couple of days… both options work. (Im not completely sure why evening needs kwargs in the first place, but im not complaining as long as it works :slight_smile: )

Cheers again

It’s to allow you top pass parameters to your callbacks, but is entirely optional.

Thanks again. everything looks like its working correctly

House Mode Script

import appdaemon.appapi as appapi
import datetime

class HouseMode(appapi.AppDaemon):

    def initialize(self):
        self.log('************* HouseMode Init *******************')

        self.mode = self.get_state("input_select.housemode")

        #Set mode on startup
        now = datetime.datetime.now().time()
        if now > datetime.time(23, 00) or now < datetime.time(5, 00):
            self.night({})
        elif self.sun_up():
            self.day({})
        elif self.sun_down():
            self.evening({})

        sun_offset = datetime.timedelta(minutes = 30).total_seconds()
        self.run_at_sunset(self.evening, offset = -sun_offset)
        self.run_at_sunrise(self.day, offset = sun_offset)
        self.run_daily(self.day, datetime.time(7, 00, 0))

    def day(self, kwargs):
        # Set the house up for daytime
        if not self.mode == "Day":
            self.mode = "Day"
            self.log("Switching mode to Day")
            self.select_option("input_select.housemode", "Day")
        else:
            self.log('Already in Day mode, not resetting mode')

    def evening(self, kwargs):
        #Set the house up for evening
        if not self.mode == "Evening":
            self.mode = "Evening"
            self.log("Switching mode to Evening")
            self.select_option("input_select.housemode", "Evening")
        else:
            self.log('Already in Evening mode, not resetting mode')

    def night(self, kwargs):
        #Set the hosue for nighttime
        if not self.mode == "Night":
            self.mode = "Night"
            self.log("Switching mode to Night")
            self.select_option("input_select.housemode", "Night")

            #Turn if all off
            #self.turn_off('group.all_lights')
        else:
            self.log('Already in Night mode, not resetting mode')
1 Like

Cool - glad you figured it out :slight_smile:

1 Like