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

**URL:** <https://discourse.nodered.org/t/simple-counter-with-python-context-get-count/75709>\
**Category:** General\
**Created:** [24 February 2023 18:40 UTC](https://discourse.nodered.org/t/simple-counter-with-python-context-get-count/75709 "2023-02-24T18:40:01Z")\
**Posts on this page:** 1\
**Showing post:** 12

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [28 February 2023 14:58 UTC](https://discourse.nodered.org/t/simple-counter-with-python-context-get-count/75709/12 "2023-02-28T14:58:57Z")

</div>

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.

 ![Untitled 3](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/f/2f3fb530a4fb6906f1d4d6730e4e0561b5c00c3d.jpeg)  
 ![Untitled 2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/3/53260920b016c61007d51dccc632e40ec497c16d.jpeg)

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

```

---

_[View the full topic](https://discourse.nodered.org/t/simple-counter-with-python-context-get-count/75709)._
