Reset counter values

Hello,

In this flow, there are randomly generated numbers between 20 and 60 in function "Data Generation". Functions' output is connected to a trigger node which configuration is shown bellow. Trigger's output is connected to a function "Fire Off" (Α Mister from this forum helped me a lot with this part). After that there is a debug node which shows the message. What I am trying to accomplish is: generate a random number between 20 and 60, then decrease this number by 1 every 1 second and when the value becomes 20 the next value I want to be the random generated number between 20 and 60. The problem is that flow runs only once and that's why I am trying to insert something like a reset to the value. Are there any ideas on how to make it happen?

Thanks for your time, appreciate your help!


image
image
image

Finished Flow

[{"id":"ff6cdaba.0c2748","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"1e5e334b.eee44d","type":"function","z":"ff6cdaba.0c2748","name":"Data Generation","func":"flow.set(\"counter\", Math.round(Math.random()*(60 - 20) + 20))//Math.floor(Math.random()*41)+20)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":580,"y":280,"wires":[["4b796f69.bc785"]]},{"id":"31b6afa1.d6b6e","type":"inject","z":"ff6cdaba.0c2748","name":"Sensor","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":530,"y":240,"wires":[["1e5e334b.eee44d"]]},{"id":"ded18de2.933c5","type":"function","z":"ff6cdaba.0c2748","name":"Fire Off","func":"var counter = flow.get('counter')\nmsg.payload = counter;\nif(counter==20)\n{\ncounter=Math.floor(Math.round(Math.random()*(60 - 20) + 20));\n}\nelse\n{\ncounter--;\n}\nflow.set(\"counter\",counter);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":680,"y":360,"wires":[["212b659d.77354a"]]},{"id":"212b659d.77354a","type":"debug","z":"ff6cdaba.0c2748","name":"Off","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":710,"y":400,"wires":[]},{"id":"4b796f69.bc785","type":"trigger","z":"ff6cdaba.0c2748","name":"","op1":"","op2":"","op1type":"pay","op2type":"payl","duration":"-500","extend":true,"overrideDelay":false,"units":"ms","reset":"20","bytopic":"all","topic":"topic","outputs":1,"x":640,"y":320,"wires":[["ded18de2.933c5"]]}]

If you would post your whole flow that might help. But... your title says reset value but in your description you say you want to clear the value. That confuses me, what exactly do you want it to do? Set to zero , to me that is clearing it or do you want to set it back to 60 or whatever the random number was?

you mean something like this

[{"id":"5f6dc1a8.610bb8","type":"inject","z":"c74669a0.6a34f8","name":"start","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":170,"y":3280,"wires":[["13c80fc2.f0da18"]]},{"id":"13c80fc2.f0da18","type":"change","z":"c74669a0.6a34f8","name":"","rules":[{"t":"set","p":"count","pt":"flow","to":"$floor($random()*41)+20","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":3320,"wires":[["6257365b.69ed28"]]},{"id":"6257365b.69ed28","type":"trigger","z":"c74669a0.6a34f8","name":"","op1":"count","op2":"20","op1type":"flow","op2type":"num","duration":"-1","extend":false,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":540,"y":3320,"wires":[["51c55056.111468"]]},{"id":"51c55056.111468","type":"change","z":"c74669a0.6a34f8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"count","tot":"flow"},{"t":"set","p":"count","pt":"flow","to":"$flowContext(\"count\") = 20 ? ($floor($random()*41)+20) : $flowContext(\"count\") - 1","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":770,"y":3320,"wires":[["2ada69b7.748bbe"]]},{"id":"fd2ebb80.2090b8","type":"inject","z":"c74669a0.6a34f8","name":"stop","props":[{"p":"reset","v":"true","vt":"bool"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":140,"y":3360,"wires":[["6257365b.69ed28"]]},{"id":"2ada69b7.748bbe","type":"debug","z":"c74669a0.6a34f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":970,"y":3320,"wires":[]}]

Excuse me for my wrong formulation. What I want to do is the following: when the value becomes 20 the next value I want to be the random generated number between 20 and 60. I have updated the text. Really thanks for time !

Looks like @E1cid has got your answer then

Thanks for your reply! I mean something like this but is it possible to be implemented using function node instead of change?

Sure
In first function node create a random number and save to context
in trigger use that context stored var
in second function read context var , copy to payload, check if value is 20, if so regenerate random number, if not - 1, then save back to context var

That should be flow.set() not context.get(), as context is only availablein the node.

and use flow in second function

also remove the quotes around the Math.random as you are making a string

In second function if counter == 20 the you need to generate another random.

Excuse me again, I am new and I hardly understand. I implement the following in the first function
image
and the second one
image
What should I check again, because its not working.
Thanks again for your time !

I see, there is an error when I replace context.get to flow.set
image
image

flow.set takes two arguments the name of the var and the value, you have only set one.
You also still have quotes around your Math.Random()

You do not need to do counter = flow.set.

just
flow.set("counter" , Math.floor(Math.random()*41)+20)

Excuse me again, I deleted some replies because they were not formulated correctly. I managed to fix the first function.. can you please explain me how the second function should work once more ? Here is what I have implemented
image

the first flow.set should be flow.get
remove the second msg.payload = counter;

Thanks a lot for your help ! I have one more question. Can you explain me what is the difference between use of ".msg" and ."flow"? I searched on google but still can't understand. Really appreciate it !

flow and global are context storage variables, these are stored and can be retrieved from other messages passing down the flow.

msg is the message passing down the flow, and is not available to other messages passing down the flow.

Working with context : Node-RED.

[edit p.s. post your finished flow so others can see the end result and learn and use it.]

Hello again!

I posted the right code, and I have one more question... I tried using "context.get" and "context.set" but what I get returned is a "Not A Number" statement. So is "context" not working because it doesn't link the nodes that is attached? On the other hand, is "flow.get/set" working because it links every node that is attached?

Thanks for your time, appreciate your help buddy !

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