Javascript rule to run Linux bash script or python script

User erik for the VNC, which is the same user as I use for the SSH connection.

Yep.

Maybe, but why then? It works when I run it as user openhab in the CLI. (Although not when SSH’ing as erik and then using sudo su -s /bin/bash openhab, only when VNC’ing as erik and then using sudo su -s /bin/bash openhab in the CLI…)

Some more testing:

erik@MinipcLG2:/var/lib/openhab/bin/python/tado-manual-control$ sudo nano tado-manual-control.py
[sudo] wachtwoord voor erik:
erik@MinipcLG2:/var/lib/openhab/bin/python/tado-manual-control$ sudo nano tado-manual-control.py
erik@MinipcLG2:/var/lib/openhab/bin/python/tado-manual-control$ sudo -u openhab nano test.py
erik@MinipcLG2:/var/lib/openhab/bin/python/tado-manual-control$ sudo chmod +x test.py
erik@MinipcLG2:/var/lib/openhab/bin/python/tado-manual-control$ ls -pal
totaal 56
drwxrwxr-x  4 openhab openhab  4096 jan 19 22:40 ./
drwxrwxr-x+ 4 erik    erik     4096 sep 17 18:15 ../
drwxrwxr-x  8 openhab openhab  4096 sep 17 18:13 .git/
-rw-rw-r--  1 openhab openhab  4688 sep 17 18:13 .gitignore
-rw-rw-r--  1 openhab openhab    21 sep 17 18:13 README.md
-rw-rw-rw-  1 openhab openhab    85 jan 19 20:38 refresh_token.json
-rw-rw-r--  1 openhab openhab    21 sep 20 16:13 requirements.txt
-rw-rw-r--  1 openhab openhab 12407 jan 19 22:39 tado-manual-control.py
-rwxrwxr-x  1 openhab openhab    31 jan 19 22:40 test.py
drwxrwxr-x  5 openhab openhab  4096 jan 19 19:22 venv/
erik@MinipcLG2:/var/lib/openhab/bin/python/tado-manual-control$ sudo -u openhab /var/lib/openhab/bin/python/tado-manual-control/venv/bin/python /var/lib/openhab/bin/python/tado-manual-control/test.py
testing python script

test.py contains one line: testing python script.

But from a rule:

var testscript = 'var/lib/openhab/bin/python/tado-manual-control/test.py';

function statusBewonersAanpassen(thuis) {
  console.log(thuis);
  console.log(typeof thuis);
  
  var status = thuis == "ON" ? 'True' : 'False';
  console.log(status);
  console.log(typeof status);
  //var command = [pythoninvenv, pythonscript, 'set_home_state', '-s',  status];
  var command = [pythoninvenv, testscript];
  callPython(command);
}


function callPython(command) {
  var pythonOutput = actions.Exec.executeCommandLine(time.Duration.ofSeconds(15), command);
  console.log("python output = "+pythonOutput);
}

Output:

22:42:31.467 [INFO ] [.automation.jsscripting.rule.tado-API] - testswitch
22:42:31.470 [INFO ] [.automation.jsscripting.rule.tado-API] - OFF
22:42:31.472 [INFO ] [.automation.jsscripting.rule.tado-API] - string
22:42:31.473 [INFO ] [.automation.jsscripting.rule.tado-API] - False
22:42:31.475 [INFO ] [.automation.jsscripting.rule.tado-API] - string
22:42:31.531 [INFO ] [.automation.jsscripting.rule.tado-API] - python output = /var/lib/openhab/bin/python/tado-manual-control/venv/bin/python: can't open file '/var/lib/openhab/var/lib/openhab/bin/python/tado-manual-control/test.py': [Errno 2] No such file or directory

Should openHAB be restarted after changing something to the files in /var/lib/openhab/bin/ or something like that? (I’ll try that tomorrow - should have been a default troubleshoot action, I suppose.)

EDIT:
Aargh… I forgot the leading slash.

Anyway: the actual script I want to run, keeps refusing to work:

erik@MinipcLG2:/var/lib/openhab/bin/python/tado-manual-control$ sudo -u openhab /var/lib/openhab/bin/python/tado-manual-control/venv/bin/python /var/lib/openhab/bin/python/tado-manual-control/tado-manual-control.py get_rate_limit_info
Login successful
total calls: 5000
remaining calls: 4954
rate limit window (in seconds): 86400
rate limit resets at (UTC): None

or

erik@MinipcLG2:/var/lib/openhab/bin/python/tado-manual-control$ sudo su -s /bin/bash openhab
openhab@MinipcLG2:~/bin/python/tado-manual-control$ /var/lib/openhab/bin/python/tado-manual-control/venv/bin/python /var/lib/openhab/bin/python/tado-manual-control/tado-manual-control.py get_rate_limit_info
Login successful
total calls: 5000
remaining calls: 4953
rate limit window (in seconds): 86400
rate limit resets at (UTC): None

versus:

configuration: {}
triggers:
  - id: "9"
    configuration:
      itemName: testswitch
    type: core.ItemStateChangeTrigger
conditions: []
actions:
  - inputs: {}
    id: "1"
    configuration:
      type: application/javascript
      script: |-
        var pythoninvenv = '/var/lib/openhab/bin/python/tado-manual-control/venv/bin/python';
        var pythonscript = '/var/lib/openhab/bin/python/tado-manual-control/tado-manual-control.py';
        
        function statusBewonersAanpassen(thuis) {
          console.log(thuis);
          console.log(typeof thuis);
          
          var status = thuis == "ON" ? 'True' : 'False';
          console.log(status);
          console.log(typeof status);
          //var command = [pythoninvenv, pythonscript, 'set_home_state', '-s',  status];
          var command = [pythoninvenv, pythonscript, 'get_rate_limit_info'];
          //var command = [pythoninvenv, testscript];
          callPython(command);
        }


        function callPython(command) {
          var pythonOutput = actions.Exec.executeCommandLine(time.Duration.ofSeconds(15), command);
          console.log("python output = "+pythonOutput);
        }


        function main() {
            statusBewonersAanpassen(event.newState);
        }

        main()
    type: script.ScriptAction

output:

10:54:22.848 [INFO ] [openhab.event.ItemCommandEvent       ] - Item 'testswitch' received command ON (source: org.openhab.ui=>org.openhab.core.io.rest$erik)
10:54:22.851 [INFO ] [openhab.event.ItemStateChangedEvent  ] - Item 'testswitch' changed from OFF to ON (source: org.openhab.core.autoupdate)
10:54:22.854 [INFO ] [.automation.jsscripting.rule.tado-API] - testswitch
10:54:22.856 [INFO ] [.automation.jsscripting.rule.tado-API] - ON
10:54:22.856 [INFO ] [.automation.jsscripting.rule.tado-API] - string
10:54:22.857 [INFO ] [.automation.jsscripting.rule.tado-API] - True
10:54:22.858 [INFO ] [.automation.jsscripting.rule.tado-API] - string
10:54:37.872 [WARN ] [org.openhab.core.io.net.exec.ExecUtil] - Timeout occurred when executing commandLine '[/var/lib/openhab/bin/python/tado-manual-control/venv/bin/python, /var/lib/openhab/bin/python/tado-manual-control/tado-manual-control.py, get_rate_limit_info]'
10:54:37.876 [INFO ] [.automation.jsscripting.rule.tado-API] - python output = null

I found the bug: the python script was searching for a file for which it had the relative path, but that doesn’t work. I changed it to the absolute path, and now it works.

And then I realized, I had already battled this issue and found the solutions many months ago. I now thought of writing it down somewhere, only to find I had already written it down back then. Terrible job by me.