Update device state in 'while loop'
Posted: Wed Apr 03, 2024 9:10 am
I am trying to break out of a while loop when a contact sensor changes but the script obviously only knows the state at the start. Is there a way of updating it inside the loop?
- Code: Select all
# Import the time module for time-related functions
import time
# Initialize the loop counter
count_loop = 0
# Get the device by its ID
dev = indigo.devices[683155862] # "Z Aqara P1 door & window contact"
# Log the current state of the onOffState
indigo.server.log(f"Current onOffState: {dev.states['onOffState']}")
# Pause execution for 2 seconds
time.sleep(2)
# Retrieve the initial state of the onOffState
doorStatus = dev.states["onOffState"]
# Loop until the doorStatus becomes True or count_loop reaches 10
while not doorStatus and count_loop < 10:
# Log the current iteration number and the onOffState
indigo.server.log(f"Loop iteration {count_loop}, onOffState: {doorStatus}")
# Increment the loop counter
count_loop += 1
# Pause execution for 2 seconds
time.sleep(2)
# Update the doorStatus with the current state
doorStatus = dev.states["onOffState"]
# Log a message indicating the end of the loop and the final value of count_loop
indigo.server.log(f"Loop finished, count_loop is {count_loop}")