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

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

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.

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

Can you do something like this?

Sorry but i don't understand maybe i'm too old, my test is very simple
image

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

The inject is :
image

and the code of the function

image

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

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

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

basic_count

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

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

i see but maybe someone use it

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

2 Likes

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

1 Like

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

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

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

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

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