Passing (msg.payload) in exec node to a python script function

#1

Hi,
I have a MQTT client that that receives the messages in the format "A0000","A0111", etc..
I have a python script named function named "rf1(code)" that receives message in the above format and executes the script accordingly.

In the node red flow i have created a node red exec function, but i am unable to pass the msg.payload and error is shown.

Exec Function - NODE RED
" python -c 'script.py; rf1(msg.payload)' " // NOT WORKING

" python -c 'script.py; rf1("A1000")' " // WORKING

In the raspberry pi lx terminal i am able to run the script.

My query is that how can i pass the message to rf1() function so that my python script can get the value ??

0 Likes

Passing parameter from node-red to python
#2

try:

"python -c 'script.py; rf1("+msg.payload+")'"
1 Like

#3

I tried the above syntax but the complete value under quotes ie. +msg.payload+ is being sent to the rf1() function.

Following message received while in debug.

String : msg.payload : string[14]

"+msg.payload+↵"

0 Likes

#4

Stick the attached flow (just a change node) before the exec node and in the exec node only enter 'python' in the 'Command' option and make sure to have 'Append' checked.

the change node uses jsonata to build the string
-c 'rf1(" & payload & ")'
being added to the python command.

[{"id":"1c52dee0.05af61","type":"change","z":"ed38ec92.4e1d2","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"\"-c 'rf1(\" & payload & \")'\"","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":180,"y":320,"wires":[["4be02acf.9e1b2c"]]}]
0 Likes

#5

In a nutshell this command has to be executed " python -c 'import rf1;rf1.rf1(" & payload & ")' " where " & payload & " will be changed to the injected value.

Now when i a change node to change the value to == " -c 'import rf1;rf1.rf1(" & payload & ")' "
and later on using the exec node to Append the expression to Python .... the followin error message is received.

Error - Traceback (most recent call last): File "<string>", line 1, in <module> NameError: name 'A0001' is not defined

The Flow is attached for reference
File Name - rf1; Module Name - rf1() ; Injected value - A0000

[{"id":"5595cb3c.49d2e4","type":"inject","z":"4022e68a.296788","name":"","topic":"String","payload":"A0001","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":200,"wires":[["2fead4c3.9d8f3c"]]},{"id":"2fead4c3.9d8f3c","type":"change","z":"4022e68a.296788","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"" -c 'import rf1;rf1.rf1(" & payload & ")'"","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":200,"wires":[["9fdf2b67.f67f68","f35dcf81.5ac7d"]]},{"id":"9fdf2b67.f67f68","type":"exec","z":"4022e68a.296788","command":"python","addpay":true,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"Rf1 Transmission","x":550,"y":240,"wires":[["207898b2.4a21b8"],["a22c7ef5.2eec1"],[]]},{"id":"f35dcf81.5ac7d","type":"debug","z":"4022e68a.296788","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":590,"y":40,"wires":[]},{"id":"207898b2.4a21b8","type":"debug","z":"4022e68a.296788","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":790,"y":120,"wires":[]},{"id":"a22c7ef5.2eec1","type":"debug","z":"4022e68a.296788","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":790,"y":260,"wires":[]}]

0 Likes

#6

I can't import your flow but try this:

[{"id":"a9eca32.283796","type":"inject","z":"cf6706ec.415a58","name":"","topic":"","payload":"A1000","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":2100,"wires":[["ac20d633.3d4338"]]},{"id":"ac20d633.3d4338","type":"function","z":"cf6706ec.415a58","name":"cmd","func":"msg.payload = \"'import script; script.rf1(\\\"\"+msg.payload+\"\\\")'\";\n\nreturn msg;","outputs":1,"noerr":0,"x":320,"y":2100,"wires":[["5e64d8d5.7fc408"]]},{"id":"5e64d8d5.7fc408","type":"exec","z":"cf6706ec.415a58","command":"python -c ","addpay":true,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"","x":460,"y":2100,"wires":[["c029036.1c54f"],[],[]]},{"id":"c029036.1c54f","type":"debug","z":"cf6706ec.415a58","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":630,"y":2100,"wires":[]}]
1 Like

#7

Use this in the change node "-c 'import rf1;rf1.rf1(\"" & payload & "\")'"

0 Likes

#8

Thanks the change node is able to transfer payload to string and subsequently append to python command.. thanks a ton for your support.

0 Likes

#9

Just for fun I tried using a templatenode:

python -c 'import rf1; rf1.rf1("{{payload}}")'
2 Likes

#10

Hi cfluring,
Your flow also worked out.. i suppose there are several ways to achieve the requirement.
Thanks.

0 Likes

#11

The Template node also works great. :slight_smile:

0 Likes

#12

Software maintenance is on the long term very important, so it 's a good practise to use the simplest solution.

1 Like

#13

Hi,
I have try the solution above but it doesn't work.
My script:
hello.py
x = "hallo"
def rf1 ():
print (x)
rf1()

Can someone help me ?

Thanks in advance

0 Likes

#14

How can we possibly know what is not right if you don't show us what you are doing and tell us what happens (or doesn't happen)?
Post a small flow showing your attempt, possibly an inject node to trigger it and then your nodes to call the script, plus the python script itself.
A question though, where do you expect the script to print to?

For posting your flow see How to share code or flow json

0 Likes

#15

At the end my plan is to create a SSL/TLS TCP connection to a XML Server with python.
First a want to try passing values to the pyton script.
msg.payload >>>> hello.py
I have read and try something but I do not find a solution for it.

[{"id":"77b57f7c.af6fe8","type":"exec","z":"6b73759.6d5388c","command":"python -c 'import /root/hello.py;rf1.rf1(\\\"\" & payload & \"\\\")'\"","addpay":true,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"","x":890,"y":240,"wires":[["ed11c859.36a05"],["1158469d.5a0889"],["d40fe16e.3f8af8"]]},{"id":"88a330d8.02b348","type":"inject","z":"6b73759.6d5388c","name":"","topic":"","payload":"juhu","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":570,"y":240,"wires":[["77b57f7c.af6fe8"]]},{"id":"1158469d.5a0889","type":"debug","z":"6b73759.6d5388c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1190,"y":240,"wires":[]},{"id":"ed11c859.36a05","type":"debug","z":"6b73759.6d5388c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1190,"y":180,"wires":[]}]

python script: (hello.py)

x = "hello"

def rf1():
  print(x)

rf1()

EDIT: thanks for the tip with the text formatting :grinning:

0 Likes

#16

All you can do with the payload in the exec node is to append (that is add it on to the end) of the command. If you want to insert it into the middle you can write a script (bash or python or whatever) that expects the payload to be the last parameter and call that script from the exec node. Then your script can do what it wants with the value from the payload,

0 Likes

#17

@Colin you can append the payload. But also then append extra parameters afterwards. Or indeed use a previous function to add anything extra to the payload before passing it in. No need for an extra level of script.

0 Likes

#18

How do you append extra parameters after the payload?

0 Likes

#19

image

0 Likes

#20

Oh that sort of extra input parameters.
Silly me.

0 Likes