[SOLVED] Doesn't run script in rules

Hello! I have a problem. Openhab doesn’t run script in rules.
This is my rule.

   Item mihome_sensor_magnet_158d0002e29d73_isOpen changed
to OPEN then
        if (mihome_gateway_7c49eb88f15c_enableSound.state == ON){
        sendTelegram("bot1", "test")
        executeCommandLine("python3 /scripts/alarm.py")}
end

Rule send message to telegram.
In log i see

2019-11-01 10:26:02.356 [INFO ] [lipse.smarthome.io.net.exec.ExecUtil] - executed commandLine ‘python3 /scripts/alarm.py’

In another openhab This script and rules works… I reinstaled Openhab. And it doesn’t work((
What could be the problem?
I run script manualy it works…

Please use code fences

/scripts/alarm.py
Is that the full path to the script?

Is the space in the path behind python3 in all openHAB installations correct?

Edit: Of course the space is correct as /scripts/alarm.py is the parameter for the command python3. Note to myself: Don’t write a post before the first coffee!

Depends what os you are running
It is better to provide the full path for python and the script

Yes it full path. I use armbian for orange pi. also in rules i run bash script that send sms if alarm. it works)

   Item mihome_sensor_magnet_158d0002e29d73_isOpen changed
to OPEN then
        if (mihome_gateway_7c49eb88f15c_volume.state == 100){
        sendTelegram("bot1", "Сработала сигнализация")
        executeCommandLine("bash /home/orange/bot/sms.sh")}
end

No it isn’t
Could it be: /etc/openhab2/scripts/alarm.py by any chance?

No i made dir /scripts and gave permissions to script. I have to put script to this path? /etc/openhab2/scripts/alarm.py
But how it worked in another openhab and how work another script?) Thank you i will try

You shouldn’t add directories to the root of a linux system.
Add your scripts to the openHAB scripts directory or (better) add a scripts directory to your user home directory instead

in first time i created script in home directory) But it didn’t work and i tryed to create in root directory)
script that send sms I created in home directory. i created in /etc/openhab2/scripts It doesn’t work

Then do some debugging:

   Item mihome_sensor_magnet_158d0002e29d73_isOpen changed to OPEN
then
    if (mihome_gateway_7c49eb88f15c_enableSound.state == ON) {
        sendTelegram("bot1", "test")
        var testString = executeCommandLine("python3 /scripts/alarm.py")
        logInfo("TEST", testString)
    }
end

and where can I view logs? /var/log/openhab2/openhab.log?

That’s the one…

2019-11-01 16:37:13.355 [INFO ] [lipse.smarthome.io.net.exec.ExecUtil] - executed commandLine 'python3 /scripts/alarm.py' 2019-11-01 16:37:13.362 [INFO ] [.eclipse.smarthome.model.script.TEST] - null
this is logs

Try using this form of executeCommandLine

var testString = executeCommandLine("python3 /scripts/alarm.py", 5000)

You’d get more details in your log if you put org.eclipse.smarthome.io.net.exec.ExecUtil into debug.

Now i see log
File "/home/orange/bot/alarm.py", line 2, in <module> import requests ModuleNotFoundError: No module named 'requests'
but I have this module. I run this script from shell and it works. But if openhab run it doesn’t work((

How can i do it?

Thank you all. I solved the poblem. I installed requests by command pip3 install reauests. When I install by sudo apt install python3-requests it begin to work))

Please tick tje solution. Thanks

Its two solutions) but I can tick only one( Thank you))))))

Hello,

sorry for bumping again into this issue, but I’ve a slimiar problem obviously in the context of python and importing libraries in the context of user openhab.

I was reading a lot, but can’t find an good solution. I want to avoid using sudo pip to install the libraries for various good reasons. On the other hand side it seems python isn’t able to find the libraries once not installed system wide by sudo pip or apt.

My problem:
I would like to start a python script using python-openhab library and various others in a rule just once and I know how to do this thanks to the doco.

I’m perfectly able to run the script to execute with the normal USER (having a home directory where the libraries are located in */.local), but once I try to check whether the script executes as user openhab, by “sudo -u openhab” I get the following reply:

USER@Machine:~/.local/lib/python3.6/site-packages$ sudo -u openhab /usr/bin/python3.6  /etc/openhab2/scripts/optimizer_6.1.py http://127.0.0.1:8080/rest --logfile /var/log/openhab2/optimizer.log --cycletime 90
Traceback (most recent call last):
  File "/etc/openhab2/scripts/optimizer_6.1.py", line 6, in <module>
    from openhab import OpenHAB
ModuleNotFoundError: No module named 'openhab'
USER@Machine:~/.local/lib/python3.6/site-packages$ 

Is there any way to come around installing all libraries with sudo pip? Or using any elevated user rights for the user openhab?

Thanks in advance and BR

Andreas