How-to
One shared timestamp for two devices in Node-RED and Machine Advisor
Short answer
Buffer one reading from each device in a Node-RED Function node, copy the first device's timestamp onto its partner, publish both together, then clear the slots. Machine Advisor Application formulas then see equal timestamps and can add or compare the two variables. The pairing aligns records for calculation; it does not make the underlying measurements simultaneous.

On this page
- Why do two devices reach Machine Advisor with different timestamps?
- What does the timestamp value actually represent?
- Which node holds the two readings until both arrive?
- How do you build the pairing Function node step by step?
- How old can a buffered reading be before the pair lies?
- How do you prove the timestamps match all the way to the cloud?
- When does one shared timestamp still not fix the formula?
- Questions asked
Two Modbus devices read through separate Node-RED branches land in Machine Advisor with different timestamps, so Application formulas that add or compare the two variables never find a matching pair of records. Build one Function node that buffers a reading from each device in context, copies one device's timestamp onto its partner, publishes both records together, and clears both slots for the next cycle. The shared timestamp gives the formula engine one calculation time; it does not make the two physical measurements simultaneous, and the pairing window you allow must come from how fast the process moves.
Why do two devices reach Machine Advisor with different timestamps? #
Each Modbus read completes on its own schedule and each publishing branch stamps its message when it sends, so two independent branches almost never carry the same time value even when both reads fire from the same trigger tick. One device answers in tens of milliseconds, the other takes longer, and scheduling plus message processing add their own delays on top. In the stored records the two counts differ well below the second while both values look current on any display, and the formula engine compares the raw counts exactly, not what the screen shows. The cure is not faster polling; it is one point in the flow where both readings are forced to carry the same value before they are published.
What does the timestamp value actually represent? #
A timestamp is a count of elapsed seconds since 00:00:00 UTC on 1 January 1970, and the Application formula engine joins two variables only when that exact count matches, so even a one second difference breaks a sum or a comparison. JavaScript delivers Date.now() in milliseconds, so two branches can easily hold values a thousand times apart in size while meaning the same moment. Read one record and decide the resolution once, before you pair anything. Then copy the complete numeric value through the pair and never rebuild it from a formatted date string: two formatted strings can look identical at second resolution while the raw counts still differ, and the formula engine does not read formatted strings.
Which node holds the two readings until both arrive? #
A single Function node placed after both Modbus branches holds one reading per device in its context, returns nothing until both slots are filled, emits the pair on one shared timestamp, and then empties both slots. Node-RED's context store exists for exactly this job: it keeps data between messages without pushing it through the flow, and context.get and context.set are the documented calls that read and write it. The default store lives in memory, which is all this application needs. A ready-made community node can also join messages across several paths inside a time window, but its output must still be shaped for two separate publishing branches, which is why the hand-written Function node below emits two messages. The pattern is the same one used to combine two MQTT sensor inputs in one Node-RED calculation, so that walkthrough doubles as background for this one.
How do you build the pairing Function node step by step? #
Five steps cover it: give each branch a distinct topic, drop failed reads, store each valid reading in context under its device key, emit two messages with one copied timestamp when both keys are present, and clear the context for the next cycle.
- Give each branch a distinct identity. Set
msg.topictolineAon one branch andlineBon the other — example names for the two devices — where each read enters the Function node. - Drop failed reads before they reach the buffer. A timeout or a garbage frame must never fill a slot, or the pair will publish a value that was never measured.
- Store each valid reading under its own device key, with the reading's own time alongside the value, so the age of a buffered slot is always checkable.
- When both keys are present, copy the designated device's time onto the partner and return the pair as an array of two messages — the documented way a Function node emits several messages on one output.
- Clear both slots on emit and return nothing until a fresh pair is complete.
The code below implements the five steps. Its names are declared as examples: lineA and lineB for the two branches, msg.time for the reading's own time in seconds as a number.
// Pairing Function node: placed after both Modbus branches, before the publishing branches.
// Illustrative names, set by your own branches: msg.topic carries lineA or lineB,
// msg.time carries the reading's Unix time in seconds as a number.
// Documented behaviour used here: context.get, context.set, returning null to send
// nothing, and returning an array to send several messages on one output.
const branch = String(msg.topic); // lineA or lineB
const slots = context.get("slots") || {}; // object, starts empty on each deploy
// A failed read must never fill a slot.
if (msg.payload === null || msg.payload === undefined) {
return null;
}
slots[branch] = { value: msg.payload, ts: msg.time };
if (slots.lineA !== undefined && slots.lineB !== undefined) {
const shared = slots.lineA.ts; // policy: the designated device supplies the pair time
const outA = { topic: "lineA", payload: slots.lineA.value, time: shared };
const outB = { topic: "lineB", payload: slots.lineB.value, time: shared };
context.set("slots", {}); // clear both slots for the next cycle
return [outA, outB];
}
context.set("slots", slots);
return null;
Prove it on the bench before it touches the cloud. Inject 41.7 into the first branch and 38.2 into the second — example values — and watch the debug pane: the node must emit two messages, one per device, whose time fields are identical down to the last digit, and a third injection into only one branch must produce no output at all until its partner arrives.
How old can a buffered reading be before the pair lies? #
Set the age limit from the process rate: a signal that moves 2 °C in 60 s with a formula tolerance of 1 °C allows a 30 s spread, because 1 °C divided by 0.033 °C per second gives 30 s. The inputs are an example: an allowed formula error of 1 °C and a signal that drifts at most 2 °C in 60 s. The drift rate is 2 ÷ 60 = 0.033 °C per second, so the permitted measurement spread is 1 ÷ 0.033 ≈ 30 s. With both branches polled on a common 5 s tick, the arrival separation never exceeds one interval, and the worst-case value skew inside a pair is 5 × 0.033 ≈ 0.17 °C — well inside the tolerance, so the pairing itself is safe. Set the discard limit tighter than the ceiling, at three intervals: 3 × 5 = 15 s, so a slot older than 15 s is dropped and a healthy device is never paired with a reading left over from a previous cycle.
// Same Function node, before the pairing check:
// drop the waiting slot when its partner is late by more than the discard limit.
const DISCARD_S = 15; // seconds; three poll intervals in the worked example
if (slots.lineA !== undefined && msg.time - slots.lineA.ts > DISCARD_S) {
delete slots.lineA;
}
The ceiling moves inversely with the process rate. The same 1-unit tolerance on a signal moving 0.5 unit per second gives 1 ÷ 0.5 = 2 s, tighter than one poll interval; at that rate, fire both reads from a single tick so the separation collapses to device response skew alone.
The copied count declares one calculation time; the underlying readings still happened up to one interval apart. That separation between when a value was measured and when it arrived is the same distinction that matters when you keep both arrival time and payload timestamp in Telegraf.
One boundary: this pairing serves monitoring and calculation records; a value that guards people or equipment belongs in certified safety hardware, not in a buffering Function node.
How do you prove the timestamps match all the way to the cloud? #
Verify at three boundaries: the Function node output, the publishing branch input, and the stored records, comparing the raw timestamp values rather than the formatted display strings.
- At the Function node output: send the same two example values again and confirm the debug pane shows equal raw time values, one message per device, not just equal formatted strings.
- At the publishing branch input: confirm the field you set is the field the branch actually consumes, in the resolution it expects.
- In the stored records: the counts for both variables must be equal. If the node output is equal but the records differ, the publishing path is replacing or re-stamping the field — that is where to look next.
- Interrupt one branch under control: the surviving device must not form a false pair, and the discard limit must empty the stale slot.
- Run several full cycles: the slots must clear every time, and no value from a previous pair may reappear.
When does one shared timestamp still not fix the formula? #
Alignment only joins records; a wrong register, a scaling error, a failed read, or a stale value still yields a wrong result, so keep value validity checks separate from the pairing logic. Work the remaining faults by symptom:
- Equal timestamps, wrong magnitude: the fault sits in the register, the scaling or the decoding, and no amount of alignment corrects it — trace the raw device value first.
- Both slots filling from one device: the branch identities are not distinct; two arrivals from the faster device must not count as a pair, which is exactly what the per-device keys prevent.
- Node output aligned, stored records not: the publishing path is re-stamping the message; feed it the field and the resolution it actually consumes.
- Formula result drifts through the day: the age ceiling is too wide for the process rate; recompute it from the current drift and shrink the discard limit with it.
The decision: pair the two readings in one Function node, copy the designated device's time across the pair, publish both records together, size the discard limit from the process rate — 15 s in the worked example — and treat any remaining wrong value as a separate fault at the register, the scaling and the read result.
Questions asked
What happens to a half-filled pair when the Node-RED runtime restarts?
The default context store lives in memory, so a restart or full deploy empties both slots. Nothing corrupted survives: the first reading that arrives after the restart simply sits in its slot until its partner arrives, and the first published pair after the restart is a fresh one. If you need the buffer to survive restarts, move the slots to a persistent context store, and then your discard limit becomes essential, because a reading that sat through a restart is old by definition.
Can the built-in Join node replace the hand-written Function node?
The built-in Join node merges the messages that arrive within its window into one combined message. That suits one downstream consumer, but this application needs two separate messages, each shaped for its own publishing branch, carrying one shared time value. A Function node returns an array of messages, so it can emit the pair; the Join node outputs one message. Community nodes such as node-red-contrib-join-wait can also do the waiting, but check that their output shape matches what your publishing branches consume.
Which device's timestamp should the pair carry?
Pick one designated device and keep the policy fixed, rather than letting whichever message arrives last decide. A last-arrival policy makes the pair time wander by up to one poll interval from cycle to cycle, which shows up as jitter in trends built on the formula. With a fixed policy the pair time advances in regular steps, equal to the poll interval, and any irregular step points at a late or missing read on the designated branch.
Does the time field need seconds or milliseconds?
Unix time is conventionally seconds elapsed since 00:00:00 UTC on 1 January 1970, while JavaScript's Date.now() call returns milliseconds. Read one stored record and compare it against a known wall-clock minute: a value near 1.7 billion is seconds, a value near 1.7 trillion is milliseconds. Convert once, in the branch that takes the reading, and copy the complete value through the pair; rounding or reformatting in the publishing path is how equal-looking times end up unequal.
Does the pattern extend to three or more devices?
Yes. Give every branch its own key in the context object and require every key to be present before the node emits. Two consequences follow: the wait grows to the slowest device each cycle, so your discard limit applies to every slot, and the shared timestamp still comes from the one designated branch. With three branches polled every 5 s, a slot can wait two full intervals for the last partner, so re-derive the age ceiling from the process rate before widening the pair.

