How to run Python script from Windows with a rule?

:frowning: am trying to run a simple python script from a windows PC where I installed the OH 3.4. My rule is:

configuration: {}
triggers: []
conditions: []
actions:
  - inputs: {}
    id: "1"
    configuration:
      type: application/vnd.openhab.dsl.rule
      script: executeCommandLine("python C:\openhab\conf\scripts\test.py")
    type: script.ScriptAction
[ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID '5e0474553a' failed: executeCommandLine("python C:\openhab\conf\scripts\test.py")

the python script is:

with open("c:\\temp\\example.txt", "w") as f:
    f.write("Hello world!")

this script is only used for a test until I can start from OH this script.

Can anybody öease give me a hint how to correct the syntax? I tried for hours already but no idea… :frowning:

Have you installed the exec binding?
Is the line you want to execute included in the exec.whitelist file?
You may find interesting informations in this thread

The Exec binding is not needed here. The Exec binding is to create Things that get their values from executing external programs.

The Exec action is part of the core. In this case the Exec action allows you to execute an external program from your scripts, which is what the OP is trying to do.

First, make sure that python is in the path that the openhab process can see.

Secondly, the executeCommandLine takes each argument as one piece of string, so it literally looks for a file called python<literal space>C:\openhab........ It doesn’t split them up for you like a normal shell / command prompt would.

Here take a look at the documentation, it specifically addressed this topic:

I installed it here:

C:\Program Files\Python311

while the PATH environment variable is set. When I directly run the python script than it works. This means I do not have to give the full python path. But, how do I find out if openhab knows the path as well?

Ok, I have seen the docu but it is not fully clear to me. When having this line:

script: executeCommandLine("C:\\openhab\\conf\\scripts\\test.py")

Does OH know it needs to start python or do I need to mention it like: executeCommandLine(python "C:.…) ?
And do I need “\” or "" or “//” or “/” if I am using everything in windows?

Thanks

Use full paths then you won’t have to worry about the PATH environment variable.

executeCommandLine("full path to python executable", "C:\\openhab\\conf\\scripts\\test.py", "arg1", "arg2")

Just try it with \\ and if that doesn’t work, try /
Sorry I can’t test it myself. I don’t have any Windows machine.

One small addition to what @JimT wrote. Use the version of executeCommandLine that returns a message in case of a problem:

var Response = executeCommandLine(Duration.ofSeconds(5),"full path to python executable", "C:\\openhab\\conf\\scripts\\test.py", "arg1", "arg2")

Write the content of Response to logoutput.

As long as the extension .py is registered with the OS I would expect the OS to take care about executing the script by running the python interpreter. To be on safe side and have everything under control the approach of @JimT should be perfect.

Hello JimT
I used “\” and the full path to the python.exe and it works perfectly!!!
Thanks a lot :slight_smile:

This is a very good idea for debugging - thanks!

Just one question more: What you mean with “arg1 and arg2” in detail? Are this the arguments used for the python script or are this the arguments used for OpenHab?

Let’s say I have a python sript “test.py” with following definition inside:

set_gpio(self, chn, state)

What you use for the openhab call now?

executeCommandLine(“full path to python executable”, “C:\openhab\conf\scripts\test.py”,…

Thanks

Those are the arguments for the command, equivalent to typing:

path/to/python/executable c:\openhab\conf\scripts\test.py arg1 arg2

on the command line.

Ok thank you that works but in my case only for scripts which do only one task. With my python script I do have one big issue.

class Russ_GPIO(object):
    def __init__(self):
        
        self.com4 = serial.Serial(port="COM4", baudrate=115200, rtscts=False, dsrdtr=False, timeout=0.1, parity=serial.PARITY_NONE)

        
        self.com4.setRTS(True)
        self.com4.break_condition = True
          
        
    
    def set_gpio(self, chn, state):     #Set channel (0,1) and state (0,1)
        if chn == 0:
            if state == 0: 
                self.com5.setRTS(True)       
            else:
                self.com5.setRTS(False)      

In this case everything works when using the IDLE of pyhton. I can instantiate the class and run afterwards the method. But how it looks when I use OH? Here I can not find a solution how I instantiate the class in order the comport remains open. The comport opens but will close right again. This also happens when I run the script dircectly from the command line.

So is there a solution for my problem?