# How to include timestamp in modbus read out

**URL:** <https://discourse.nodered.org/t/how-to-include-timestamp-in-modbus-read-out/23253>\
**Category:** Industrial\
**Created:** [17 March 2020 23:40 UTC](https://discourse.nodered.org/t/how-to-include-timestamp-in-modbus-read-out/23253 "2020-03-17T23:40:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![chengdi123000](https://avatars.discourse-cdn.com/v4/letter/c/e9a140/32.png) [@chengdi123000](https://discourse.nodered.org/u/chengdi123000)\
**Post date:** [17 March 2020 23:40 UTC](https://discourse.nodered.org/t/how-to-include-timestamp-in-modbus-read-out/23253/1 "2020-03-17T23:40:25Z")

</div>

Hi everyone,

I want to use `modbustcp` to gather some data from a `modbus over tcp` server. And I want to attach a timestamp on the output message. Which one is the best solution:

1. modify the original `modbustcp` source code. I am not so familiar with the coding. It used some promise and async feature.
2. modify use injection to add timestamp at both start and end of `modbustcp read` node and calculate the average timestamp to set the time of msg result.
3. Is there any internal mechanism to include the timestamp to output result?

---

<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:** [18 March 2020 08:51 UTC](https://discourse.nodered.org/t/how-to-include-timestamp-in-modbus-read-out/23253/2 "2020-03-18T08:51:41Z")

</div>

How accurate a value do you need? I assume the actual read process will take only fractions of a second, surely you do not need to get the time before and after and take the average.  
Would it not be good enough to add a node after the modbus node that adds the timestamp? A function node containing this should do it:

```auto
msg.timestamp = new Date()
return msg;

```

---

<div class="post-metadata">

**Author:** ![chengdi123000](https://avatars.discourse-cdn.com/v4/letter/c/e9a140/32.png) [@chengdi123000](https://discourse.nodered.org/u/chengdi123000)\
**Post date:** [18 March 2020 12:50 UTC](https://discourse.nodered.org/t/how-to-include-timestamp-in-modbus-read-out/23253/3 "2020-03-18T12:50:12Z")

</div>

Thanks a lot for your advice.

It cost a little time in my LAN environment though I did not know how to measure it. But I think it is not so important for my application.

Currently I am using a `function` node to get result from `modbustcp` node and add `msg.time = Data.now()` to the `msg` because of following reasons:

1. It is an UTC timestamp in ms precision. It can be write into my `influxdb` directly without further adjustment;
2. The timestamp of `modbustcp` readout do not include delay time from `sampling` and `measurement` process of the instrument behind of the `modbus tcp server`. I still need to calibrate it and align the data. So I just choose a convenient way to add timestamp.

---

<div class="post-metadata">

**Author:** ![mturnbull](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mturnbull/32/20742_2.png) [@mturnbull](https://discourse.nodered.org/u/mturnbull)\
**Post date:** [27 April 2020 13:15 UTC](https://discourse.nodered.org/t/how-to-include-timestamp-in-modbus-read-out/23253/4 "2020-04-27T13:15:41Z")

</div>

This issue is probably resolved but another neat way to add a timestamp without writing code in a function node is to use a Change node with the Jasonata option.

Jasonata has a couple of easy to use time evaluation functions. I'm not saying this is better than a function node but it does avoid coding and I believe it solves this in one line (one statement). I just tried it with $millis() and it does provide millisecond resolution you are seeking. The Epoch number format can can then be written directly to InfluxDB in this format... See below...

 ![Jasonata](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/5/258755b92391f89ae92eadeeb44b9d003d2329d4.jpeg)

---

<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:** [26 June 2020 13:15 UTC](https://discourse.nodered.org/t/how-to-include-timestamp-in-modbus-read-out/23253/5 "2020-06-26T13:15:53Z")

</div>

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