Passing 2 values to python script using exec node

Hello I have a simple questions pls,
I've got a python script which is simply doing addition:

#!/usr/bin/python3
import sys
def ad(a, b):
print(a+b)
return a+b
ad(int(sys.argv[1]), int(sys.argv[2]))

so in node-red i am able to call this python script by using the exec node, CMD line in exec node is: /root/add_p.py 3 4
so my question is: instead of simply passing constant 3 and 4, how to pass the parameter with, say, msg.payload.p1 and msg.payload.p2?
Thank you very much for the help.

If you set the exec node to "append payload as arguments", it will require your payload in the format of 3 4. There's several ways you can get there, but my idea would be to put a change node (or function node) in front of it and create a new payload in that format. An example would be a function node with the following:

msg.payload = `${msg.payload.p1} ${msg.payload.p2}`;
return msg;

This syntax is the ES6 template syntax, that allows you to insert variables directly into a combined string value.

Hi afelix, thanks for the reply. But i got error from the python script: ValueError: invalid literal for int() with base 10: 'undefined'. My inject node is: {"payload":{"p1":11,"p2":22}}.

start by adding debugging to your python script. Check for the incoming arguments:

print(type(sys.argv[1]), sys.argv[1])
print(type(sys.argv[2]), sys.argv[2])

if I uncheck append msg.payload and CMD is: /root/add_p.py 3 4
I got:
7
<class 'str'> 3
<class 'str'> 4

if I check append msg.payload and CMD is: /root/add_p.py
I got:
Command failed: /root/add_p.py undefined undefined
Traceback (most recent call last):
File "/root/add_p.py", line 9, in
ad(int(sys.argv[1]), int(sys.argv[2]))
ValueError: invalid literal for int() with base 10: 'undefined'

Can you add a debug node behind the node that's feeding into your exec node, and show how you've the exec node configured?

Edit: I'm seeing the same behaviour with your script in my test flow, going to take a closer look at what's happening here... I feel like I'm missing something obvious

Mine was caused by wiring it wrong. Did it again and it works just fine. Can you please upload your flow using the </> button? Looks like something else is going on.

msg.payload needs to be a simple string containing the two values, so the payload going into the exec node must be just "3 4"
As @afelix suggested, feed what you are sending to the exec node into a debug node and see what it shows.

1 Like

debug node after inject node shows:
{"payload":{"p1":11,"p2":22}}
debug node after function node shows:
undefined undefined

function node looks like:
msg.payload = ${msg.payload.p1} ${msg.payload.p2};
return msg;

Please use the </> button to format your code when posting some. I'm not 100% sure if your function is correct now because of how the formatting software of the forum messed with it. Can you confirm it looks like

msg.payload = `${msg.payload.p1} ${msg.payload.p2}`;
return msg;

And can you confirm that the debug node after inject is set to "complete message" or to "msg.payload". If complete message, I'm about to run out of options as the exact setup works here without an issue. If msg.payload, you would have payload in it twice...

This part suggests that what goes in the function as msg.payload.p1 and msg.payload.p2 are already both undefined. Please check everything again to make sure it is correct.

what nodejs version do you use?

hi, yes, the function code is the same as yours, i just copy it. And in inject node I chose {}JSON, the content is:

{
    "payload": {
        "p1": 11,
        "p2": 22
    }
}

Well, there's your reason. What you inject in {} Json mode, is put into msg.payload. Try instead injecting the following object:

{
    "p1": 11,
    "p2": 22
}

What is happening here with your previous post is that you create a message object from your inject that looks like this:

{
    "topic": "",
    "_msgid": ".....",
    "payload": {
        "payload": {
            "p1": 11,
            "p2": 22
        }
    }

Take a look at the Working with Messages part of the documentation and see if you understand the difference between the formats described here. If not, feel free to ask for more information :slight_smile:

yeah I made a mistake here :joy:
again, thank you so much for helping so fast :slightly_smiling_face:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.