Increment slowly until certain point

Hi, Sorry for asking a silly question. But I am a super beginner at node-red and trying my best to learn something.
I want to have a function that slowly increases the value to 100 (I want that value to pass to IOT device continuously.)
Like initially 1, then after 1 sec:2, after 1 sec:3 ..... like that, until 100. All values should be continuously sent to IOT device.

How can I do this? Any help/resources will be super helpful.
Thank you so much!!

You could use a trigger node and context storage for this

[{"id":"06946e2ac3186460","type":"inject","z":"366a43adb328cf95","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":100,"y":640,"wires":[["8b8dbe0d1812b395"]]},{"id":"8b8dbe0d1812b395","type":"change","z":"366a43adb328cf95","name":"","rules":[{"t":"set","p":"count","pt":"flow","to":"1","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":260,"y":640,"wires":[["bfdd439c7f41fc2a"]]},{"id":"bfdd439c7f41fc2a","type":"trigger","z":"366a43adb328cf95","name":"","op1":"count","op2":"count","op1type":"flow","op2type":"flow","duration":"1","extend":false,"overrideDelay":false,"units":"s","reset":"10","bytopic":"all","topic":"topic","outputs":2,"x":460,"y":640,"wires":[["ce7726e99ee0e4c0"],["78aeaa0f29ffe286"]]},{"id":"78aeaa0f29ffe286","type":"change","z":"366a43adb328cf95","name":"","rules":[{"t":"set","p":"count","pt":"flow","to":"$$.payload+1","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":480,"y":700,"wires":[["bfdd439c7f41fc2a"]]},{"id":"ce7726e99ee0e4c0","type":"debug","z":"366a43adb328cf95","name":"debug 92","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":680,"y":640,"wires":[]}]

To see flow example, Copy code, press ctrl i in editor, paste code and import.
Set to count to 10, to change this set reset trigger if msg.payload = 100, in the trigger node.

1 Like

Example in a function node

let count = 100 // iterations
let interval = 1000 // initial 1 second

function iterator() {
    node.send({ payload: count, interval: `${interval / 1000} second(s)` })

    count -= 1
    interval += 1000
    if (count > 0) {
        setTimeout(iterator, interval)

edit. arg i was reading it incorrectly - i was reading it as increasing/decreasing the time between messages, but if it is a fixed time, you can easily do this with delay node in rate limit mode, you can drop all 100 messages of at the delay node and they will be queued according to the parameters.

In a function node generate the numbers:


connect it to the delay node.

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