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:
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}}.
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.
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
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.
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