Terminating a running Python script (file)

Posted on
Wed Jan 13, 2016 4:25 am
marketability offline
User avatar
Posts: 198
Joined: Dec 08, 2015
Location: UK

Terminating a running Python script (file)

Hi
Forgive the newbie question but does anyone know if its possible to create an action group to terminate a running python script.
T`he python script I'm working on is being run as a file rather than an embedded script if that helps clarify

Many thanks
Peter

Posted on
Thu Jan 14, 2016 10:27 am
jay (support) offline
Site Admin
User avatar
Posts: 18199
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Terminating a running Python script (file)

It's possible, but kinda ugly. If I may ask, can you describe the scenario where you want to kill the script? There's often a better way to have a script terminate than killing it from the outside.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Thu Jan 14, 2016 11:17 am
marketability offline
User avatar
Posts: 198
Joined: Dec 08, 2015
Location: UK

Re: Terminating a running Python script (file)

Thanks Jay
I'm running a script to randomly turn lights on if certain variables are true.
My issue is that when other scheduled actions simulate bedtime or leaving home (i.e. all lights turn off) and change the variables, my script often runs for one more loop rather than terminating immediately with the lights turned off. An example of the script...

Code: Select all
#!/usr/bin/env python2.5

from random import randint

iterCount = 6
dev = indigo.devices[750911005] # "Porch Inside"

indigo.server.log("Random: Porch Script Started")

while iterCount > 0:
   alarmMode = indigo.variables[1007026022] # "alarm_state"
   xauto = indigo.variables[142739729] # "automated_athome"
   xneed = indigo.variables[1931720860] # "need_light"
   indigo.server.log("Alarm: " + str(alarmMode.value) + "  Auto at Home:" + str(xauto.value) + "  NeedLight:" + str(xneed.value) + "  Iteration: " + str(iterCount))
   if (xauto.value == "true") and (alarmMode.value == "on") and (xneed.value == "true"):
        randomDelay = randint(2700, 4500)
        indigo.activePlugin.sleep(randomDelay)
        indigo.server.log("Random: Turning on Porch - " + str(iterCount))
        indigo.device.turnOn(dev)
        randomDuration = randint(180, 240)
        indigo.activePlugin.sleep(randomDuration)
        indigo.device.turnOff(dev)
   iterCount -= 1
indigo.server.log("Random: Porch Script Complete")


Does that make sense? thanks for your help on this - much appreciated

Posted on
Thu Jan 14, 2016 3:14 pm
jay (support) offline
Site Admin
User avatar
Posts: 18199
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Terminating a running Python script (file)

The problem is because the delay is so long between the time you get/test the variable values in the if statement and the time that the turnOn happens. So, operationally, you're going to need to sorta rethink the order of things. Try moving the first delay to the end (so that it happens at the end of the loop rather than the beginning). That will make sure that your conditions are tested right before the turnOn command, which is what you want.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Thu Jan 14, 2016 5:29 pm
Bollar offline
Posts: 528
Joined: Aug 11, 2013

Re: Terminating a running Python script (file)

You could check an Indigo variable and do a while loop while it's in the state you want.

Code: Select all
   while string.capitalize(indigo.variables["hvacStop"].value) == str(False):
      try:

Insteon / Z-Wave / Bryant Evolution Connex /Tesla / Roomba / Elk M1 / SiteSage / Enphase Enlighten / NOAA Alerts

Posted on
Fri Jan 15, 2016 1:14 am
marketability offline
User avatar
Posts: 198
Joined: Dec 08, 2015
Location: UK

Re: Terminating a running Python script (file)

Thanks guys - will give that a crack

Posted on
Fri Jan 15, 2016 7:04 am
marketability offline
User avatar
Posts: 198
Joined: Dec 08, 2015
Location: UK

Re: Terminating a running Python script (file)

looking good so far - thanks for the help

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 2 guests