Command line arguments for python scripts

Posted on
Tue Feb 07, 2023 10:35 am
Jason G. offline
User avatar
Posts: 54
Joined: Mar 21, 2018

Command line arguments for python scripts

Hi-

Is there a way to have Indigo pass a command line argument to a script when it runs?

Here's what I'm trying to do... when someone arrives home, I want to run my atHomeArrive.py script with a param for who arrived home (atHomeArrive.py Jason) so that I can customize the push notification Indigo is sending, as well as other customizations...

I can get arguments into the python script if I run it as a normal script (python atHomeArrive.py), but if I run it in the Indigo environment, it seems like the command line arguments are not being passed along...

Code: Select all
indigo-host -x "/Library/Application Support/Perceptive Automation/Scripts/atHomeArrive.py" Foo Bar Baz


->> no command line arguments are available in sys.argv

And, obviously the same lack of args when I run from within Indigo.

Worst case I can use extra variables or multiple scripts, but I was hoping there was some way to pass command line arguments instead.

Thanks,
Jason

Posted on
Tue Feb 07, 2023 11:10 am
racarter offline
User avatar
Posts: 477
Joined: Jun 18, 2016
Location: North Yorkshire, UK

Re: Command line arguments for python scripts

If you create an action to run a shell script with an argument such as:

Code: Select all
/usr/local/bin/python '/Library/Application Support/Perceptive Automation/Scripts/skyq.py' %%d:1785163549:ipNumber%% 'chno' '115'


it should work. This example calls the script sky.py and passes two arguments: an IP number and a channel number.

Posted on
Tue Feb 07, 2023 11:18 am
Jason G. offline
User avatar
Posts: 54
Joined: Mar 21, 2018

Re: Command line arguments for python scripts

Yes, I could do that... but I would like it to run in the Indigo environment so I can do things like: indigo.dimmer.setBrightness("Kitchen Lights", value=50), etc.

Posted on
Tue Feb 07, 2023 11:56 am
jay (support) offline
Site Admin
User avatar
Posts: 18216
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Command line arguments for python scripts

You can't pass command line args to a script using indigo-host -x. But, you can pass an actual script:

Code: Select all
indigo-host -e 'indigo.dimmer.setBrightness("Kitchen Lights", value=50)'


So, you can write a generic python script that accepts the command line args, then executes the above passing in the script string.

I don't mention the REST API (which would also work) because it's being deprecated in the next release (very soon). There will be a new API that you can use (say, from a python script, or anything that can send an HTTP POST) to control devices, etc.

Check out this section of the new HTTP API docs for an example.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Tue Feb 07, 2023 12:10 pm
whmoorejr offline
User avatar
Posts: 762
Joined: Jan 15, 2013
Location: Houston, TX

Re: Command line arguments for python scripts

Not the answer you asked for but this is my solution for the same situation which uses 2 scripts (acceptable entry, other entry) and uses variables that are populated from the initiating trigger.

Known person / acceptable door use:
Whoever unlocks the door, their code updates a variable "unlockedBy" that puts their NAME in it.
I have a folder of static images for each person that has a door code in two formats (full size and thumbnail)
ACS_NAME.jpg
THUMB_NAME.jpg
my script fires that uses the variable "unlockedBy" to populate a message and retrieve the correct static image.

The full image is used to populate a control page dynamic image thing. The thumb image is used in a pushover message.
A copy of the image is saved in a log file with the current date/time added to it .

Unknown / other entry: (Jammed, invalid code, etc.):
A notification variable is populated for a jam, invalid code, etc with a description, Door jammed, invalid code, etc.
The script uses that variable to populate the message.
A snapshot from the front door camera is captured and added to the message.
A copy of the image is saved in a log file with the current date/time added to it.

In the end, if my front door is unlocked, I get a thumbnail image of the person pop up on my watch, phone and any control page in addition to an alexa announcement for who entered.

Let me know if you want me to elaborate on any of that.

Bill
My Plugin: My People

Posted on
Tue Feb 07, 2023 3:51 pm
Jason G. offline
User avatar
Posts: 54
Joined: Mar 21, 2018

Re: Command line arguments for python scripts

Very cool - that gives me several options to play with.

Thanks everyone!

Posted on
Wed Feb 08, 2023 2:08 pm
BigDaddyNC offline
User avatar
Posts: 15
Joined: Sep 25, 2019
Location: Wake Forest, NC

Re: Command line arguments for python scripts

Just my own two pennies worth...

I had a similar problem when there was a script that I wanted to share between triggers with different calling parameters. I didn't want four copies of the same code in different triggers.

I ended up writing a library module (in this case called "hvac_lib.py") and putting it into the Python3Includes folder (I may not be remembering the folder name correctly as I am not home at the moment, but that and the Scripts folder share the same parent) and then from the trigger I could do a:

Code: Select all
import hvac_lib

temp, set_point, mode = get_info_from_thermostat("upstairs", <var2>, <var3>, etc)

...

# do stuff if I want to...

You can either embed it in the trigger, or (what I did) is wrote a simple script like the above and pointed the triggers to the mini script files, which in turn called the library module with the requisite parameters.

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 11 guests