While loop

Posted on
Tue Dec 28, 2021 6:13 pm
SMUSEBY offline
Posts: 507
Joined: Sep 16, 2009
Location: California

While loop

I have some devices that are reluctant to respond for what I suspect are hardware reasons. I've tried activating schedules temporarily until the device is on, but they don't always report their status. So I'm trying to use python, and from reading online, I think the While loop is the correct method(?) - to continue in a loop until the device is on with the following code:
Code: Select all
import time
dev = indigo.devices[21376882]
while not dev.onState
   indigo.device.turnOn(21376882)
   time.sleep(2)   
time.sleep(5)   
indigo.device.turnOff(21376882)

The event log objects to line 3, "while..."
Is this approach possible? If so, what's wrong with the syntax?

Posted on
Tue Dec 28, 2021 9:20 pm
FlyingDiver offline
User avatar
Posts: 7189
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: While loop

You need a colon at the end of the line.

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

Posted on
Wed Dec 29, 2021 9:06 am
jay (support) offline
Site Admin
User avatar
Posts: 18200
Joined: Mar 19, 2008
Location: Austin, Texas

Re: While loop

Your script won't work reliably for a variety of reasons. First, your device instance (dev), will never change because devices in scripts are snapshots of the device state when it's retrieved. To fix that, add dev.refreshFromServer() after the sleep() command.

Second, you need to use self.sleep() rather than time.sleep().

Third, this script could easily become a runaway script because there's no way to stop it if the device never actually responds. This can cause the server itself to hang up. If these are Insteon devices, a 2 second sleep is not nearly long enough because Insteon is a very slow protocol and this script will just cause a big backlog in commands trying to get out to the PowerLinc. So, in effect, you are likely making the problems worse with this script, not better, by introducing a lot more Insteon traffic which will cause a lot of protocol collisions causing more failures.

I will reiterate to you what we always tell users trying to deal with this kind of situation: you really should figure out how to fix the underlying problem that's causing the devices to fail rather than hack around the symptoms.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Wed Dec 29, 2021 1:11 pm
SMUSEBY offline
Posts: 507
Joined: Sep 16, 2009
Location: California

Re: While loop

ah - the colon. You'd think I might know that trick by now.
The colon enabled the script. However, it revealed a new issue. The script not only didn't execute the last line and turn the device off, the script seems to be constantly running, watching the device: it won't let me turn it off.. When I independently turn the light off, it comes back on. I had to stop the server to be able to turn the light off..
It was my understanding that once the 'while' condition was met, the script would stop. Instead, I need some way to exit the while loop. Perhaps I could do that with an If statement? There must me some elegant way to exit the loop.
Code: Select all
import time
while not dev.onState:
   indigo.device.turnOn(21376882)
   time.sleep(2)   
indigo.device.turnOff(21376882)

Posted on
Wed Dec 29, 2021 1:27 pm
SMUSEBY offline
Posts: 507
Joined: Sep 16, 2009
Location: California

Re: While loop

It definitely is a runaway. (I just noticed your reply to my initial post after posting the runaway problem you predicted).
As for fixing the underlying issue, I have three devices for tree lights and the instructions are not reliably responded to. I don't know how to fix what I think must me noisy signals. I tried using a temporary Indigo schedule that is disabled when all three devices are on, but there are lots of "device did not respond" messages.
So if I persist with the hack, and the addition of the code you suggest, could I avoid the problem you mentioned by increasing the sleep time? (they are insteon devices). If so, how long?

Posted on
Wed Dec 29, 2021 1:55 pm
jay (support) offline
Site Admin
User avatar
Posts: 18200
Joined: Mar 19, 2008
Location: Austin, Texas

Re: While loop

SMUSEBY wrote:
So if I persist with the hack, and the addition of the code you suggest, could I avoid the problem you mentioned by increasing the sleep time? (they are insteon devices). If so, how long?


If you are running this as an embedded script, I think the process that runs it will eventually kill the script (after 10 seconds). I'd recommend waiting at least 5 seconds. So at most you'd get two (possibly 3) tries before the script is killed. Maybe it'll work, maybe it won't. Maybe it will make your system less reliable, maybe it won't.

