# Delay in reading each line of data from the file

**URL:** <https://discourse.nodered.org/t/delay-in-reading-each-line-of-data-from-the-file/93648>\
**Category:** General\
**Created:** [3 December 2024 01:59 UTC](https://discourse.nodered.org/t/delay-in-reading-each-line-of-data-from-the-file/93648 "2024-12-03T01:59:43Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![lihao](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lihao/32/72171_2.png) [@lihao](https://discourse.nodered.org/u/lihao)\
**Post date:** [3 December 2024 01:59 UTC](https://discourse.nodered.org/t/delay-in-reading-each-line-of-data-from-the-file/93648/1 "2024-12-03T01:59:43Z")

</div>

I want to use node-red to read a file, but I want the reading speed for each line of data to be 40ms. What should I do?

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [3 December 2024 02:48 UTC](https://discourse.nodered.org/t/delay-in-reading-each-line-of-data-from-the-file/93648/2 "2024-12-03T02:48:45Z")

</div>

I don't know how in Node-red you could read one line from a file every 40ms, but you could read the file once and use a rate limit (delay) node to only pass 25 lines per second.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/f/7fdfc2880f8c1f494165b55dd02180799aea53e4.png)

---

<div class="post-metadata">

**Author:** ![lihao](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lihao/32/72171_2.png) [@lihao](https://discourse.nodered.org/u/lihao)\
**Post date:** [3 December 2024 03:01 UTC](https://discourse.nodered.org/t/delay-in-reading-each-line-of-data-from-the-file/93648/3 "2024-12-03T03:01:24Z")

</div>

Thanks for your help, but the time intervals of the data inside are very random.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [3 December 2024 03:14 UTC](https://discourse.nodered.org/t/delay-in-reading-each-line-of-data-from-the-file/93648/4 "2024-12-03T03:14:36Z")

</div>

> [@lihao](#):
>
> the time intervals of the data inside are very random.

Can you explain what you mean?

Why do you need to read one line every 40ms?

How precise must the 40ms interval be?

How big is the file?

Are it's contents static or is it growing?

---

<div class="post-metadata">

**Author:** ![lihao](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lihao/32/72171_2.png) [@lihao](https://discourse.nodered.org/u/lihao)\
**Post date:** [3 December 2024 03:42 UTC](https://discourse.nodered.org/t/delay-in-reading-each-line-of-data-from-the-file/93648/5 "2024-12-03T03:42:29Z")

</div>

1. Although the delay node will limit the output per second, the interval of the output result is 30ms or 50ms, not 40ms.
2. I want to make a dynamic display of data, combined with influxDB
3. The accuracy is ms
4. The file has a total of 750 data, each data interval is 40ms, a total of 30s data
5. I read data from a file, the file data is static

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [3 December 2024 04:44 UTC](https://discourse.nodered.org/t/delay-in-reading-each-line-of-data-from-the-file/93648/6 "2024-12-03T04:44:56Z")

</div>

It might be worth trying a queue node such as node-red-contrib-queue-gate and a function node to release one message at a time something like this:

```auto
setInterval(mytimer, 1000) // Interval 1 second
function mytimer() {
    node.send({"topic":"control", "payload": "trigger"})
}

```

You will probably need a mechanism to stop the interval timer with `clearInterval(mytimer)` !

I doubt if you could achieve better accuracy than the rate limit node though.  
I don't even know if it will work at all.

---

<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:** [3 December 2024 06:50 UTC](https://discourse.nodered.org/t/delay-in-reading-each-line-of-data-from-the-file/93648/7 "2024-12-03T06:50:35Z")

</div>

Depending on use, you could create a timestamp for each message 40ms apart.  
e.g

```auto
[{"id":"65bead4e367b5e99","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"timestamp","v":"","vt":"date"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":350,"y":7600,"wires":[["d61684d287ddb1ac"]]},{"id":"d61684d287ddb1ac","type":"template","z":"d1395164b4eec73e","name":"read file","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"1\n2\n3\n4\n5\n6\n7\n8\n9","output":"str","x":380,"y":7660,"wires":[["4cc392834d50680b"]]},{"id":"4cc392834d50680b","type":"split","z":"d1395164b4eec73e","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","property":"payload","x":510,"y":7660,"wires":[["a0c1f08bbbf34f9a"]]},{"id":"a0c1f08bbbf34f9a","type":"delay","z":"d1395164b4eec73e","name":"","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"20","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":660,"y":7660,"wires":[["c9b9cb4997fdf4fa"]]},{"id":"c9b9cb4997fdf4fa","type":"change","z":"d1395164b4eec73e","name":"","rules":[{"t":"set","p":"timestamp","pt":"msg","to":"$$.timestamp +( $$.parts.index * 40)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":850,"y":7660,"wires":[["8047327890ff07d0"]]},{"id":"8047327890ff07d0","type":"debug","z":"d1395164b4eec73e","name":"debug 2575","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":670,"y":7600,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![lihao](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lihao/32/72171_2.png) [@lihao](https://discourse.nodered.org/u/lihao)\
**Post date:** [3 December 2024 08:33 UTC](https://discourse.nodered.org/t/delay-in-reading-each-line-of-data-from-the-file/93648/8 "2024-12-03T08:33:47Z")

</div>

Thanks for your help, I will try this method soon.

---

<div class="post-metadata">

**Author:** ![lihao](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lihao/32/72171_2.png) [@lihao](https://discourse.nodered.org/u/lihao)\
**Post date:** [3 December 2024 08:36 UTC](https://discourse.nodered.org/t/delay-in-reading-each-line-of-data-from-the-file/93648/9 "2024-12-03T08:36:00Z")

</div>

Yes, this method of combining the delay node and redefining the timestamp is indeed a good method.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [3 December 2024 09:24 UTC](https://discourse.nodered.org/t/delay-in-reading-each-line-of-data-from-the-file/93648/10 "2024-12-03T09:24:15Z")

</div>

> [@lihao](#):
>
> The file has a total of 750 data, each data interval is 40ms, a total of 30s data

Can you not get the timestamp added when the data are initially collected and written to the file?

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [3 December 2024 11:22 UTC](https://discourse.nodered.org/t/delay-in-reading-each-line-of-data-from-the-file/93648/11 "2024-12-03T11:22:10Z")

</div>

Somehow I got the idea that the OP wanted to _read_ one line per 40ms, but it does make more sense to want one line _written_ per 40 ms.

---

<div class="post-metadata">

**Author:** ![lihao](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lihao/32/72171_2.png) [@lihao](https://discourse.nodered.org/u/lihao)\
**Post date:** [3 December 2024 11:34 UTC](https://discourse.nodered.org/t/delay-in-reading-each-line-of-data-from-the-file/93648/12 "2024-12-03T11:34:42Z")

</div>

You can try combining the _inject node_ with the _trigger node_.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [3 December 2024 12:29 UTC](https://discourse.nodered.org/t/delay-in-reading-each-line-of-data-from-the-file/93648/13 "2024-12-03T12:29:59Z")

</div>

> [@jbudd](#):
>
> I got the idea that the OP wanted to _read_ one line per 40ms,

That is what he asked for, but actually what he wants to do, I believe, is add them at that interval to influxdb. My suggestion is to add timestamps when the data are collected so they have timestamps in the file, which can be passed to influxdb.

---

<div class="post-metadata">

**Author:** ![lihao](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lihao/32/72171_2.png) [@lihao](https://discourse.nodered.org/u/lihao)\
**Post date:** [3 December 2024 12:42 UTC](https://discourse.nodered.org/t/delay-in-reading-each-line-of-data-from-the-file/93648/14 "2024-12-03T12:42:44Z")

</div>

Yes, but time sync is not possible on my sensor.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [3 December 2024 13:02 UTC](https://discourse.nodered.org/t/delay-in-reading-each-line-of-data-from-the-file/93648/15 "2024-12-03T13:02:19Z")

</div>

How does the value get from the sensor to the file?

---

<div class="post-metadata">

**Author:** ![lihao](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lihao/32/72171_2.png) [@lihao](https://discourse.nodered.org/u/lihao)\
**Post date:** [3 December 2024 13:40 UTC](https://discourse.nodered.org/t/delay-in-reading-each-line-of-data-from-the-file/93648/16 "2024-12-03T13:40:34Z")

</div>

BLE .  
The wireless receiver is a USB Dongle, not a smart phone.

---

<div class="post-metadata">

**Author:** ![lihao](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lihao/32/72171_2.png) [@lihao](https://discourse.nodered.org/u/lihao)\
**Post date:** [3 December 2024 13:41 UTC](https://discourse.nodered.org/t/delay-in-reading-each-line-of-data-from-the-file/93648/17 "2024-12-03T13:41:59Z")

</div>

Then we use the USB Dongle's UART to input to the Node Red. The only place where we can get the timestamp may be the Node Red.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [3 December 2024 14:25 UTC](https://discourse.nodered.org/t/delay-in-reading-each-line-of-data-from-the-file/93648/18 "2024-12-03T14:25:36Z")

</div>

Yes, add the timestamps at the same time as you add the values.  
Why are you writing them to a file? Why don't you send them straight to influx?

---

<div class="post-metadata">

**Author:** ![lihao](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lihao/32/72171_2.png) [@lihao](https://discourse.nodered.org/u/lihao)\
**Post date:** [4 December 2024 04:20 UTC](https://discourse.nodered.org/t/delay-in-reading-each-line-of-data-from-the-file/93648/19 "2024-12-04T04:20:23Z")

</div>

I have already implemented the function you mentioned, and my purpose is just to show it dynamically later.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [4 December 2024 07:27 UTC](https://discourse.nodered.org/t/delay-in-reading-each-line-of-data-from-the-file/93648/20 "2024-12-04T07:27:48Z")

</div>

I don't think you answered the question, why do you not write the data to influx as you receive it?

[Next page](https://discourse.nodered.org/t/delay-in-reading-each-line-of-data-from-the-file/93648.md?page=2)
