Input data to Influxdb with dynamic fields

Hi,
I have been new to node-red and so far have managed to push data to InfluxDB with ease. I want to understand how to structure my payload better for Grafana visualization.

My payload looks like the following

[
	{
		"measurement": "device_0",
		"fields": {
			"Reading_0": 0.1177
		},
		"timestamp": 1636861200
	},
	{
		"measurement": "device_1",
		"fields": {
			"Reading_0": 0.02613000012934208,
			"Reading_1": -0.012897999957203865,
			.
			.
			"Reading_10": -0.012897999957203865,
		},
		"timestamp": 1636861200
	}
]

Different measurements could have different count of readings (as many 100). I wonder if there is a way to dynamically generate the "Reading_0", "Reading_1" tags. I'm using InfluxDB batch node to transform json to payload as below

payload.push(
{
	measurement: msg.payload.Telemetries[i].NAME + '_' + msg.payload.Telemetries[i].INDEX.toString(),
	fields: {
		Reading_0: msg.payload.Telemetries[i].Sensor.Channels[0].Reading
	},
	timestamp: msg.payload.Telemetries[i].Timestamp,
}

off topic

I am no influxdb expert, but as per influxdb documentation:

measurement acts as a container for tags

Remember that fields are not indexed, thus if you have 100 of them, it will slow your database down a lot if you cannot reference them. Use tags to assign the device names.

My home automation lives in "measurement": "homeautomation" and I differentiate the devices by using tags.

on topic

you can make them dynamic by using [key]:xxx

eg

payload.push(
{
	measurement: msg.payload.Telemetries[i].NAME + '_' + msg.payload.Telemetries[i].INDEX.toString(),
	fields: {
		[my_key_name]: msg.payload.Telemetries[i].Sensor.Channels[0].Reading
	},
	timestamp: msg.payload.Telemetries[i].Timestamp,
}

Normally you would be correct. If you have multiple devices (or machines or rooms or whatever) where each device records the same data to the database then you should use one measurement, with the device id as a tag. So for example you may have a number of rooms, each one of which has temperature, humidity. Then the fields would be temperature and humidity, and the tag would be the room.
In this case, however, it seems that the devices have different fields. One device may only have a few, another may have 100. If they were all put into the same measurement then there would be very many null samples in the database (for the fields that a device does not have), which is not efficient. So in this case I think the individual measurements is the correct solution.
If, however, the devices actually fall into a number of classes, where all the devices in that class have the same fields, then each class should have a measurement.

@mdmobashir I am interested in how you manage to have the data in that form from multiple devices.

Back on the original question, I would not use the batch node. I would split the message into multiple individual messages, using a split node, and then a very small amount of adjustment would be required to reformat the data for the Influx Out node.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.