Yet another counter

This is my take on a counter:

There are 2 modes, if outputPayload is set to true, the count will be output as the payload, otherwise the input msg will pass through.

You can also specify a flow or global variable to store the count in flowVariable or globalVariable.

Sending msg.reset set to true will set the count back to 0.

[{"id":"f7b58a29cb7c9366","type":"subflow","name":"counter","info":"This is yet another counter implementation.\n\nThere are 2 modes, if `outputPayload` is set to `true`, the count will be output as the `payload`, otherwise the input `msg` will pass through.\n\nYou can also specify a flow or global variable to store the count in `flowVariable` or `globalVariable`.\n\nSending `msg.reset` set to `true` will set the count back to 0.","category":"","in":[{"x":180,"y":80,"wires":[{"id":"91cf1049c708cd18"}]}],"out":[{"x":600,"y":80,"wires":[{"id":"91cf1049c708cd18","port":0}]}],"env":[{"name":"flowVariable","type":"str","value":""},{"name":"globalVariable","type":"str","value":""},{"name":"outputPayload","type":"bool","value":"false"}],"meta":{},"color":"#DDAA99","status":{"x":600,"y":200,"wires":[{"id":"5ad360c7a7c44b42","port":0}]}},{"id":"91cf1049c708cd18","type":"function","z":"f7b58a29cb7c9366","name":"count","func":"const flowVariable = env.get('flowVariable')\nconst globalVariable = env.get('globalVariable')\nconst outputPayload = env.get('outputPayload')\n\nif (msg.reset === true) {\n    flow.set('count', 0)\n    node.status({text: 0})\n    if (flowVariable.length) {\n        flow.set(`$parent.${flowVariable}`, 0)\n    }\n    return\n}\n\nlet count = flow.get('count')\ncount = count + 1\nflow.set('count', count)\nnode.status({ text: count })\n\n\nif (flowVariable.length) {\n    flow.set(`$parent.${flowVariable}`, count)\n}\n\nif (globalVariable.length) {\n    global.set(globalVariable, count)\n}\n\nif (outputPayload) {\n    msg.payload = count\n}\nreturn msg","outputs":1,"noerr":0,"initialize":"flow.set('count', 0)","finalize":"","libs":[],"x":370,"y":80,"wires":[[]]},{"id":"5ad360c7a7c44b42","type":"status","z":"f7b58a29cb7c9366","name":"","scope":["91cf1049c708cd18"],"x":280,"y":200,"wires":[[]]},{"id":"f59fde0be7802af8","type":"subflow:f7b58a29cb7c9366","z":"3bd8a7012489e42c","g":"6fdfafa553b5e627","name":"","env":[{"name":"flowVariable","value":"count.compteur1","type":"str"},{"name":"globalVariable","value":"bimbo","type":"str"},{"name":"outputPayload","value":"true","type":"bool"}],"x":550,"y":400,"wires":[["18554fec136d7e6f"]],"icon":"font-awesome/fa-plus"}]

Flow Info

Created 2 years, 4 months ago
Rating: not yet rated

Owner

Actions

Rate:

Node Types

Core
  • function (x1)
  • status (x1)
Other
  • subflow (x1)
  • subflow:f7b58a29cb7c9366 (x1)

Tags

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