Calculating the time between two packets (TTN + Node-RED)

Good afternoon every one, following this topic Getting uplink metadata using Node-RED i was able to see the MetaData of the packets that i’m sending from TTN to Node-Red . I want to calculate the difference in time between the received packets so i can know the latency of the transmission , i don’t have any experience with Javascript ,but i tried to write some simple math operations but with no success , kindly i want to know how i can subtract the time of arrival of the previous packet from the time of arrival of the the latest received packet .

NodeRed_Issue

Just for those who are facing the same issue , i’d found a node which called (interval length node) that measure the time between 2 successive messages. you can read about this node in:
Interval length Node

the result on my flow was like:

Node-RED

2 Likes

Nice.

Just in case you need time handling later on: msg.metadata.time is a text string. Programming languages cannot automatically tell the difference from "2020-07-04T15:38:25.418218894Z" to, say, "Hello World". So you’ll first need to tell JavaScript the text denotes some date and time. For the ISO 8601 formatted text that TTN gives you that’s easy:

var previous = new Date("2020-07-04T15:38:25.418218894Z");
var current = new Date(msg.metadata.time);
var milliseconds = current - previous;
var seconds = milliseconds / 1000;
var hours = seconds / 3600;

Aside: this is not called “latency”. Latency is the time it takes for a packet to get from node to gateway (this is the calculated air time), then from gateway(s) to the network server and other TTN components, and finally up to your Node-RED application server. That kind of delay you cannot calculate as you don’t know anything about the accuracy of the clocks in all those systems. You may even get negative values then. But that’s not what you wanted anyway. “Interval” is a much better name!

Another aside, please don’t post code as images: it just adds a lot of unrelated details (even inactive code and comments; why even bother the readers with that?), it’s hard to view on small devices, it’s not searchable, and one cannot copy code out of an image.

Thanks for posting the solution!

1 Like

Oh, another aside: beware that the TTN Node-RED library has been discontinued last year:

:warning: The support and maintenance for this SDK has been discontinued, it is not recommended to use the SDK for new projects.

So, use a regular Node-RED MQTT client for new development, along with the details from the MQTT Data API documentation.

1 Like

I’m using a RN2483 which fires a packet with current time in milliseconds as the payload. Upon arrival the ‘delay’ is calculated and displayed, it keeps track of cur/min/max/avg and lost ‘packets’ pretty similar to a ping. In below data you can see it can take up to 25 seconds before it arrives, but on average it takes 531 milliseconds.

2020-07-07 12:25:34.330418 mac tx uncnf 100 7a32cf2873010000
2020-07-07 12:25:34.827556 *********/devices/******/up - received 3413, lost 7, on 2 gateways 'eui-1dee192e3d82b8e4' 'ttn-1424ar-001', rtt cur/min/avg/max 460/359/531/25727
2020-07-07 12:25:36.483470 transmission successful
2020-07-07 12:25:36.484862 up=4067, down=9, mac_tx=2154
1 Like