Running a python script once a channel is triggered

Don’t think so; it more usually looks something like

If your trigger is actually fired, you’ll probably see it happening in your events.log

Would I use the specific IDs for the binding, thing, and channel?

Whether you defined or discovered your trigger channel by text file or UI, you can look at it under your Thing in the UI and see what the UID is that you are looking for.
This is the UID that gets reported in your events.log.

Could I send the log info over to you? There are a few errors but I’m not sure what’s going on

You can see what’s relevant to your expected trigger, just paste an excerpt here. Remember, we don’t even know what binding you are dealing with.

The binding I’m working with is an MQTT Broker, I have an ESP32 connected to the broker via MQTT and then when a certain payload is sent from the ESP to the Broker, I want the script to activate (For testing purposes I’ve just been running the rule to see if it would work).

This is the log information:

19:28:17.651 [ERROR] [.internal.handler.ScriptActionHandler] - Script execution of rule with UID '60399c6089' failed:  ___ rule  ___ TEST
     Channel "mqtt:b ___ roker:0e ___ 6f031da3:PayloadToText" triggered START ___
    var String Command = executeCommandLine("sudo python /etc/openhab/scripts/", 10000)
   1. The method or field rule is undefined; line 1, column 0, length 4
   2. The method or field TEST is undefined; line 1, column 5, length 4
   3. The method or field when is undefined; line 2, column 10, length 4
   4. The method or field Channel is undefined; line 3, column 20, length 7
   5. The method or field triggered is undefined; line 3, column 67, length 9
   6. The method or field START is undefined; line 3, column 77, length 5
   7. The method or field then is undefined; line 4, column 83, length 4
   8. The method or field end is undefined; line 6, column 186, length 3
   9. Type mismatch: cannot convert from int to String; line 5, column 179, length 5
   10. Type mismatch: cannot convert from void to String; line 5, column 113, length 72
   11. This expression is not allowed in this context, since it doesn't cause any side effects.; line 3, column 28, length 38

What are you working with, a UI entered rule? You can’t just paste in a whole rule from file format into the ‘script’ section. You use the UI to set up the trigger, then put the operative part in the script section.

I understand, wow that was really smooth brain of me to not realize. What I’ve done now is copied that code into it’s own rule. This is what I am working with now.

rule "payloadToTxt"
    Channel "PayloadtoText" Triggered
    logInfo("payloadToTxt.rules", "Making file according to payload")
    val result = executeCommandLine("python sudo /etc/openhab/scripts/", 10000)
    logInfo("payloadToTxt.rules", "payload.txt updated")

I am not receiving any log info either, not sure what the issue is.

I’m not totally convinced trigger channels do produce an event log until they are linked to something, like a rule.

But the same observation as earlier applies - it is really unlikely your trigger channel UID is “PayloadtoText” and you should look into your UI to see what you have set up.
Your previous attempt had a mangled version of
which looks rather more likely for a UID.

You do understand it is not usual to use the trigger channel option of MQTT binding? While that is usable for topics without payloads, the more conventional approach is to link the channel to an Item that is to be updated with the payload.
A rule can then listen for Item update.

I’ve replaced the PayloadtoText with an actual UID within the script, after testing still nothing appears in the log and the function does not work. Do I need to turn the script into a rule or will defining it how I did work? This is what I am working with now, thank you for your help by the way. I would have been very lost.

rule "payloadToTxt"
    Channel "mqtt:broker:0e6f031da3:PayloadToText" Triggered
    logInfo("payloadToTxt.rules", "Making file according to payload")
    val result = executeCommandLine("python sudo /etc/openhab/scripts/", 10000)
    logInfo("payloadToTxt.rules", "payload.txt updated")

Maybe your MQTT channel is not set up to be trigger type. It remains a secret.

I’ve no real idea how you defined it, you haven’t said. What did you do, paste it into a file? What’s the filename and folder? Does your openhab.log show that it has loaded this file without error?

The MQTT channel is setup to be a trigger type, sorry for not saying that earlier. As for defining it, I’ve just written that code within the script part of the UI.

If you say so.

But that’s a complete rule, suitable for putting in an xxx.rules file without any UI involvement at all.

If you use the UI, then you must use the UI to define the rule trigger.
The only bit of the shown rule suitable to be included is the stuff between ‘then’ and ‘end’ exclusive, to be put in the script section and be marked as type DSL.

EDIT - depending on the OH3 version in use, this might still be relevant -

I do not see it mentioned just to make sure: you are on OH2, right ?

sudo has to be the first entry in your executeCommandLine statement and python the second one:
val result = executeCommandLine("sudo python /etc/openhab/scripts/", 10000)

I am on OH3, sorry for not mentioning. Okay so I’ve got it working to an extent now, the WHEN part of the rule is in the UI and the THEN part is a script I’ve written for the second part, this is it below:

logInfo("payloadToTxt.rules", "Making file according to payload")
executeCommandLine("sudo python /home/rook/")
logInfo("payloadToTxt.rules", "payload.txt updated")

The rule is working however, this is the log message I am receiving:

12:51:43.813 [WARN ] [] - Error occurred when executing commandLine '[sudo python /home/rook/]' Cannot run program "sudo python /home/rook/": error=2, No such file or directory

This should not be an error given the file is indeed in this directory so I think there is something wrong with my THEN part of the rule. Not sure what I am missing.

The executeCommand function is not running in an environment that has access to the full path data of the regular environment. It’s not the /home/rook/ that can’t be found but the python alias. You will have to give the command the full path to the python executable.

Your python executable is probably located at /usr/bin/python but you can check from the terminal:

$ type python
python is /usr/bin/python

OH3 uses a differnt syntax for executeCommandLine than OH2 does.
As long as you do not need to wait for the execution run


In case it does not work use the full path for the executables.

So once I give it the full path to the python executable, how does it determine what py script I want it to run? I made the by writing it to a text file and saving it as .py, it still works when executed in the command line manually.

In this case the command python is just a shortcut that is automatically made when you install python. The actual file that runs python has a location just like everything else. The issue is that in linux, not every user has access to the same set of these shortcuts for commands. Your instance of OH is running under a certain user, which depending on your current setup may or may not be the same user as what you see when you open a terminal. If OH is running under a different user: 1) you may in fact have different behaviors between commands that you test in a terminal and commands that are run from OH and 2) instead of just using python to run your script you may have to use the full path. Nothing else about the command changes, just the form of the python reference:
sudo /usr/bin/python /home/rook/

As Wolfgang_S says, it may be that your only problem is the formatting of the executeCommand which changed in OH3. But, if you try that correct formatting and it still doesn’t work, then you likely have to use the full path for the python executable:

1 Like