How can I create a light sequence without overflowing threads?

Hi guys!

When I wake up, I’d like to turn on some lights in sequence, and fade them in. Eg:

In 5 s, fade in light by bed over 10s to 50 brightness
In 30 s, fade in main light over 50s to 255 brightness

Etc. I thought I was being clever by iterating over them like this:

    """ A list of lists containing:
    Entity id, delay, fade duration
    """

    lights = [
        ["light.monitor", 2, 30],
        ["light.bathroom_2", 2, 20],
        ["light.reol_2", 2, 240],
        ["light.loft_2", 210, 300],
        ["light.gang", 60, 300],
    ]


    for light in lights:
            self.run_in(self.setstate,
                        light[1], <-- Delay
                        lt=light[0], <-- Entity ID 
                        fade=light[2], <-- Fade time
                        switch="input_boolean.carpemieke" <-- Conditional switch)

    def setstate(self, lt, brightness, fade, switch, color=""):
    if self.get_state(switch) == "on":
        self.log("Set " + lt + " to fade in over " + str(fade * self.modulator) + "s with \n     Brightness: {}\n     Kelvin: {}".format(brightness, color))

        if self.get_state(switch) == "on":
            if color != "":
                self.turn_on(lt, brightness=brightness, transition=self.modulator * fade, kelvin=color)
            else:
                self.turn_on(lt, brightness=brightness, transition=self.modulator * fade)

        if self.get_state(switch) == "on":
            time.sleep(self.modulator * fade)
    else:
        self.log("Switch turned off, terminating")

However, this seems to result in thread starvation.

2019-05-13 07:53:08.537428 WARNING AppDaemon: Queue size is 10, suspect thread starvation

2019-05-13 07:53:08.537830 INFO AppDaemon: --------------------------------------------------
2019-05-13 07:53:08.538098 INFO AppDaemon: Threads
2019-05-13 07:53:08.538374 INFO AppDaemon: --------------------------------------------------
2019-05-13 07:53:08.538682 INFO AppDaemon: Currently busy threads: 20
2019-05-13 07:53:08.538988 INFO AppDaemon: Most used threads: 24 at 2019-05-10 21:10:53.252923
2019-05-13 07:53:08.539271 INFO AppDaemon: Last activity: 2019-05-12 23:09:54.492178
2019-05-13 07:53:08.539535 INFO AppDaemon: --------------------------------------------------
2019-05-13 07:53:08.540256 INFO AppDaemon: thread-1 - current callback: idle since 2019-05-10 21:27:44.252923, alive: False
2019-05-13 07:53:08.540602 INFO AppDaemon: thread-2 - current callback: idle since 2019-05-11 22:01:28.492178, alive: False
2019-05-13 07:53:08.540906 INFO AppDaemon: thread-3 - current callback: idle since 2019-05-12 23:09:54.492178, alive: False
2019-05-13 07:53:08.541209 INFO AppDaemon: thread-4 - current callback: idle since 2019-05-10 20:57:41.252923, alive: False
2019-05-13 07:53:08.541528 INFO AppDaemon: thread-5 - current callback: idle since 2019-05-10 21:03:12.252923, alive: False
2019-05-13 07:53:08.541842 INFO AppDaemon: thread-6 - current callback: idle since 2019-05-10 20:57:08.252923, alive: False
2019-05-13 07:53:08.542153 INFO AppDaemon: thread-7 - current callback: idle since 2019-05-11 22:01:28.492178, alive: False
2019-05-13 07:53:08.542452 INFO AppDaemon: thread-8 - current callback: idle since 2019-05-10 20:57:12.252923, alive: False
2019-05-13 07:53:08.542752 INFO AppDaemon: thread-9 - current callback: idle since 2019-05-10 21:29:44.252923, alive: False
2019-05-13 07:53:08.543053 INFO AppDaemon: thread-10 - current callback: idle since 2019-05-10 21:02:40.252923, alive: False
2019-05-13 07:53:08.543318 INFO AppDaemon: --------------------------------------------------

I’m guessing this is because the run_in commands aren’t assigned a handle, and therefore aren’t cleared as variables? Or perhaps my diagnosis is wrong? Any ideas? :slight_smile:

Thanks!

I am not sure what you think this is doing, but I expect it is preventing the thread from exiting for enough time for you to run out of threads.

Uff, that’s embarrassing! It’s a remnanet from a time where I had even worse coding practices.

I’ve stripped it – I’ll let you know if it fixes it. Thanks!