Use Python to change the script a trigger executes

Posted on
Tue Feb 02, 2016 5:09 pm
gazally offline
Posts: 27
Joined: Jan 18, 2016
Location: Bigfoot country

Use Python to change the script a trigger executes

First, a disclaimer. This:
1) uses an unsupported Python library, appscript,
2) to access some "legacy" functionality from a previous version of Indigo,
3) in order to accomplish something that hopefully will be built into a future version of Indigo.

But it is saving me time and effort and I'm not going to mind discarding it when #3 happens.

If you have a lot of devices and you want each one to have an associated schedule or trigger that runs a python script, and the python script needs the device number, there's no good way to get it there other than a lot of manual copying and pasting, or if you have a large number of action groups with python scripts that you need to pass a parameter into, currently the only way to do that is with a variable so you need a variable for each action group, more copying and pasting. Or if like me, you find yourself wanting to create a whole family of triggers for a large number of possible events, and leave them all disabled and have a python script that figures out which one to turn on, that means a lot of copy trigger, modify trigger, rinse, repeat, when wouldn't it just be easier to have the script build the triggers for you as needed?

The access to triggers in the IOM right now is limited and doesn't let you get at the actions attached to the trigger from Python. But I had noticed that Indigo Server's Applescript dictionary does include the actions attached to triggers, schedules and action groups. So I wrote an Applescript to change the Python script attached to one of my triggers and it worked! But I don't want to write anything in Applescript if I don't absolutely have to, and appscript theoretically lets you access anything in Applescript from Python, so I experimented with appscript until I found a working formula.

Here is my Python function for copying and editing triggers and optionally setting them up to automatically delete themselves after they execute. In order for it to work you need to create a trigger with one action that runs a Python script, called "trigger prototype". Mine is a Device State Becomes Equal To trigger and my function also changes the device id and state value.

Every time this runs it makes Indigo spit out an error to the log that is apparently the result of it feeding Python code to an Applescript syntax checker. But that doesn't stop it from working.

Code: Select all
from __future__ import unicode_literals
import appscript

def make_trigger(device_id, state_value, python_code, temporary=False):
    trig = indigo.trigger.duplicate("trigger prototype")
    trigfolder = next((f for f in indigo.triggers.folders
                       if f.name == "Script-Generated Triggers"), None)
    if trigfolder is not None:
        indigo.trigger.moveToFolder(trig.id, trigfolder.id)

    trig.deviceId = device_id
    trig.stateValue = state_value
    trig.replaceOnServer()

    if temporary:
        python_code = python_code + "\nindigo.trigger.delete({0})\n".format(trig.id)
    isapp = appscript.app("IndigoServer")
    isapp.trigger_actions[trig.name].action_steps[1].script_code.set(python_code)
    indigo.trigger.enable(trig.id, value=True)
    return trig


The Python scripts of schedules and action groups should also be accessible:
Code: Select all
isapp.time_date_actions[name].action_steps[1].script_code.set(python_code)
isapp.action_groups[name].action_steps[1].script_code.set(python_code)

Posted on
Tue Feb 02, 2016 5:12 pm
matt (support) offline
Site Admin
User avatar
Posts: 21411
Joined: Jan 27, 2003
Location: Texas

Re: Use Python to change the script a trigger executes

This scares me very much. :twisted:

But I'm glad it actually works. :roll:

Image

Posted on
Tue Feb 02, 2016 5:23 pm
jay (support) offline
Site Admin
User avatar
Posts: 18199
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Use Python to change the script a trigger executes

BTW, we include appscript in the Indigo install so any Python script running from the Execute Script action (or any plugin) has access. It is, as pointed out, no longer supported, and given the direction Apple's taking I wouldn't be surprised if it breaks any day now... ;)

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Tue Feb 02, 2016 8:33 pm
gazally offline
Posts: 27
Joined: Jan 18, 2016
Location: Bigfoot country

Re: Use Python to change the script a trigger executes

Then I hope you finish implementing Actions in the IOM before Apple breaks appscript, because otherwise I'll have to go looking for an even scarier way to do this... :shock:

Posted on
Tue Nov 22, 2016 1:47 am
gabbas offline
Posts: 39
Joined: Jun 22, 2008

Re: Use Python to change the script a trigger executes

Hi guys, any update on this in Indigo 7? Any improvements to scripting triggers?

Posted on
Tue Nov 22, 2016 9:30 am
jay (support) offline
Site Admin
User avatar
Posts: 18199
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Use Python to change the script a trigger executes

Not for the initial release of Indigo 7, but it is pretty high on our todo list. We have to get it done for several other things that we have on the longer-term roadmap to be possible, so it is definitely on the radar.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Tue Apr 19, 2022 11:26 pm
TimH offline
Posts: 20
Joined: Mar 06, 2008
Location: Los Gatos, CA

Re: Use Python to change the script a trigger executes

Is there a way to create Triggers with Actions using Python yet? I can create a trigger but don't see any examples of adding an action... I simply want to call a script.

Posted on
Wed Apr 20, 2022 5:24 am
FlyingDiver offline
User avatar
Posts: 7189
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Use Python to change the script a trigger executes

TimH wrote:
Is there a way to create Triggers with Actions using Python yet? I can create a trigger but don't see any examples of adding an action... I simply want to call a script.


Not that I know of. If you explain what you're actually trying to accomplish we might be able to come up with a solution. But doing anything with triggers and action groups from within a non-plugin script is going to be iffy.

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

Posted on
Wed Apr 20, 2022 10:53 am
TimH offline
Posts: 20
Joined: Mar 06, 2008
Location: Los Gatos, CA

Re: Use Python to change the script a trigger executes

Thanks for the reply. Its.a pity the APIs aren't there yet, I saw posts back to 2012 and a hopeful post for it coming with 7.0 in 2016 - maybe it will come this year :D I have Aeotec sensors in each room and use them to determine if a room is occupied (daily heating on etc) and if there is any recent presence (lights on/auto off). I use the same variable names with the room name appended and any variable ending in _AUTO_COUNT_DOWN is decremented automatically every minute. I then have triggers to reset the presence counter to its max when the sensor becomes on and other triggers to turn off lights etc. Lots of rooms with more or less the same stuff. Rather than set this up manually (and usually forget something!) I wrote a script that deletes everything and then recreates the required stuff for a list of rooms... nice as I make changes/improvements over time. I guess for the moment I will just have to set the actions up manually... not a big deal. Again, thanks for the quick reply and assistance.

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 2 guests