# Increment slowly until certain point

**URL:** https://discourse.nodered.org/t/increment-slowly-until-certain-point/68938
**Category:** General
**Tags:** function-node
**Created:** [12 October 2022 15:37 UTC](https://discourse.nodered.org/t/increment-slowly-until-certain-point/68938 "2022-10-12T15:37:28Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![ujjwalkuikel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ujjwalkuikel/32/69012_2.png) [@ujjwalkuikel](https://discourse.nodered.org/u/ujjwalkuikel)
#### Post date: [12 October 2022 15:37 UTC](https://discourse.nodered.org/t/increment-slowly-until-certain-point/68938/1 "2022-10-12T15:37:28Z")

</div>

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!!

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

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [12 October 2022 15:55 UTC](https://discourse.nodered.org/t/increment-slowly-until-certain-point/68938/2 "2022-10-12T15:55:52Z")

</div>

You could use a trigger node and context storage for this  
e.g.

```auto
[{"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.

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [12 October 2022 16:14 UTC](https://discourse.nodered.org/t/increment-slowly-until-certain-point/68938/3 "2022-10-12T16:14:17Z")

</div>

Example in a function node

```auto
let count = 100 // iterations
let interval = 1000 // initial 1 second
iterator();

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:

```auto
Array.from(Array(10).keys()).forEach(n=>{
    node.send({payload:n})
})

```

connect it to the delay node.

 ![SCR-20221012-pde](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/b/1b61e23d18344b347ceeafad19e4a80dbd937442.png)

---

<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: [11 December 2022 16:14 UTC](https://discourse.nodered.org/t/increment-slowly-until-certain-point/68938/4 "2022-12-11T16:14:30Z")

</div>

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