Help first attempt at AD script

I have an automation that turns a lamp on at sunset and turns off at 8:00pm. I’m trying to convert this to AD as my first test/learning script. Below is what I have and the message i get when starting AD.

Cheers,
Mike

import appdaemon.appapi as appapi
import datetime

class LRLampOn(appapi.AppDaemon):

    def initialize(self):
        self.run_at_sunset(self.lights_on)

        time = datetime.time(20, 0, 0)
        
        self.run_at(self.lights_off, time)

    def lights_on(self, kwargs):
        self.turn_on(self.args["lightID"])

    def lights_off(self, kwargs):
        self.turn_off(self.args["lightID"])

2017-03-04 17:41:01.094715 INFO AppDaemon Version 1.5.2 starting
2017-03-04 17:41:11.309488 INFO Got initial state
2017-03-04 17:41:11.313881 INFO Loading Module: /home/homeassistant/.homeassistant/apps/lampon.py
2017-03-04 17:41:11.319498 INFO Loading Object lamp_on using class LRLampOn from module lampon
2017-03-04 17:41:11.321804 WARNING ------------------------------------------------------------
2017-03-04 17:41:11.322503 WARNING Unexpected error during loading of lamp_on:
2017-03-04 17:41:11.323092 WARNING ------------------------------------------------------------
2017-03-04 17:41:11.330531 WARNING Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/appdaemon/appdaemon.py", line 996, in read_app
    init_object(name, class_name, module_name, config[name])
  File "/usr/local/lib/python3.4/dist-packages/appdaemon/appdaemon.py", line 690, in init_object
    conf.objects[name]["object"].initialize()
  File "/home/homeassistant/.homeassistant/apps/lampon.py", line 11, in initialize
    self.run_at(self.lights_off, time)
  File "/usr/local/lib/python3.4/dist-packages/appdaemon/appapi.py", line 484, in run_at
    if start < now:
TypeError: unorderable types: datetime.time() < datetime.datetime()

2017-03-04 17:41:11.331700 WARNING ------------------------------------------------------------

EDIT: I changed run_at to run_daily and the error is gone. After re-reading the command list provided by @ReneTode it looks like run_at also requires a date? Will see what happens when the sun sets here soon.

You’re close, @Mike_D! This line gives you a hint as to what’s wrong …

TypeError: unorderable types: datetime.time() < datetime.datetime()

…but it’s not very helpful if you don’t understand the datetime library. What you’re trying to do is supply self.run_at a type it isn’t expecting, and that’s why you’re getting the error. @aimc’s run_at is expecting a datetime.datetime object, and now a datetime.time object. The difference between these two? The latter is just the TIME, while the former is a DATE and TIME.

What happens when you change your assigned value of time to …

time = datetime.datetime(2017, 3, 4, 20) # year, month, date, hour

Additionally, you should understand that run_at() will run the callback once, at the specified date and time. So you’re right, run_daily(), which expects a datetime.time object fits better, and works flawlessly in your code.



###Some reading from the documentation about datetime objects…

class datetime.date
An idealized naive date, assuming the current Gregorian calendar always was, and always will be, in effect. Attributes: year, month, and day.

class datetime.time
An idealized time, independent of any particular day, assuming that every day has exactly 24*60*60 seconds (there is no notion of “leap seconds” here). Attributes: hour, minute, second, microsecond, and tzinfo.

class datetime.datetime
A combination of a date and a time. Attributes: year, month, day, hour, minute, second, microsecond, and tzinfo.

Hope this helps!

  • SN
2 Likes

Nice, lamp turned off at 8 as expected.

Thanks @SupahNoob for taking the time to give a detailed explanation, MUCH appreciated. The lamp automation was my easiest one. Time to move on to something a bit more complicated.

Cheers,
Mike

Sure! I’ve recently started a tutorial series, if you wanted to check that out! It might help you learn some of the basics or get you at least a bit more familiar with Python!

Tutorial #1
Tutorial #2

Yep already completed tutorial one with some help from @turboc. Will go through 2 soon and looking forward to more tutorials.

The time travel feature of appdaemon saves a lot of waiting around for situations like this.

1 Like

Awesome. Thanks for the info. Missed that at the end of the doc.