How-to
From Modbus TCP payload to InfluxDB fields in Node-RED
Short answer
Map the Modbus TCP payload array in a Node-RED function node to one named, correctly scaled InfluxDB field per register, and write it with the influx out node. When a register must be stored as an integer or boolean field, post line protocol to the InfluxDB HTTP write endpoint instead, because the influx out node stores those values as floats or strings.

On this page
Modbus TCP hands Node-RED an unlabeled array of register counts, and InfluxDB wants named fields. Put a function node between the Modbus getter and the influx out node, build one object property per register from a verified register map, and apply only a scale factor that map documents. That is the whole logging chain, and it has exactly one branch: the influx out node stores numeric values as floats or strings, so when a register must land as a true integer or boolean field, the same object goes out as line protocol through an HTTP Request node instead. Below are the mapping code with its bench test, a worked ten-register example, and the decision between the two write paths.
Confirm what the getter delivers #
Before any code, look at the message. Connect a Debug node directly to the getter's output, set it to show the complete msg object, trigger one read, and confirm three things: which property carries the values — with the Modbus getter from node-red-contrib-modbus this is normally an array in msg.payload — how many elements one read returns, and whether that order holds across several reads. The contribution's own examples make a point that matters here: one Modbus response per data source, because read, write and getter outputs do not share a single response node, so give each poll its own response handling. When the registers are read as one grouped block, the array arrives as one coherent snapshot of the slave's registers; what one grouped Modbus request guarantees goes deeper on that. Register order is the mapping's foundation: a reordered read or a firmware change silently moves every name onto the wrong value.
Map the payload array to named fields #
Work in this order:
- Write the register map before any code: for each payload position, its signal name, its unit and its scale, taken from the PLC's register documentation. Name fields for the queries you plan to run, not for the wire; the field-versus-tag split for PLC data is laid out in the InfluxDB tag and field schema for a PLC historian dashboard. One limit on what this database is for: a logged gas or oxygen reading is a record for analysis, while alarm and shutdown logic on those signals stays in the PLC.
- Build the mapping in a function node.
fieldNamesis a string array you maintain from the map,scaleis a number,fieldsstarts as an empty object, andmsg.payloadmust be an array of numbers:
// Illustrative names you maintain; msg.payload is the documented
// Node-RED property that carries the getter's register array.
const fieldNames = ["temp1", "temp2", "temp3"]; // one entry per register map line
const scale = 100; // keep only where the register map documents it
const fields = {}; // becomes one named property per register
if (!Array.isArray(msg.payload) || msg.payload.length !== fieldNames.length) {
node.error("payload length does not match the register map", msg);
return null;
}
for (let i = 0; i < fieldNames.length; i++) {
fields[fieldNames[i]] = msg.payload[i] / scale;
}
msg.payload = fields; // the influx out node expects a named-field object here
return msg;
- Bench test before any database is attached: drive the function from an inject node carrying
[123, 89, 10]as JSON. The debug panel on the function output must show temp1 at 1.23, temp2 at 0.89 and temp3 at 0.1 — that proves the key mapping and the divisor together, on counts that carry no unit until the register map assigns one. - Prove the guard: shorten the injected array by one element and confirm the function reports the mismatch through
node.errorinstead of writing partial data. - Connect the influx out node only now, point it at a test database, and leave the debug on the function output while the first rows land.
Two notes on the code. Registers with different scales cannot share one divisor, so when the map mixes them, keep name and scale together and index both arrays in the same loop. And the divisor in this pattern is an assumption carried in the classic version of the flow, not a Modbus rule — the next section works the check that decides it.
Worked example: a ten-register payload with a divide-by-100 scale #
The inputs. One Modbus TCP read returns ten raw register counts in msg.payload, positions 0 through 9; a count carries no unit until the map assigns one. The register map prepared for this flow names nine signals — four temperatures, four humidity values and one pressure — covering positions 0 through 8. The classic version of the flow assumed a divisor of 100 for the temperature registers, and the PLC's register definition is the only source that confirms or replaces that number. A bench read shows position 0 at raw 1997.
The rule, in words: engineering value equals raw count divided by the documented scale, one unique field name per payload position, and the name list length must equal the payload length.
The arithmetic, written out. Nine names cover positions 0 through 8 and position 9 has none, so every write stores nine fields and discards the tenth count: ten values minus nine names is one value lost on every poll, with no error raised. For the scale, if the register definition confirms 100 counts per degree Celsius, position 0 logs as 1997 / 100 = 19.97 °C; without that definition, 1997 is only a count that could as easily be a scaled value, a different signal or a shifted index.
The result: nine fields in the database, one register permanently absent from it, and a temperature that is correct only while the documented scale really is 100.
The decision, with its threshold: extend the map to ten verified names before the first production write, or drop the spare register deliberately and record why — a name list shorter than the payload silently discards data, so the runtime check demands equal lengths. Keep the divisor of 100 only for registers whose definition documents it, and log status words and counters raw.
Write integer and boolean fields over HTTP #
The influx out node writes floats and strings cleanly. Numbers that should be integers reach the database as floats or strings depending on how the write is set up, and boolean values fare no better — and on Modbus, integer and boolean registers are ordinary. It breaks when the type carries meaning: an event counter queried with type-sensitive aggregations, or a state bit you need as true and false rather than a decimal.
Line protocol types each field explicitly: an integer carries a trailing i, for example count=23i; a boolean is written t, f, true or false; a float is a plain decimal; a string is enclosed in double quotes. On an InfluxDB 1.x server, set an HTTP Request node to POST with the database named in the query string — for example http://192.168.1.50:8086/write?db=plant — and send the line protocol text as the raw body, not wrapped in JSON; a 2.x server moves the write to its versioned API endpoint with a bucket, an organization and a token instead. One body can carry several lines, one point per line; an array pushed as a single line leaves a single point behind — the trap described in why InfluxDB keeps only the last record of a Node-RED array write.
| Requirement | Write path |
|---|---|
| Float and string fields answer every query the dashboard runs | Function node into the influx out node |
| An integer or boolean field type must survive storage | Function node into an HTTP Request node posting line protocol |
Decide the write path before the first production write: it is a schema decision about how the fields will be typed, not a preference between two nodes.
Verify the path before you trust it #
Test the chain in pieces, then as one piece. Compare stored values against the PLC's own display of the same registers at the same moment, and query the test database back over the bench-test window to confirm the field names, the values and the scaling you intended. Then send one controlled sample per field type through the production path and read it back, checking that an integer sent as an integer behaves as one in a query. A successful write is not verification: a write succeeds just as readily with a shifted index, a wrong divisor, or an integer that landed as a float. Once those checks pass, the flow can run unattended.
Questions asked
Can a change node do this mapping instead of a function node?
Yes. A change node that sets msg.payload to a JSONata object with one named key per register performs the same array-to-fields mapping, and it suits a short, fixed register list. For ten or more registers, a runtime length check or a documented scale factor, the function node is easier to keep in step with the register map and to test on the bench.
How do I log the same registers from several PLCs?
Keep the field names identical on every machine and separate the devices with a tag instead, for example a machine or line identifier attached as a tag on each point. Tags split the series so each PLC keeps its own history, while one function node, one field map and one set of queries serve the whole fleet.
Why does an integer write fail with a field type conflict?
InfluxDB fixes a field's type at its first write. If earlier rows stored that field as a float, a line protocol write declaring it as an integer is rejected as a type conflict, which is what happens when a flow moves from the influx out node's float output to integer line protocol. Write the integer version to a new measurement, or remove the old series after exporting the history you still need.
How do I log a Modbus coil or discrete input?
A coil or discrete input is a single bit. Convert its array position to a boolean in the function node by comparing the value against zero, then send it through the HTTP route so the boolean type reaches the database. Keep status bits in their own boolean fields rather than mixing them into float fields, so queries that count states stay dependable.


