Simple counter with python context.get('count')

I've never used the python script node so don't know what it provides.

The exec node lets you call an external script, regardless of it's language.
It is possible to pass arguments (which can include context variables) to the script.

But you may well prefer to keep the script code within Node-red rather than in an external file.
It is possible to do this using the template and exec nodes; I put together an example python script.
It's clunky but it does work.


[{"id":"23a656330a015255","type":"inject","z":"db04fd79f3a93d19","name":"Data","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"4.2","payloadType":"num","x":170,"y":620,"wires":[["1f69acd98bdddd94"]]},{"id":"0231db705087ba50","type":"template","z":"db04fd79f3a93d19","name":"Python script","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"python -c '            # -c argument - accept command inline. Must use single quotes.\nimport sys             # Process argument list\nnum1 = float(sys.argv[1])\nstr = sys.argv[2]\n\nprint (str)\nif(num1 <= 10):\n  print (num1, \"is less than 10\")\nelse :\n  print (num1, \"is greater than 10\")\n\n' {{payload}} {{flow.printme}}     # Closing single quote followed by list of arguments","output":"str","x":510,"y":620,"wires":[["d943c3197090c7a9","b3e6f7810bb068bb"]]},{"id":"d943c3197090c7a9","type":"exec","z":"db04fd79f3a93d19","command":"","addpay":"payload","append":"","useSpawn":"false","timer":"","winHide":false,"oldrc":false,"name":"exec","x":670,"y":620,"wires":[["4d1978e73e22ba4e"],["21e343f317015952"],[]]},{"id":"4d1978e73e22ba4e","type":"debug","z":"db04fd79f3a93d19","name":"output","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":810,"y":600,"wires":[]},{"id":"b3e6f7810bb068bb","type":"debug","z":"db04fd79f3a93d19","name":"Script","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":670,"y":680,"wires":[]},{"id":"21e343f317015952","type":"debug","z":"db04fd79f3a93d19","name":"error","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":810,"y":640,"wires":[]},{"id":"1f69acd98bdddd94","type":"change","z":"db04fd79f3a93d19","name":"flow.printme","rules":[{"t":"set","p":"printme","pt":"flow","to":"This\\ script\\ defined\\ in\\ Node-red.","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":330,"y":620,"wires":[["0231db705087ba50"]]}]
1 Like