# 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:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![superlogan72](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/superlogan72/32/74680_2.png) [@superlogan72](https://discourse.nodered.org/u/superlogan72)\
**Post date:** [24 February 2023 18:40 UTC](https://discourse.nodered.org/t/simple-counter-with-python-context-get-count/75709/1 "2023-02-24T18:40:01Z")

</div>

I have seen one example of of simple counter in js  
var count=context.get('count')  
there is similar function that i can use in python with node red

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [24 February 2023 21:54 UTC](https://discourse.nodered.org/t/simple-counter-with-python-context-get-count/75709/2 "2023-02-24T21:54:24Z")

</div>

I am not sure what you are asking. Node red does not use Python. You can call a python script using an exec node, but it does not appear that is what you want. Please describe what you want help with again.

Also your example does not count, it simply fetches the current value from context. If you want it to count you would need to increment it and then store it back in the context.

---

<div class="post-metadata">

**Author:** ![superlogan72](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/superlogan72/32/74680_2.png) [@superlogan72](https://discourse.nodered.org/u/superlogan72)\
**Post date:** [27 February 2023 09:14 UTC](https://discourse.nodered.org/t/simple-counter-with-python-context-get-count/75709/3 "2023-02-27T09:14:25Z")

</div>

Hello i'm using python3 function node that up to now is working.  
My question is there is a way in python to use:

- The **context object** -stores data for a node
- The **Flow object** – stores data for a flow
- The **global object** -stores data for the canvas.  
Because i found only example in js

---

<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:** [27 February 2023 10:46 UTC](https://discourse.nodered.org/t/simple-counter-with-python-context-get-count/75709/4 "2023-02-27T10:46:39Z")

</div>

Can you do something like this?

 ![Untitled 6](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/3/a3d295543a237e7c66f0acb24fd7e2e98486ef17.jpeg)

---

<div class="post-metadata">

**Author:** ![superlogan72](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/superlogan72/32/74680_2.png) [@superlogan72](https://discourse.nodered.org/u/superlogan72)\
**Post date:** [27 February 2023 12:18 UTC](https://discourse.nodered.org/t/simple-counter-with-python-context-get-count/75709/5 "2023-02-27T12:18:31Z")

</div>

Sorry but i don't understand maybe i'm too old, my test is very simple  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/c/7c9de2c00751bb5396a5c9455e3eda4f6798a390.png)

I need that each time i inject the value will increase of 1 unit

The inject is :  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/7/d7232c79b820c1bbc0ec677641bd96522b733e94.png)

and the code of the function

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/3/e3194bd3a01ebc162f455797444e11c3f9202ad2.png)

As you can understand any time i inject i reach the value of 2, but i need that the value will change to 3..4 etc.  
i'm not able to pass the return of my python function to the inject code

Thanks for your reply

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [27 February 2023 13:10 UTC](https://discourse.nodered.org/t/simple-counter-with-python-context-get-count/75709/6 "2023-02-27T13:10:11Z")

</div>

In your test flow, is 'pippo' a normal function node?

If it is, then here's a simple test flow for you.

![basic_count](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/1/6131b0a2aed556c16ec3e09036cb5ed22b157aab.png)

```auto
[{"id":"301e693371510a26","type":"tab","label":"Basic counter","disabled":false,"info":"","env":[]},{"id":"cbd46ba34bcfe405","type":"inject","z":"301e693371510a26","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":140,"y":120,"wires":[["855d3484f4e11828"]]},{"id":"01555cd5a614e68e","type":"inject","z":"301e693371510a26","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":140,"y":180,"wires":[["5d4a180c7b384be5"]]},{"id":"855d3484f4e11828","type":"change","z":"301e693371510a26","name":"Reset count to ZERO","rules":[{"t":"set","p":"count","pt":"flow","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":120,"wires":[[]]},{"id":"5d4a180c7b384be5","type":"function","z":"301e693371510a26","name":"Increment","func":"let my_count = flow.get('count') || 0;\n\nmy_count+=1;\n\nflow.set('count', my_count);\n\nmsg.payload = my_count;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":300,"y":180,"wires":[["c562f4f260eb80a7"]]},{"id":"c562f4f260eb80a7","type":"debug","z":"301e693371510a26","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":470,"y":180,"wires":[]}]

```

**EDIT:**  
I can see now it is called 'python-function' confusing as it has the same colour/shape as the function node.

![python_node](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/1/51498208f05a3d4f5bb3ea90cf61f3bebba0c89b.png)

Did you see the node hasn't been updated for over 3-years?

---

<div class="post-metadata">

**Author:** ![superlogan72](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/superlogan72/32/74680_2.png) [@superlogan72](https://discourse.nodered.org/u/superlogan72)\
**Post date:** [27 February 2023 16:45 UTC](https://discourse.nodered.org/t/simple-counter-with-python-context-get-count/75709/7 "2023-02-27T16:45:17Z")

</div>

i see but maybe someone use it

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [27 February 2023 17:26 UTC](https://discourse.nodered.org/t/simple-counter-with-python-context-get-count/75709/8 "2023-02-27T17:26:23Z")

</div>

Can I ask... why do you want to go to this extra complication when you can do it in a function node?

---

<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:** [27 February 2023 19:02 UTC](https://discourse.nodered.org/t/simple-counter-with-python-context-get-count/75709/9 "2023-02-27T19:02:18Z")

</div>

One of the delights of Node-red is that you can include programs in whatever language you are familiar with.  
Even so, I think calling a Python script just to increment a variable is over the top.

Here is a simple function node to do this in javascript

 ![Untitled 1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/6/b6f639cad0c76aa3dc1d187674e5374a3a9ff1ef.jpeg)

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [27 February 2023 20:26 UTC](https://discourse.nodered.org/t/simple-counter-with-python-context-get-count/75709/10 "2023-02-27T20:26:11Z")

</div>

You could even do it in a `change` node. here is a sample flow

```auto
[{"id":"c9f5d906b22d91f6","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[],"_mcu":{"mcu":false}},{"id":"a9900e4051f63adc","type":"inject","z":"c9f5d906b22d91f6","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","_mcu":{"mcu":false},"x":180,"y":140,"wires":[["62e29ac82735a364"]]},{"id":"62e29ac82735a364","type":"change","z":"c9f5d906b22d91f6","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$flowContext(\"count\")+1\t","tot":"jsonata"},{"t":"set","p":"count","pt":"flow","to":"payload","tot":"msg","dc":true}],"action":"","property":"","from":"","to":"","reg":false,"_mcu":{"mcu":false},"x":340,"y":140,"wires":[["88632bef54ab7765"]]},{"id":"88632bef54ab7765","type":"debug","z":"c9f5d906b22d91f6","name":"debug 191","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","_mcu":{"mcu":false},"x":530,"y":140,"wires":[]},{"id":"177f9b05650bc36c","type":"inject","z":"c9f5d906b22d91f6","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","_mcu":{"mcu":false},"x":200,"y":60,"wires":[["c15b0844979d4218"]]},{"id":"c15b0844979d4218","type":"change","z":"c9f5d906b22d91f6","name":"reset count","rules":[{"t":"set","p":"count","pt":"flow","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"_mcu":{"mcu":false},"x":350,"y":60,"wires":[[]]}]

```

 ![Screenshot 2023-02-27 at 3.12.06 PM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/0/802b1890474dd1fd12b944116da1f466ee1c85a9.png)

---

<div class="post-metadata">

**Author:** ![superlogan72](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/superlogan72/32/74680_2.png) [@superlogan72](https://discourse.nodered.org/u/superlogan72)\
**Post date:** [28 February 2023 10:04 UTC](https://discourse.nodered.org/t/simple-counter-with-python-context-get-count/75709/11 "2023-02-28T10:04:48Z")

</div>

Because i'm interested to know if i can develop projects with note-red and python, and for developing this i need to understand all the problems that i can find.  
is clear that if i need to do a simple counter as for the example i can use js, but my problem is if i need context object, flow object and global object it seems that i can't use it in python inside complex script .  
Thanks for your interest

---

<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"]]}]

```

---

<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:** [29 April 2023 14:59 UTC](https://discourse.nodered.org/t/simple-counter-with-python-context-get-count/75709/13 "2023-04-29T14:59:43Z")

</div>

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