To troubleshoot your signal noise issue: https://wiki.indigodomo.com/doku.php?id ... leshooting

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Wed Dec 29, 2021 3:02 pm
SMUSEBY offline
Posts: 507
Joined: Sep 16, 2009
Location: California

Re: While loop

The script, now revised, below runs and there is no runaway (for testing purposes, I'm using a reliable device).
Code: Select all
import time
dev = indigo.devices[21376882]
while not dev.onState:
   indigo.device.turnOn(21376882)
   self.sleep(6)
   dev.refreshFromServer()   
   #script stops here - anything following will not be executed


However, there is an error reported in the event log (I use a trigger to test my scripts):
Trigger run a script #1
Script Error fYrdTreesON.py: global name 'self' is not defined
Script Error Exception Traceback (most recent call shown last):

fYrdTreesON.py, line 30, at top level
NameError: global name 'self' is not defined

Posted on
Wed Dec 29, 2021 3:37 pm
jay (support) offline
Site Admin
User avatar
Posts: 18200
Joined: Mar 19, 2008
Location: Austin, Texas

Re: While loop

What version of Indigo are you running?

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Wed Dec 29, 2021 4:20 pm
SMUSEBY offline
Posts: 507
Joined: Sep 16, 2009
Location: California

Re: While loop

2021.1.2

Posted on
Wed Dec 29, 2021 4:21 pm
FlyingDiver offline
User avatar
Posts: 7189
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: While loop

I thought self referred to the plugin base object, which would not apply in a script?

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

Posted on
Wed Dec 29, 2021 4:31 pm
jay (support) offline
Site Admin
User avatar
Posts: 18200
Joined: Mar 19, 2008
Location: Austin, Texas

Re: While loop

It runs in a special IPH process, which I though had self defined. Guess not.

You can go ahead with time.sleep() if you insist on this approach.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Wed Dec 29, 2021 5:46 pm
SMUSEBY offline
Posts: 507
Joined: Sep 16, 2009
Location: California

Re: While loop

Forgetting about the WHILE loop approach and looking at the suggested 'fix the underlying problem' approach. I started with the three devices on. I then attempted to turn them off using the Devices panel on my iPhone. All three presses failed - the event log showed the following:
Dec 29, 2021 at 3:23:26 PM
Error "fYrdCherryTreeLights" off; send failed (no acknowledgment)
Error "fYrdCherryTreeLights" off; send failed (no acknowledgment)

Dec 29, 2021 at 3:24:00 PM
Error "fYrdDogwoodTreeLights" off; send failed (no acknowledgment)

Dec 29, 2021 at 3:24:15 PM
Error "fYrdOakTree" off; send failed (no acknowledgment)


I get this same message intermittently with the T/D Action and the While loop. The Device ON/OFF condition may or may not be reflected properly on the Devices panel.
So it seems the underlying condition is the right approach; I'm hopeful that someone may have some insight into what exactly is wrong.

Posted on
Wed Dec 29, 2021 6:39 pm
matt (support) offline
Site Admin
User avatar
Posts: 21411
Joined: Jan 27, 2003
Location: Texas

Re: While loop

Scripts run inside of their own special plugin shell, which can be accessed via indigo.activePlugin, so try using:

Code: Select all
indigo.activePlugin.sleep(5)   # example 5 second sleep

Image

Posted on
Wed Dec 29, 2021 7:21 pm
SMUSEBY offline
Posts: 507
Joined: Sep 16, 2009
Location: California

Re: While loop

I'm confused: how does indigo.activePlugin.sleep(5) help with non-responsive devices which I am trying to turn on/off with simple presses in the Device panel? Or is this command intended for use with my 'while' loops? - to replace the 'time.sleep()' function?

Posted on
Wed Dec 29, 2021 8:56 pm
matt (support) offline
Site Admin
User avatar
Posts: 21411
Joined: Jan 27, 2003
Location: Texas

Re: While loop

Sorry, I should have been more clear. That is just a replacement for time.sleep() method which will allow the Indigo Server to be able to more quickly and cleanly cancel the script when needed.

Image

Who is online

Users browsing this forum: No registered users and 6 guests