Calculating time differences across messages
Sometimes you want to check the time used by some processing steps, or the time of an external request. This can be done by adding a Time Stamp module before and after the modules you want to measure. Then the Time Difference module can be used to calculate the difference between the two timestamps. This is easy when the two timestamps appear in the same message.
But what if you want to measure the difference between two timestamps that appear in different messages? For example, you want to check the time between messages received through an MQTT Subscriber module. Then you will have a timestamp in each received message, but to calculate the difference you need to get them into the same message.
For the special case where the time is monotonically increasing, which is the case if we for example look at reception times from an external source, there is a trick you can use. It works also for any data, if you are only interested in the absolute difference.
To get the two timestamps into the same message we can use the Statistics module. This module uses a rolling window and calculates some statistics over these windows, like average/min/max. If we set it up to use a window of 2 (two messages), the ‘max’ value will contain the most recent timestamp and the ‘min’ value the previous timestamp. We now have the two timestamps in the same message and can use e.g. the Time Difference module, or the Math module to calculate the difference. Note that the Statistics module only works with numerical data, so you have to generate, or convert, your timestamps as ‘Unix timestamps’, which can be configured in the Time Stamp module.