Executing Python scripts based on node templates

I was searching for documentation on how to write a Python script using the template node and executing that script with the exec node.

This flow is an example of that - using only built in Node-RED nodes.

I would have liked to skip the part, where I am writing the file to disk - but I was unable to get this result, when running the exec node in spawn mode.

[{"id":"d7276cd2.d6f55","type":"tab","label":"pythontest","disabled":false,"info":""},{"id":"a7d4e707.fd8a18","type":"inject","z":"d7276cd2.d6f55","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":240,"wires":[["97c92fce.dd6f6"]]},{"id":"97c92fce.dd6f6","type":"template","z":"d7276cd2.d6f55","name":"","field":"payload","fieldType":"msg","format":"python","syntax":"plain","template":"import time\n\nwhile True:\n    print(time.ctime())\n    time.sleep(10)\n","output":"str","x":320,"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":510,"y":240,"wires":[["14057906.c24237"]]},{"id":"14057906.c24237","type":"exec","z":"d7276cd2.d6f55","command":"python3 pythontest.py","addpay":false,"append":"","useSpawn":"true","timer":"","oldrc":false,"name":"","x":740,"y":240,"wires":[["a33c536c.9900e"],[],[]]},{"id":"a33c536c.9900e","type":"debug","z":"d7276cd2.d6f55","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":970,"y":240,"wires":[]}]

Flow Info

Created 1 year, 11 months ago
Rating: not yet rated

Node Types

Core
  • debug (x1)
  • exec (x1)
  • file (x1)
  • inject (x1)
  • tab (x1)
  • template (x1)

Tags

  • template
  • exec
  • python
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option