Python code reading msg.payload value in Template node

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:

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
The "hello" word is recognized but "it's not defined" and I don't understand what that means :frowning:

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:

[{"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:


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

Would appreciate any help !!
Thank you in advance!

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

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

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

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

wow, It's working!! Thank you!! :tada: :confetti_ball:

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