Running a python script once a channel is triggered

Hello everyone,

I have a channel that is being triggered and once this occurs, I want a python script I had made to execute it’s function. Right now the script is

rule "TEST"
when
     Channel "PayloadToText" triggered
then
    var String Command = executeCommandLine("sudo python /etc/openhab/scripts/command.py", 10000)
end

I have the EXEC Binding installed but to my knowledge it is not exactly linked to anything since I am confused about the documentation. I assume why it is not working has something to do with that. Any advice / help would be much appreciated. Thank you!

First things first - is this specifically a Trigger Channel?

From the docs:

Some add-ons provide trigger channels…Your rules can take actions based upon trigger events generated by these trigger channels.

Hi Conner, welcome to the forums.

There are several different possibilities for why your script isn’t executing and there’s just not enough information in your first post to let us help you figure it out. Check out the following post for examples of the information we might need to get to the bottom of this.

Yes this is indeed a trigger channel

Don’t think so; it more usually looks something like
some_binding:a_thing:a_channel

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
when
     Channel "mqtt:b ___ roker:0e ___ 6f031da3:PayloadToText" triggered START ___
then
    var String Command = executeCommandLine("sudo python /etc/openhab/scripts/command.py", 10000)
end
   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"
when
    Channel "PayloadtoText" Triggered
then
    logInfo("payloadToTxt.rules", "Making file according to payload")
    val result = executeCommandLine("python sudo /etc/openhab/scripts/command.py", 10000)
    logInfo("payloadToTxt.rules", "payload.txt updated")
end

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
mqtt:broker:0e6f031da3:PayloadToText
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"
when
    Channel "mqtt:broker:0e6f031da3:PayloadToText" Triggered
then
    logInfo("payloadToTxt.rules", "Making file according to payload")
    val result = executeCommandLine("python sudo /etc/openhab/scripts/command.py", 10000)
    logInfo("payloadToTxt.rules", "payload.txt updated")
end

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/command.py", 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/command.py")
logInfo("payloadToTxt.rules", "payload.txt updated")

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

12:51:43.813 [WARN ] [org.openhab.core.io.net.exec.ExecUtil] - Error occurred when executing commandLine '[sudo python /home/rook/command.py]'
java.io.IOException: Cannot run program "sudo python /home/rook/command.py": 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.