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

Posted on
Tue Dec 27, 2016 5:23 pm
Turribeach offline
Posts: 262
Joined: Feb 06, 2015
Location: London, UK

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

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

Posted on
Tue Dec 27, 2016 9:22 pm
FlyingDiver offline
Posts: 3592
Joined: Jun 07, 2014
Location: Southwest Florida, USA

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

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.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Wed Dec 28, 2016 3:54 am
Turribeach offline
Posts: 262
Joined: Feb 06, 2015
Location: London, UK

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

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]

Posted on
Wed Dec 28, 2016 2:12 pm
matt (support) offline
Site Admin
User avatar
Posts: 19439
Joined: Jan 27, 2003
Location: Texas

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

The efficiency is about identical, but you can also just do:

ToiletLight. refreshFromServer()

Image

Posted on
Wed Dec 28, 2016 6:33 pm
Turribeach offline
Posts: 262
Joined: Feb 06, 2015
Location: London, UK

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

Thanks Matt. I have now changed it as it is a lot more clearer using refreshFromServer().

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 1 guest