Running a python script when Channel triggered

(dipl_ing_thomas_scha) #1

I have a channel which can be triggered. I tested it with a button and Telegram addon: every time I press the button a messge is sent to me via telegram… works like a charm.

Now I also want to execute a litle scipt once the button is pressed. I wrote it in python and created a thing via the EXEC binding. Now my question is: How can I start this script out of a rule? I just want it to run once.

rule "Pump reset"
   Channel "button:press" triggered

I have no idea what to put in the [then] block! Can anyone help me here?
Thank You

(Harry) #2

(dipl_ing_thomas_scha) #4

Maybe I am just too confused about the rules file but I tried this after reading the links:

rule "Pump reset"                                                                                                                                                                    
        Channel "button:press" triggered                                                                                                                        
        var String Pumpenreset = executeCommandLine("sudo python /etc/openhab2/scripts/",10000)                                                                                 

It seems not to work as the script is not executed (command linde with sudo -u openhab python… it works). The whole Syntax looks strange. I would expect the new line to just set a variable, but not activly doing something. Is there no call or something like this?

(Harry) #5

Are there any entrys in the log?
Is the Exec-Binding installed?
Try without sudo.
Does the script have a return value?

(dipl_ing_thomas_scha) #6

No return Values. Just a script that restarts a pump. Maybe I will add some return codes later. Exec Binding is installed. Without sudo the script does not work, I think I have a right-management problem here. Oh and I have no log entries.
Does the rules need a restart of the server? In OH1 that was not neccesary…

(Harry) #7

There sould be entrys in openhab.log.
I think sudo is the problem. Try without and change the script to see, if is works.
It doesn’t need a restart, but a restart after changes could be a good option.
Without return value try

executeCommandLine("python /etc/openhab2/scripts/")

(Josar) #8

Basically first get the script to execute in command line without sudo, find out which permissions you need and add openhab to the specific group. Or enable sudo for this one command, which would be the last solution.

Additional add the shebang to your script and use it like this.

sudo u- openhab  /etc/openhab2/scripts/

(dipl_ing_thomas_scha) #9

A fresh reboot did the trick! Thank you. And I also checked the permissions and do no longer need sudo. Thank you all.