Conditional scripts in Python

Posted on
Wed Jun 29, 2016 3:54 pm
Brady offline
Posts: 57
Joined: May 25, 2016

Conditional scripts in Python

Full disclosure - I did not search very hard to see if this is already implemented in some way or of someone has already requested...

In the condition panels for Triggers and Schedules, it seems the that the "If script returns true" option only takes Applescript. I'd like to see the option to switch this to Python.

I just spent the last 10 min doubting what I was writing in here because I kept getting an error when I was hitting Compile and Test. Upon searching the error in Google I discovered it was actually compiling the script as Applescript.

So if it is possible to run a Python script for conditions, I'd love to know how... if it is not possible currently, outside of calling a Python script from within the Applescript... I'd love to see it in Indigo 7 or 7.1.

Thanks!

Posted on
Wed Jun 29, 2016 7:17 pm
virgahyatt offline
Posts: 132
Joined: Jan 11, 2014

Re: Conditional scripts in Python

You could just set it to always run, then have it execute a python script, Then within the script call a different action or trigger it programmatically if the condition is true.

Posted on
Thu Jun 30, 2016 5:56 am
PeteVis offline
Posts: 180
Joined: Jun 19, 2015

Re: Conditional scripts in Python

Brady, you don't need to use the script window in de condition tab, but simply go to the actions tab and put your python in that window. Here you can choose either applescript or python (embedded or an external file).

The action (python) will only get executed when your condition is true.

Posted on
Thu Jun 30, 2016 6:25 am
Colorado4Wheeler offline
User avatar
Posts: 2794
Joined: Jul 20, 2009
Location: Colorado

Re: Conditional scripts in Python

PeteVis wrote:
The action (python) will only get executed when your condition is true.

I might not understand the original post but it seems to me he wants the script to calculate if a condition is true or not before getting to that phase.

I would love to see Python in that window as well and I have little doubt it's coming soon since over the past couple of years Indigo has moved forward with Python to the point that for Applescript's it seems like the writing is on the wall.

My Modest Contributions to Indigo:

HomeKit Bridge | Device Extensions | Security Manager | LCD Creator | Room-O-Matic | Smart Dimmer | Scene Toggle | Powermiser | Homebridge Buddy

Check Them Out Here

Posted on
Thu Jun 30, 2016 9:47 am
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Conditional scripts in Python

Everyone is correct - the workaround is to have the python conditional logic in a python script action, then the script can execute an action group which contains the actions that you want to execute if true. A benefit to this approach is that you can also call an action group that runs if the conditions are false, something you can't do with the built-in conditions tab.

Code: Select all
if [CONDITIONS HERE]
    indigo.actionGroup.execute(1235456) # ID of true action group
else:
    indigo.actionGroup.execute(9408243) # ID of optinal false action group


Python scripts as conditions are something we want to do, but the implementation is quite complex from the server standpoint, so it's not likely to happen soon since there's a straight-forward workaround. This is one area where the ugly AppleScript OS implementation is in fact a benefit: AppleScripts have to execute on the application's main thread of execution, so they inherently stop action execution (and other stuff as well) from happening until they're finished processing.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Fri Jul 01, 2016 8:45 pm
Brady offline
Posts: 57
Joined: May 25, 2016

Re: Conditional scripts in Python

While I do have many rules set up with Python conditions set up in the actual action as mentioned above... I'd still really love to see Python supported in the conditions panel. The rest of the application and even documentation seems heavily focused on Python. It seems strange that this one piece is stuck with AppleScript. The reasoning given above makes perfect sense to me from a development standpoint, but I'm just looking at it from an end user stand point :-)

While I can have triggers fire different action groups based on a python script, that takes me from having 1 trigger to 1 trigger and potentially 2 action groups. When you start setting up a large number of these rules, the UI simply becomes unmanageable (I could probably come up with better naming practices). I'd almost need to create flow charts to see how everything is connected...

The other consideration is that not all of my trigger actions are Python scripts. Sometimes I'd like a Python script to evaluate the conditional then have the trigger run through my list of actions that I created within the Indigo UI (non scripted actions).

Is there any way to set up something like this? I'm not sure if all of the actions tied to a trigger are executed in parallel or sequentially.

Trigger
--- Some external trigger
Conditions
--- Set to always execute
Actions
--- Action 1: Python script with condition. Evaluate conditions and if the logic decides that I should not take action, cancel all other associated actions from running?
--- Action 2: Only runs if I don't terminate in Action 1

Posted on
Fri Jul 01, 2016 10:35 pm
kw123 offline
User avatar
Posts: 8366
Joined: May 12, 2013
Location: Dallas, TX

Re: Conditional scripts in Python

if you put everything into one python script it should work.
if you use 2 indigo actions (action1 and action 2) then they run in parallel .. you could delay action2 by 1 second and stop pending action in action1, but is actually more complicated.


