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

**URL:** <https://discourse.nodered.org/t/passing-msg-payload-in-exec-node-to-a-python-script-function/3200>\
**Category:** General\
**Created:** [17 September 2018 07:16 UTC](https://discourse.nodered.org/t/passing-msg-payload-in-exec-node-to-a-python-script-function/3200 "2018-09-17T07:16:57Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![pandeyprakhar00](https://avatars.discourse-cdn.com/v4/letter/p/a9a28c/32.png) [@pandeyprakhar00](https://discourse.nodered.org/u/pandeyprakhar00)\
**Post date:** [17 September 2018 07:16 UTC](https://discourse.nodered.org/t/passing-msg-payload-in-exec-node-to-a-python-script-function/3200/1 "2018-09-17T07:16:57Z")

</div>

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 ??

---

<div class="post-metadata">

**Author:** ![cflurin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cflurin/32/29_2.png) [@cflurin](https://discourse.nodered.org/u/cflurin)\
**Post date:** [17 September 2018 08:13 UTC](https://discourse.nodered.org/t/passing-msg-payload-in-exec-node-to-a-python-script-function/3200/2 "2018-09-17T08:13:58Z")

</div>

try:

```auto
"python -c 'script.py; rf1("+msg.payload+")'"

```

---

<div class="post-metadata">

**Author:** ![pandeyprakhar00](https://avatars.discourse-cdn.com/v4/letter/p/a9a28c/32.png) [@pandeyprakhar00](https://discourse.nodered.org/u/pandeyprakhar00)\
**Post date:** [17 September 2018 10:51 UTC](https://discourse.nodered.org/t/passing-msg-payload-in-exec-node-to-a-python-script-function/3200/3 "2018-09-17T10:51:36Z")

</div>

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+↵"**

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [17 September 2018 11:30 UTC](https://discourse.nodered.org/t/passing-msg-payload-in-exec-node-to-a-python-script-function/3200/4 "2018-09-17T11:30:46Z")

</div>

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.

```auto
[{"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"]]}]

```

---

<div class="post-metadata">

**Author:** ![pandeyprakhar00](https://avatars.discourse-cdn.com/v4/letter/p/a9a28c/32.png) [@pandeyprakhar00](https://discourse.nodered.org/u/pandeyprakhar00)\
**Post date:** [17 September 2018 12:21 UTC](https://discourse.nodered.org/t/passing-msg-payload-in-exec-node-to-a-python-script-function/3200/5 "2018-09-17T12:21:38Z")

</div>

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":[]}]

---

<div class="post-metadata">

**Author:** ![cflurin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cflurin/32/29_2.png) [@cflurin](https://discourse.nodered.org/u/cflurin)\
**Post date:** [17 September 2018 12:53 UTC](https://discourse.nodered.org/t/passing-msg-payload-in-exec-node-to-a-python-script-function/3200/6 "2018-09-17T12:53:08Z")

</div>

I can't import your flow but try this:

```auto
[{"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":[]}]

```

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [17 September 2018 13:04 UTC](https://discourse.nodered.org/t/passing-msg-payload-in-exec-node-to-a-python-script-function/3200/7 "2018-09-17T13:04:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pandeyprakhar00](https://avatars.discourse-cdn.com/v4/letter/p/a9a28c/32.png) [@pandeyprakhar00](https://discourse.nodered.org/u/pandeyprakhar00)\
**Post date:** [17 September 2018 13:29 UTC](https://discourse.nodered.org/t/passing-msg-payload-in-exec-node-to-a-python-script-function/3200/8 "2018-09-17T13:29:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cflurin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cflurin/32/29_2.png) [@cflurin](https://discourse.nodered.org/u/cflurin)\
**Post date:** [17 September 2018 13:47 UTC](https://discourse.nodered.org/t/passing-msg-payload-in-exec-node-to-a-python-script-function/3200/9 "2018-09-17T13:47:32Z")

</div>

Just for fun I tried using a `template`node:

```auto
python -c 'import rf1; rf1.rf1("{{payload}}")'

```

---

<div class="post-metadata">

**Author:** ![pandeyprakhar00](https://avatars.discourse-cdn.com/v4/letter/p/a9a28c/32.png) [@pandeyprakhar00](https://discourse.nodered.org/u/pandeyprakhar00)\
**Post date:** [17 September 2018 14:06 UTC](https://discourse.nodered.org/t/passing-msg-payload-in-exec-node-to-a-python-script-function/3200/10 "2018-09-17T14:06:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pandeyprakhar00](https://avatars.discourse-cdn.com/v4/letter/p/a9a28c/32.png) [@pandeyprakhar00](https://discourse.nodered.org/u/pandeyprakhar00)\
**Post date:** [17 September 2018 14:09 UTC](https://discourse.nodered.org/t/passing-msg-payload-in-exec-node-to-a-python-script-function/3200/11 "2018-09-17T14:09:28Z")

</div>

The Template node also works great. 🙂

---

<div class="post-metadata">

**Author:** ![cflurin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cflurin/32/29_2.png) [@cflurin](https://discourse.nodered.org/u/cflurin)\
**Post date:** [17 September 2018 14:10 UTC](https://discourse.nodered.org/t/passing-msg-payload-in-exec-node-to-a-python-script-function/3200/12 "2018-09-17T14:10:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Frank](https://avatars.discourse-cdn.com/v4/letter/f/3d9bf3/32.png) [@Frank](https://discourse.nodered.org/u/Frank)\
**Post date:** [2 December 2018 10:24 UTC](https://discourse.nodered.org/t/passing-msg-payload-in-exec-node-to-a-python-script-function/3200/13 "2018-12-02T10:24:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [2 December 2018 10:50 UTC](https://discourse.nodered.org/t/passing-msg-payload-in-exec-node-to-a-python-script-function/3200/14 "2018-12-02T10:50:32Z")

</div>

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](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506/3)

---

<div class="post-metadata">

**Author:** ![Frank](https://avatars.discourse-cdn.com/v4/letter/f/3d9bf3/32.png) [@Frank](https://discourse.nodered.org/u/Frank)\
**Post date:** [2 December 2018 18:30 UTC](https://discourse.nodered.org/t/passing-msg-payload-in-exec-node-to-a-python-script-function/3200/15 "2018-12-02T18:30:08Z")

</div>

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.

```auto
[{"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)

```auto
x = "hello"

def rf1():
  print(x)

rf1()

```

EDIT: thanks for the tip with the text formatting 😀

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [2 December 2018 19:48 UTC](https://discourse.nodered.org/t/passing-msg-payload-in-exec-node-to-a-python-script-function/3200/16 "2018-12-02T19:48:32Z")

</div>

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,

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [2 December 2018 20:24 UTC](https://discourse.nodered.org/t/passing-msg-payload-in-exec-node-to-a-python-script-function/3200/17 "2018-12-02T20:24:32Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [2 December 2018 21:40 UTC](https://discourse.nodered.org/t/passing-msg-payload-in-exec-node-to-a-python-script-function/3200/18 "2018-12-02T21:40:39Z")

</div>

> [@dceejay](#):
>
> But also then append extra parameters afterwards.

How do you append extra parameters after the payload?

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [2 December 2018 23:13 UTC](https://discourse.nodered.org/t/passing-msg-payload-in-exec-node-to-a-python-script-function/3200/19 "2018-12-02T23:13:03Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/2/2a85ac0224c852e4803620b4b34d2b4bff01275d.png)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [3 December 2018 17:43 UTC](https://discourse.nodered.org/t/passing-msg-payload-in-exec-node-to-a-python-script-function/3200/20 "2018-12-03T17:43:39Z")

</div>

Oh _that_ sort of extra input parameters.  
Silly me.
