Python loop in function

Hello, I have made a few Conditional Functions for each stage of my irrigation system.
Now, I would like to create another function that runs the full cycle (takes about 45mins), but I would like to be able to stop the cycle when I deactivate this function.
I tried using sleep in a conditional function like so:

image

this seems to work but will wait for sleep to end before it deactivates.
How can I make it so it instantly breaks the loop and deactivates without waiting for sleep ? Or is there another way ? I’m afraid I don’t know python or what I’m really doing :slight_smile:

2 Likes

There is the variable self.running that is either True or False and determines if the controller has been deactivated. You could use code such as this to delay rather than sleep(30):

timer = time.time() + 30
while self.running and time.time() < timer:
    sleep(0.1)  # Wait 30 sec or if self.running changes to False, end sleep period
# Code to run after 30 second wait
1 Like