in english word s (not python)
if condition 1 do action1 (set variable action1_result)
wait 0.1 seconds
if action1 results in true (eg check variable action1_result) then do action2


in python :
Code: Select all
import time
if [CONDITIONS HERE]
    indigo.actionGroup.execute(1235456)   # must set variable resultAction1  1 or 0
time.sleep(0.1)
if indigo.variable["resultAction1"].value =="1":
    indigo.actionGroup.execute(9408243)

Posted on
Sat Jul 02, 2016 6:43 am
johnpolasek offline
Posts: 911
Joined: Aug 05, 2011
Location: Aggieland, Texas

Re: Conditional scripts in Python

kw123 wrote:
if you put everything into one python script it should work.
if you use 2 indigo actions (action1 and action 2) then they run in parallel .. you could delay action2 by 1 second and stop pending action in action1, but is actually more complicated.


in english word s (not python)
if condition 1 do action1 (set variable action1_result)
wait 0.1 seconds
if action1 results in true (eg check variable action1_result) then do action2


in python :
Code: Select all
import time
if [CONDITIONS HERE]
    indigo.actionGroup.execute(1235456)   # must set variable resultAction1  1 or 0
time.sleep(0.1)
if indigo.variable["resultAction1"].value =="1":
    indigo.actionGroup.execute(9408243)



It doesn't have to be 0 or 1; You could add as many if clauses as you like to different things depending on what different values get stuffed into resultAction1... with the fall through being do nothing if it doesn't match any of the ifs; for example if the first action calculates and loads the average house temperature into the variable resultAction1 under condition house mode is vacant, you could then say if resultAction1>85 turn on the ac, if resultAction1< 60 turn on the heater, if resultAction1> 65 turn off the heater, if resultAction1<80 turn off the ac..(doing the coerce to values that Python requires, of course)

Posted on
Mon Jul 04, 2016 6:07 am
Brady offline
Posts: 57
Joined: May 25, 2016

Re: Conditional scripts in Python

I definitely appreciate all the responses :-) I've got all my needs covered by several of the solutions above.

My original request still stands, though. I'd love to deal with Python directly inside this script box. If you look at the "Indigo Scripting" documentation (here)... It would appear that the focus of Indigo as a platform is all in on Python. I think this conditional script box should follow suite :-)

In the mean time I'd maybe label the box as AppleScript so that other new users don't pull their hair out trying something stupid as I have :-)

Posted on
Mon Jul 04, 2016 7:47 am
matt (support) offline
Site Admin
User avatar
Posts: 21417
Joined: Jan 27, 2003
Location: Texas

Re: Conditional scripts in Python

We'd definitely like to see python conditionals in there as well, especially since we are promoting the use of python over AppleScript for Indigo.

Image

Posted on
Sat Aug 27, 2016 12:01 am
Vig offline
Posts: 111
Joined: Oct 06, 2015

Re: Conditional scripts in Python

It would be nice if Conditionals would allow comparing Device parameter values with variables and not just specific values.
Variables do have that feature allowing to choose between value and variable but Devices for some reason don't...
Attachments
Screen Shot 2016-08-27 at 1.57.15 AM.png
Screen Shot 2016-08-27 at 1.57.15 AM.png (43.21 KiB) Viewed 4775 times
Screen Shot 2016-08-27 at 1.56.51 AM.png
Screen Shot 2016-08-27 at 1.56.51 AM.png (57.26 KiB) Viewed 4775 times

Posted on
Wed Jun 19, 2019 9:08 am
sumocomputers offline
Posts: 267
Joined: Jun 23, 2008

Re: Conditional scripts in Python

I know this is old, but according to the following article, AppleScript is still the only supported language for "If Script Returns True" under a Trigger Condition for example.

https://wiki.indigodomo.com/doku.php?id ... conditions

I know Matt said back in 2016 they would like to support Python conditionals, since they are pushing it over AppleScript :P

So is this still the case in 2019?

Posted on
Wed Jun 19, 2019 9:31 am
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Conditional scripts in Python

Yes - when we ship 7.4 later this year it will address the issue. In the meantime, you can just write your conditional code in a Python script action then execute any actions from the script (or execute an Action Group that contains the actions from the script).

Honestly, because of the almost unlimited flexibility of the rule editor and the solution above, we've not heard very much demand from customers for this (supported by the age of the last post to this topic before yours).

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Mon Sep 30, 2019 4:13 pm
matt (support) offline
Site Admin
User avatar
Posts: 21417
Joined: Jan 27, 2003
Location: Texas

Re: Conditional scripts in Python

Indigo 7.4 is now available and includes support for Python embedded conditional scripts (inside Triggers and Schedules).

Image

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 15 guests