Stopping app - last detail I can't work out


I’m still learning with AppDaemon (and py in general, for that matter) but I’ve got everything working great, with one small exception - I can’t figure out how to get the app to stop.

I’m using it to run some custom lighting scenes on my Hue lights. I have a binary sensor in HA that monitors an input boolean, so when I toggle the input to “on” - the AD app starts almost immediately.

I’ve been reading the documentation and this is what I have come up with so far (The initialize is working perfectly, the stop_app, not so much):

import appdaemon.plugins.hass.hassapi as hass

class TestClass(hass.Hass):
    def initialize(self):
        self.listen_state(self.test, "binary_sensor.test_flash_sensor", new = "on")

    def stop_app(self):
        self.listen_state(self.test, "binary_sensor.test_flash_sensor", new = "off")

    def test(self, entity, attribute, old, new, kwargs):

        while True:

            import requests

I left the rest of the code out (the Hue API requests to change light colors, etc)

Appreciate any help you might be able to offer!

Hi, I think there are some things which come together.

  • When you have an app like your TestClass the function initialize() is called whenever your app is started by AppDaemon. This is not the place to define conditions for your app to be started, but it is the code which will run when it is started.
  • The method listen_state() is used to register a callback for an event which is triggered by AppDaemon. In your example you register a callback to the function ‘test’, when the state of binary_sensor.test_flash_sensor is switched to on. Since you do that during initialisation, the function ‘test’ of your app is executed every time the binary_sensor is turned on.
  • The ‘stop_app’ function does nothing in your example, because it is never called. It is not a method which you should define yourself, but it is a method which you can call to manually stop a different app.
  • while True: creates a loop which will never ends, so the code inside will run forever (unless there is a break or return inside the loop).

So what happens in your example is: the app TestClass is started and the function initialize() is executed. It registers a callback to ‘test’ function when the input_boolean is turned on. When this happens the ‘test’ function is executed and stays forever in the infinite while loop.

Hope that helps…

Hi Tobi-bo,

Thanks for the help, that makes sense. So would it work to put some type of listen_state inside the loop? Basically what I’m trying to accomplish is something that will wait for the binary sensor to be turned off, stop the loop, and then go back to listening for the binary sensor to be turned back on again.

Thanks again, your post helped me conceptualize the different parts of the app and how they interact, now I’m trying to figure out how to fix it. This forum has been awesome in dealing with people new to all this, so I really appreciate it.

I think it is generally not a good idea to have an infinite loop in an app. Because normally one instance of an app is linked to one thread. So when your app is in the while loop, it cannot receive any calls from the listener. So once in the loop it is stuck there.