OpenHAB calling external scripts from openHAB

Hi all,

I was just wondering if anyone has any experience with calling external scripts from openHAB’s rules and scripting language. For example possibly having a rule which uses the openHAB timers to call a python script every X seconds. If not is there anyway to execute shell commands from openHAB’s scripting language. I am currently running openHAB on the raspberry pi with the latest version of openhabian.

Edit:
I have found this executeCommandLine(String commandLine), haven’t had a chance to test it yet but could be of help to people https://www.openhab.org/docs/configuration/actions.html#exec-actions

What version? I know there was whitelisting implemented in 2.5.2 for exec. I do not know if that affects other external script calls.

This was the image I downloaded and flashed to my SD card running on the raspberry pi 3 https://github.com/openhab/openhabian/releases/tag/v1.5

executeCommandLine is not affected by the whitelist issue… no worries

yes, sounds like a great use for a rule triggered by cron, see here

getting an external script running from a rule can be a little tricky
first make sure it runs as the openhab user from the command line
make sure openhab user owns script and script is executable ect

1 Like

If you just freshly installed, you are running openHAB 2.5.2. the openHABian image itself does not contain openHAB but downloads the current stable version.

You may want to look at Jython scripting to keep things in the same language. Jython is basically Python 2.

I have seen things about Jython on the openHAB website but haven’t looked too much into it. The scripts I am currently running now are in python 3 but I would like to try to integrate them into openHAB so it can handle more of the scheduling of scripts so I don’t have to implement it myself in the python scripts.

@rlkoshak and @5iver are a couple of the experts here.

Here’s a bundle to make the install go smoother…

And here is the documentation for the helper libraries…

https://openhab-scripters.github.io/openhab-helper-libraries/index.html

Other than needing an import, the executeCommandLine Action is used just as it is in the DSL…

https://openhab-scripters.github.io/openhab-helper-libraries/Guides/Actions.html?highlight=executecommandline

2 Likes

Thanks for the resources Scott. I have been messing around with the executeCommandLine Action and have got it running external scripts that I have in python3. The code looks something like this in case anyone else was interested and is located in the rules/ directory of openHAB. The permissions on the python files also had to be changed to give openHAB access to them.

rule Test_Volts
when
  System started
then 
   var result1 = executeCommandLine("python3 /home/openhabian/Environments/env_1/openHAB_Proj/bin/main.py")
logInfo("Test_Volts", result1)
end
1 Like

Assuming it is not massive, if you post your script I’ll convert it over to scripted automation.

2 Likes

I am going to see how this works out with just calling them as I have my own python packages that I created so might be a bit of work in it, thanks for the offer though. If i do end up having to convert them I’ll probably ask you for few pointers if thats ok? :grinning:

1 Like

HABApp runs on python3 natively and can call functions from your script without any changes.
Or you can use the built in scheduler:

import HABApp

class MyRule(HABApp.Rule):

    def __init__(self):
        super().__init__()
        self.run_minutely(self.run_every_min)

    def run_every_min(self):
        self.execute_subprocess(self.proc_finished, 'path_to_executable', 'arg1', capture_output=True)
        
    def proc_finished(self, info: HABApp.rule.FinishedProcessInfo):
        print(info.returncode)
        print(info.stdout)

1 Like