Store Node-RED Sensor Data in InfluxDB 2.x and Chart It in Grafana
A live gauge answers “what is the latest reading?” A time-series database helps answer “what changed over the last hour, day or week?” Node-RED can validate messages and write measurements into InfluxDB, while Grafana can query that history and display trends.
This guide describes the InfluxDB 2.x line-protocol/write API path. InfluxDB 3 and other major releases have different setup and API details, so follow documentation for the exact version you install. The example assumes Node-RED receives JSON sensor data over MQTT, but a serial or HTTP input can use the same validation-and-write pattern.
1. Design measurements before wiring nodes
Choose a stable measurement name, tags for low-cardinality identifiers such as device and room, and numeric fields for readings. Example: measurement environment, tags device=esp32-01, fields temperature_c=24.8 and humidity_pct=51.0.
Do not use rapidly changing or unbounded identifiers as tags; that can increase series cardinality and storage cost. Keep units consistent. Don't log a failed sensor read as 0 or invent a value just to keep a chart continuous. Track last-seen status separately from measurements.
2. Prepare InfluxDB 2.x
In InfluxDB 2.x, create an organization, bucket and API token with the permissions needed for writing to that bucket. Keep the token on the Node-RED host in protected configuration/environment variables, not in a public flow export or browser code. Use a narrow token rather than an administrator token.
The write endpoint follows this general pattern:
/api/v2/write?org=YOUR_ORG&bucket=YOUR_BUCKET&precision=s
Use the host and protocol of your own InfluxDB instance. If TLS is used, ensure certificate validation is configured correctly. Do not copy another person's API token or leave the HTTP write endpoint publicly accessible without a security design.
3. Build the Node-RED write path
Connect MQTT In to JSON parsing, validate the payload and then format line protocol for InfluxDB. The example Function node expects the properties temperature_c and humidity_pct to already be finite numbers. It uses a Node-RED environment variable named INFLUX_TOKEN for the server-side token.
Add an HTTP Request node after the Function. Configure it to POST to the InfluxDB 2.x write URL for your organization and bucket, using the line protocol body from msg.payload. Check the HTTP status and route failures to a Debug/error path. The example's device tag is normalized for a restricted ID format; if you allow arbitrary tag values, escape line-protocol special characters correctly rather than concatenating raw user input.
4. Connect Grafana and build a time-series chart
Add the InfluxDB 2.x data source in Grafana and configure its URL, organization, bucket/query mode and a read-capable token. Keep read and write permissions separate where possible. Exact setup options depend on Grafana and InfluxDB versions, so use the matching official docs.
Start with a time-series panel for temperature and one for humidity. Use a time range that includes your test samples, verify the query returns data, and label units. A blank chart may indicate no successful writes, an incorrect time range, a wrong bucket/org, or a query that selects the wrong measurement/field.
5. Retention and data quality
Choose a retention policy that matches the project: a hobby lab may keep short-term readings, while an environmental installation may need longer history. Before keeping data indefinitely, estimate sample rate, field count and retention volume. Consider downsampling/aggregation for long-range trend charts.
Treat missing or invalid readings as missing data, not zeros. Show device availability and last received time separately. For large message rates, batch writes where supported and avoid writing duplicate values more frequently than the project needs.
Troubleshooting
HTTP 401/403: check token permissions and authentication header configuration without printing the token.
HTTP 400: check line-protocol syntax, field types, organization, bucket and precision.
Grafana is empty: test recent writes first, verify selected bucket/data source and widen the time range.
Values appear in the wrong series: check measurement/tag/field names and confirm tags are not changing unnecessarily.
Charts show zero gaps: review validation and sensor-error handling; don't fabricate measurements to hide a failure.