How-to
Build multidimensional arrays in Node-RED for InfluxDB writes
Short answer
Cache each subscription's last value per point, re-emit it on a 100 ms inject tick, batch 10 samples per point and join them into one multidimensional array, then format line protocol in a single function node so one influxdb out node writes the whole batch. Ten points at a 100 ms tick become one 100-line write per second; the demonstrated flow neared Node-RED's limit at 80 points.
On this page
- Prove the write path before batching
- Set every subscription to publish at the tick interval
- Cache the last value and emit one sample per tick
- Batch ten samples into ordered per-point arrays
- Combine per-point arrays into one multidimensional array
- Format line protocol and write once per batch
- Worked example: 10 points at the demonstrated tick
- Questions asked
Build the array in stages and write it once. Cache each subscription's last value in a change node keyed to that point, re-emit it on an inject tick at 100 ms, let a batch node hold 10 samples per point, and let two join nodes turn those groups into one multidimensional array: points along one dimension, samples along the other. A single function node formats that array as InfluxDB line protocol, and one influxdb out node writes the whole batch. On the demonstrated flow this replaced 100 separate writes per second with one 100-line write per second, cutting controller load on both the acquisition and the write side; 80 points at the same tick was where that flow approached Node-RED's practical limit.
Prove the write path before batching #
A flow that moves messages locally has not yet proved the destination stores them. Commission the path first:
- Confirm Node-RED and the controller's data-layer nodes run, then read one known value and compare it with the source. The value and the data type must agree.
- Configure the InfluxDB connection exactly as it will run — endpoint, authentication, storage — following the same steps as connecting Node-RED to InfluxDB 2.6 with the v2 API.
- Push one small write through the intended influxdb out node, then read it back and check identity, value and timestamp. A successful connection is not a stored value.
- Record the controller's CPU load with the connection active. That baseline is what separates acquisition load from write load when the flow grows.
Set every subscription to publish at the tick interval #
- Select the points — the demonstrated set is one PLC counter incrementing every 1 ms and nine slowly changing system values — and give each stream a stable identity for everything downstream.
- Use subscribe-style data-layer nodes rather than request-style polling: the demonstrated flow measured lower controller CPU with subscriptions publishing at a fixed interval.
- Configure every subscription with the same publication interval as the tick.
- Watch one changing value and one stationary value. The changing value arrives; the stationary one goes quiet. That silence is why the caching stage exists.
Two limits to accept up front. A counter incrementing every 1 ms, published at the tick interval, gives 10 observations of a running count per second, not a record of every increment, so state the sampling interval wherever the data is consumed. And a shared interval does not synchronize independent subscriptions; each value still arrives at its own time. If the application needs simultaneous measurements, synchronize at the source, not in Node-RED.
Cache the last value and emit one sample per tick #
Because subscriptions publish only on change, a periodic record needs a cache and a tick:
- One change node per point stores each arriving subscription value under its own context key, so updating one point never overwrites another.
- An inject node repeating at the tick interval drives a change node that copies the cached value into a message and forwards it — one sample per point per tick, whether or not the source changed.
- Hold each stream out of the batch path until its first valid subscription value arrives, so nothing uninitialized reaches storage.
- Hold a process value constant and confirm samples still flow at the tick rate.
| Flow element | Setting | Commissioning check |
|---|---|---|
| Subscribe node | publication interval matched to the tick | changing values update the right cache |
| Change node | latest value per point in its own key | one point's update never overwrites another |
| Inject node | repeat at the tick interval | ten consecutive ticks span about 0.9 s |
| Startup gate | first valid value before batching | no placeholder sample reaches storage |
Verify the tick you actually get. The inject node's documented repeat interval is set in seconds, minutes, hours or days with a minimum above 1 in the chosen unit, while the demonstrated flow runs a sub-second repeat — so after importing the flow, check the debug sidebar: across ten consecutive ticks, first to last should span about 0.9 s, which is nine tick intervals.
The tick proves sampling continuity, not freshness. A subscription that stops updating leaves its last value sitting in the cache, and the flow keeps writing it at every tick — a plausible number with no live source behind it. Monitor subscription status separately, and treat repeated identical values as a liveness question rather than an answer. It is the same class of silent fault as a historian storing zeros after an MQTT edge node reconnects.
Batch ten samples into ordered per-point arrays #
- Feed the periodic samples into a batch node configured to release a group of 10 messages.
- Inspect a released group at a debug node: exactly 10 entries.
- Let the first join node assemble each group into one array. Element order must equal arrival order — prove it with the fast counter and compare the array against the incoming sample sequence.
- Restart or pause the flow mid-group and observe whether the deployed configuration retains, clears or emits the partial group, then choose the behavior the destination can tolerate.
- Accept the latency and record around it: a full batch takes 1 s to collect, so stamp each sample's time at the tick if the destination must preserve individual timing. The write time cannot recover it.
Combine per-point arrays into one multidimensional array #
The second join node merges the per-point arrays into one array of arrays. Both dimensions have to survive the merge: which point each inner array belongs to, and which sample position each element holds. Verify on a small set — with the demonstrated 10 points you should see 10 inner arrays of 10 samples, and forcing a change on one point should move only its own row. Establish the segment on one stream, then duplicate it per point.
Format line protocol and write once per batch #
One function node walks the multidimensional array and emits one line protocol record per sample per point: measurement, tags identifying the point, the field value, and the sample time. Cast every value to the field type the destination expects inside the formatter — one malformed line can cost the whole batch. The formatted output goes to a single influxdb out node; replacing one output node per point with one shared node was the write-side CPU fix in the demonstrated flow. A different destination keeps everything upstream and changes only this function node: the 80-point demonstration ran the same acquisition, caching and batching with a formatter written for the destination's own line protocol.
Worked example: 10 points at the demonstrated tick #
Inputs, all from the demonstrated flow: a tick interval of 100 ms, a batch count of 10 samples per point, and 10 points — the 1 ms counter plus nine system values.
The rules in words: the sample rate per point is one divided by the tick interval; the batch period is the batch count times the tick; the lines per write are the points times the batch count; and the writes avoided are the points times the sample rate.
The arithmetic:
- 1 ÷ 0.100 s = 10 samples per second per point.
- 10 samples × 0.100 s = 1,000 ms = 1 s to fill a batch.
- 10 points × 10 samples = 100 lines in one write.
- 1 ÷ 1 s = 1 write per second from the single output node, replacing 10 points × 10 samples/s = 100 separate writes per second.
The same arithmetic at the demonstrated 80-point extension: 80 × 10 = 800 lines per write, one write per second replacing 800 per-point writes per second.
Decision: run one batched write per second and accept 1 s of collection latency as the price. Treat roughly 80 points at the demonstrated tick as the planning ceiling — that is where the demonstrated flow sat at Node-RED's practical limit — and above it, split the acquisition across flows or lengthen the tick instead of extending one flow. Confirm controller CPU against the baseline recorded during commissioning; deciding how much machine the sustained rate needs is the same calculation as sizing an edge PC for oversampled PLC data to InfluxDB.
Questions asked
How do I confirm the inject node is really ticking at the configured interval?
The inject node's documented repeat interval is entered in seconds, minutes, hours or days with a minimum above 1 in the chosen unit, while the demonstrated flow carries a 100 ms repeat in its configuration. After import, check the debug sidebar: the first and tenth of ten consecutive ticks should sit about 0.9 seconds apart. If they sit 9 seconds apart, the node is repeating at 1 s and every rate downstream is a tenth of what the sizing assumes.
What happens if one line in the batch is malformed?
The node-red-contrib-influxdb3 node documents that a batch write is all-or-nothing by default: one invalid line makes InfluxDB reject the entire batch, and the node offers a partial-write option that stores the valid lines instead. The cheaper discipline is to cast every value to the destination field type inside the formatting function node, so a bad sample never reaches the output node.
Does the cached value survive a restart?
Not reliably. File-based context stores data under ~/.node-red/context/ but keeps it in memory and writes to disk only every 30 seconds, so after a hard stop the file can hold a value up to 30 seconds old. Gate each stream out of the batch path until its first fresh subscription value arrives after a restart, and the stale file value never reaches storage.
How many points can one flow carry at the demonstrated tick?
Plan against 80 points as the ceiling. The demonstrated flow carried 80 points at 10 samples per second each with negligible loss and was close to Node-RED's practical limit at that count. Verify controller CPU as points are added; above the ceiling, split acquisition across two flows or lengthen the tick rather than extending one flow.
How do I keep individual sample times when the write happens once a second?
Stamp the time when the sample is generated, not when the batch is written; the write timestamp only says when the batch left the node. Since Node-RED 1.1.0 the inject node can set any message property, so the tick can carry its own timestamp property alongside the cached value, and the formatting function node can emit it as each line's timestamp.