# How to time difference using CoAP

**URL:** <https://discourse.nodered.org/t/how-to-time-difference-using-coap/37656>\
**Category:** General\
**Created:** [17 December 2020 15:18 UTC](https://discourse.nodered.org/t/how-to-time-difference-using-coap/37656 "2020-12-17T15:18:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sura45](https://avatars.discourse-cdn.com/v4/letter/s/eb8c5e/32.png) [@sura45](https://discourse.nodered.org/u/sura45)\
**Post date:** [17 December 2020 15:18 UTC](https://discourse.nodered.org/t/how-to-time-difference-using-coap/37656/1 "2020-12-17T15:18:16Z")

</div>

![Screen Shot 2020-12-17 at 4.13.38 pm](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/9/490dc872dca5d398c9b6e7a6a5718b1863dbca0c.png)

I want to get time1 which is the sending time(in milliseconds) and time2 for received time to calculate the time it takes to receive packet data. However, I am only getting one time(for received time) . Can anyone suggest me how can I get two different times in debug messages?

---

<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:** [17 December 2020 15:38 UTC](https://discourse.nodered.org/t/how-to-time-difference-using-coap/37656/2 "2020-12-17T15:38:15Z")

</div>

Welcome to the forum @sura45.

You have not told use what node type the time nodes are. node-red-contrib-something possibly.  
What to you see if you add a debug node showing the output of the first time node?

---

<div class="post-metadata">

**Author:** ![sura45](https://avatars.discourse-cdn.com/v4/letter/s/eb8c5e/32.png) [@sura45](https://discourse.nodered.org/u/sura45)\
**Post date:** [18 December 2020 08:52 UTC](https://discourse.nodered.org/t/how-to-time-difference-using-coap/37656/3 "2020-12-18T08:52:21Z")

</div>

The time nodes are "moment" nodes (node-red-contrib-moment).

 ![Screen Shot 2020-12-18 at 9.49.33 am](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/f/6fc80be18e4c31206b5fdd07fac4f94e6b052495.png)  
I added a debug node for the first time node but it shows like this(time1 is in the first debug message and C value is time2). I can see the time1 but I want to get both time1 and time2 in same debug message. Is there any way to do this?

---

<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 December 2020 09:55 UTC](https://discourse.nodered.org/t/how-to-time-difference-using-coap/37656/4 "2020-12-18T09:55:02Z")

</div>

Add a debug node showing what is coming out of the coap node and see if it has lost `msg.time1`. If it has that that is an issue with the coap node, it should not remove properties that are not relevant to its operation, so I suggest you submit an issue on the node on its github page. In the meantime the easiest solution is probably to save it to a flow context variable after the first moment node and then you can pick it up again later. However, I know there are nodes on the node-red flows site that will tell you the time between received messages, if that is actually what you are after, so you could feed both the incoming message and the coap node output message into that to get the time difference.  
Another alternative, that would do away with the moment node, which is very heavy on resources I believe would be to instead use a Change node on the input and use that to directly write the date to the flow variable on the way in. Configure it like this

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

Then after the coap node have another change node configured as

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/a/9ac3355603596c71e51679709b5aa40b92e911dc.png)

and you will then have the two times in `msg.time1` and `msg.time2`.

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [18 December 2020 10:17 UTC](https://discourse.nodered.org/t/how-to-time-difference-using-coap/37656/5 "2020-12-18T10:17:10Z")

</div>

This is what I use to easily make that calculation (courtesy of @E1cid) , and the time taken is displayed beneath the debug node.

```auto
[{"id":"a10c485a.fa75c","type":"inject","z":"c9c4eeb8.bad8e","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"str","x":160,"y":2150,"wires":[["4f4ae899.fee218"]]},{"id":"4f4ae899.fee218","type":"change","z":"c9c4eeb8.bad8e","name":"","rules":[{"t":"set","p":"start","pt":"msg","to":"","tot":"date"}],"action":"","property":"","from":"","to":"","reg":false,"x":310,"y":2150,"wires":[["980601d0.59c2a"]]},{"id":"ae47bbde.84a0a","type":"debug","z":"c9c4eeb8.bad8e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"$millis()-start","statusType":"jsonata","x":630,"y":2150,"wires":[]},{"id":"980601d0.59c2a","type":"http request","z":"c9c4eeb8.bad8e","name":"","method":"GET","ret":"txt","paytoqs":"ignore","url":"https://api.ipify.org","tls":"","persist":false,"proxy":"","authType":"","x":470,"y":2150,"wires":[["ae47bbde.84a0a"]]}]

```

---

<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:** [1 January 2021 10:17 UTC](https://discourse.nodered.org/t/how-to-time-difference-using-coap/37656/6 "2021-01-01T10:17:13Z")

</div>

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