var antwoord = actions.Exec.executeCommandLine(Duration.ofSeconds(5), 'ls')
console.log(antwoord)
But:
[ERROR] [b.automation.script.file.testRegel.js] - Failed to execute rule Te-negeren-test-dfe74dd6-c797-421c-9138-0dade3bad567: ReferenceError: "Duration" is not defined: ReferenceError: "Duration" is not defined
at execute (testRegel.js:16)
at doExecute (/node_modules/openhab.js:2)
Strange…?
I did manually install nodejs earlier today, in order to make a JS file run outside of openHAB. Did I break something by doing that? But removing it might just make matters worse, who knows?
Tiny errors like these are hard to spot when you aren’t used to looking for them.
Try separating the command and the argument. python is the command and the path to the script is the argument. See Actions | openHAB. Sadly the docs for JS Scripting shows how to call the action, but the actual docs for the actions are elsewhere. Be aware that the docs are Rules DSL centric so there might need to be some translation (e.g. Duration verses time.Duration.
Remember that openHAB is executing this script as user openhab, not the user you log in as. User openhab needs to have execute permissions on the script.
Note, /etc/openhab/automation/python is intended to be where one puts Jython rules code, not random personal Python scripts. You can put those just about anywhere but it can be dangerous to put them in a folder that OH expects to contain something else.
f: /home/erik/persoonlijkeToevoegingenOpenHAB/
drwxr-xr-x root root /
drwxr-xr-x root root home
drwx------ erik erik erik
drwxrwxr-x openhab openhab persoonlijkeToevoegingenOpenHAB
So long as you are not using a folder that has some other purpose it’s fine. Beyond that what’s most important is that the path makes sense to you.
I would probably create my own separate folder, maybe /etc/openhab/bin so that everything associated with OH are in OH folders.
Another option is to put them in /var/lib/openhab/bin. as /var/lib/openhab is the openhab user’s home directory. Then you can omit the full path and just use bin/tractive.py in the configs.
It’s all up to you.
The folder has OK permissions but what about the filetractive.py? On Linux permissions are not inherited. Each file has it’s own set of permissions.
This seemed to be the easiest option. It also fixed the permission problem.
But…
[INFO ] [b.automation.script.file.testRegel.js] - Traceback (most recent call last):
File "/var/lib/openhab/bin/python/tractive.py", line 3, in <module>
from aiotractive import Tractive
ModuleNotFoundError: No module named 'aiotractive'
I installed the aiotractive library as user erik, but that’s not enough, it seems… Should I sudo pip install aiotractive? I’ve read that sudo pip really shouldn’t be used (and that even pip is to be avoided, but I took my chances)…
I think to remember that such libararies are installed locally to the user that runs the install if you don’t use sudo.So the libs are installed relative to user erik while the script is executed with permissions of user openhab.
I think best would be to install
Nope. As @Wolfgang_S indicates, installing a Python library as a regular user installs it only for that user. You can install it as root which globally installs the library but if you are on bookworm that’s been disabled. So you’ll either need to install it as user openhab or create a virtualenv like @Wolfgang_S describes.
To install it as the openHAB user use sudo -u openhab pip install aiotractive
Besides the supply chain issues with package managers like pip and npm, are there other reasons you’ve read not to use pip?
The openhab user account quite deliberately does not have a password nor does it have a shell. You cannot log in as the openhab user. But you can use sudo to run a command as any user.
OK, well it’s important to understand that the reply is in the context of Linux Mint, with some relevance to Ubuntu. The main takeaway is to not change the global Python environment because apt is written in and depends on Python (yum is too all you Fedora users out there).
Using pip to install libraries for a regular user should not cause problems and using a vritualenv is best practice and using pip there is OK too.
shouldn’t be any difference on doing that for user openhab …
Main difference is that user openhab does not have a shell but with sudo you can get one for openhab.
With the command
sudo su -s /bin/bash openhab
you get a shell for user openhab. From that I think you should be able to continue to build the virtual environment - or use habapp which also install one.
The only thing I have to add to @Wolfgang_S’s answer is that your venv will have it’s very own copy of python so make sure to use that copy of the python to run the script. I think that’s sufficient to make it use the venv. If not, you may need to cd to the folder first in which case you’ll need a slightly more complicated command line.