Page 1 of 1

External script can't see the change on the onState property

PostPosted: Tue Dec 27, 2016 5:23 pm
by Turribeach
Hi,

I want to execute a while loop while a device it's on. I got this code in an external Python script:

Code: Select all
ToiletLight = indigo.devices[999999999]

while ToiletLight.onState and ToiletLight.enabled:
   do something


The script works as expected in that if the device it's Off it won't go inside the while loop and if it is On it will get inside and execute what I want. But the issue I got is that if I switch the device off while the while loop it's executing it the script never exits the while loop even though ToiletLight.onState should then be False. I have printed the ToiletLight.onState inside the while statement and I can confirm it stays True, at least for context of this script. Am I doing something wrong? Why can't the external script see the change on the ToiletLight.onState property value?

Thanks

Re: External script can't see the change on the onState prop

PostPosted: Tue Dec 27, 2016 9:22 pm
by FlyingDiver
Code: Select all
ToiletLight = indigo.devices[999999999]
is returning a static snapshot of the device state. You either need to re-fetch it or update it inside the loop.

Re: External script can't see the change on the onState prop

PostPosted: Wed Dec 28, 2016 3:54 am
by Turribeach
Thanks Joe, that works. I was incorrectly assuming the line was returning a reference to the object, not a copy. Here is how it looks now:

Code: Select all
ToiletLight = indigo.devices[999999999]

while ToiletLight.onState and ToiletLight.enabled:
   do something
   ToiletLight = indigo.devices[999999999]

Re: External script can't see the change on the onState prop

PostPosted: Wed Dec 28, 2016 2:12 pm
by matt (support)
The efficiency is about identical, but you can also just do:

ToiletLight. refreshFromServer()

Re: External script can't see the change on the onState prop

PostPosted: Wed Dec 28, 2016 6:33 pm
by Turribeach
Thanks Matt. I have now changed it as it is a lot more clearer using refreshFromServer().