# Python code reading msg.payload value in Template node

**URL:** <https://discourse.nodered.org/t/python-code-reading-msg-payload-value-in-template-node/43832>\
**Category:** General\
**Created:** [7 April 2021 20:03 UTC](https://discourse.nodered.org/t/python-code-reading-msg-payload-value-in-template-node/43832 "2021-04-07T20:03:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fatima221](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/fatima221/32/39935_2.png) [@Fatima221](https://discourse.nodered.org/u/Fatima221)\
**Post date:** [7 April 2021 20:03 UTC](https://discourse.nodered.org/t/python-code-reading-msg-payload-value-in-template-node/43832/1 "2021-04-07T20:03:19Z")

</div>

Hello,

I recently started using Node-red, it is wonderful and hoping to get the hang of stuff here!

So, I was trying to connect a 16X2 LCD with Raspberry pi and write to it from Node-red. unfortunately, I could not find any node that can help me with that (all nodes in the palette are not working with me for some reason, either out-dated or not compatible with my LCD type).

So, I found that I can make it work, but by running a python script from the **exec** and **template** nodes. I write my python script in a template node, save it to a file, and then execute that file, like what you see in this flow:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/1/01590f3134fddf440b2ff7c65e2157737688cc51.png)

Now, my problem is that I want to send a message such that it is read by the python script and then displayed in the LCD. But.. I don't know how to read that message in the template node, it keeps on giving me error messages, even though it displays the correct message.  
To put it simple, I made a flow where I inject "hello" word as a msg.payload to the template node; all I am trying to do is read the msg.payload value and print it to the debug window. I am getting the following Error:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/0/b0744a21be98d40003453c560477f12fd576c57b.png)  
The "hello" word is recognized but "it's not defined" and I don't understand what that means ☹

P.S. In the template node, the Format is: Mustache, and the Output is set as: plain text. dropping this If it is relevant.

this is the flow code:

```auto
[{"id":"a7d4e707.fd8a18","type":"inject","z":"d7276cd2.d6f55","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"hello","payloadType":"str","x":770,"y":240,"wires":[["97c92fce.dd6f6"]]},{"id":"97c92fce.dd6f6","type":"template","z":"d7276cd2.d6f55","name":"","field":"payload","fieldType":"msg","format":"python","syntax":"mustache","template":"\nmsg = {{payload}}\nwhile True:\n print(msg)\n time.sleep(10)\n","output":"str","x":920,"y":240,"wires":[["1273e63.74b5c1a"]]},{"id":"1273e63.74b5c1a","type":"file","z":"d7276cd2.d6f55","name":"","filename":"pythontest.py","appendNewline":false,"createDir":false,"overwriteFile":"true","encoding":"none","x":1070,"y":240,"wires":[["14057906.c24237"]]},{"id":"14057906.c24237","type":"exec","z":"d7276cd2.d6f55","command":"python3 pythontest.py","addpay":true,"append":"","useSpawn":"true","timer":"","oldrc":false,"name":"","x":1260,"y":240,"wires":[["a33c536c.9900e"],["dce98493.4675b"],[]]},{"id":"a33c536c.9900e","type":"debug","z":"d7276cd2.d6f55","name":"Output","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1530,"y":200,"wires":[]},{"id":"dce98493.4675b","type":"debug","z":"d7276cd2.d6f55","name":"Error message","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1560,"y":240,"wires":[]}]

```

and this is the python script:

```auto

msg = {{payload}}
while True:
    print(msg)
    time.sleep(10)

```

Would appreciate any help !!  
Thank you in advance!

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [7 April 2021 20:31 UTC](https://discourse.nodered.org/t/python-code-reading-msg-payload-value-in-template-node/43832/2 "2021-04-07T20:31:33Z")

</div>

You are appending msg.payload to your command in the exec node.  
So your command ends up being

```auto
python3 pythontest.pymsg = hello
while True:
    print(msg)
    time.sleep(10)

```

Also in your template you need to put quotes around the payload

```auto
msg = '{{payload}}'
while True:
    print(msg)
    time.sleep(10)

```

---

<div class="post-metadata">

**Author:** ![Fatima221](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/fatima221/32/39935_2.png) [@Fatima221](https://discourse.nodered.org/u/Fatima221)\
**Post date:** [7 April 2021 20:45 UTC](https://discourse.nodered.org/t/python-code-reading-msg-payload-value-in-template-node/43832/3 "2021-04-07T20:45:21Z")

</div>

wow, It's working!! Thank you!! 🎉 🎊

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [21 April 2021 20:46 UTC](https://discourse.nodered.org/t/python-code-reading-msg-payload-value-in-template-node/43832/4 "2021-04-21T20:46:15Z")

</div>

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