AppDaemon and constraints

I am writing an AppDaemon script that does some scheduled stuff. It’s run when triggered by a state change, turns on some stuff, and then uses run_once() to set up another callback to turn the stuff back off at a later time.

I’d like to have a front-end switch to turn the script on and off, so I set up an input boolean constraint. The problem it seems is that the constraint turns off not only the initial state change callback but also an already-scheduled run_once() callback. The result is that if I turn off the switch in between, the stuff never gets turned off, which is not the desired behavior. The desired behavior is for it to turn off, and then not turn back on again.

Now, I am sure I can fix this programmatically in the python code, by having the initial routine check for the switch being in the off state, but that seems clunky when the constraint mechanism is already present. Is there any way to have the constraint mechanism allow already-scheduled callbacks only, rather than disallowing all callbacks?

there are several levels to contrain.

you can happ app level constrain or callback level constrain.
applevel is set in yaml and it constrains the entire app like you experienced.

but callback level shouldnt, so i think thats what you are looking for.
how to?

self.listen_state(self.callback,entity, constrain_input_boolean= “input_boolean.your_name”)

1 Like

is constrain_input_boolean a kwarg in this case or is it an actual input for listen_state?

If not I could see a possible solution to create a listener that listens to the boolean. The callback for that creates the automation listener.

constraints are actual input for listen_state (or run_daily, or listen_event, or …)
but if you look really close i think they are kwargs also, i believe (i dont go looking into the code that much)

sorry i dont understand what you want with it.

its a constraint. so at the moment the listen_state gets triggered (so if a state change takes place) it checks if the boolean is on, and if not the callback from listen_state isnt called.
there are also constraints for time, weekdays, input_selects, etc.

I was simply meaning: “Create an automation that turns the automation trigger on” Instead of an automation that triggers and checks.

you wanted to use the constraints to start and stop the listener instead of the callback?

there is not really a point for that because even if you dont have listen_state the state change will still be noticed.
all you do with listen_state is connect a callback to something that is happening anyway.

The only benefit would be a reduced load on the system. That’s why I was wondering if the attribute was part of the function or kwarg. If it was part of the function, appdaemon would likely turn the trigger off.

i dont know how its handled on the background.
i leave that to Andrew. :wink:

if there is a way it could make AD better he probably knows about it, and if not you can check the code and see if there is a way to make it better. im sure he will appreciate that.

i am just the super user, who helps out others with the functionality and helps Andrew with making things better on the none coding level. :wink: the coding i leave to him.

That’s why I was asking you! There clearly are functions in AD that I do not know about and at this point I’m trying to learn them all to help others in a more ‘appdeamon’ way.

1 Like

thats great.
i would suggest coming to the appdaemon server on discord as well, makes it easier to talk about those things.

besides that i always fall back on the api.
AD api: https://appdaemon.readthedocs.io/en/latest/AD_API_REFERENCE.html
hass api: https://appdaemon.readthedocs.io/en/latest/HASS_API_REFERENCE.html
mqtt api: https://appdaemon.readthedocs.io/en/latest/MQTT_API_REFERENCE.html

those docpages i got open 24/7 :wink:
and when its not in there (like constrainst, global files, get_app, etc.) then its mostly in
https://appdaemon.readthedocs.io/en/latest/APPGUIDE.html

but feel to ask me anything any time here on PM or on discord.

Aha! That’s what I needed, thanks!