Function won't populate tag in Influx database

I am an absolute beginner with Node-Red but I have had some success with the help of Open-AI to suggest how things should be configured.
What I am trying to do is populate an Influx Database from a MQ_TT source and it works fine as long as everything is fields. My fields are value_num, value_str and topic. Topic will be populated by a string similar to "meteorcams/nz0014/starcount". As I said, this all works if all three are fields but I want topic to be a tag for indexing and sorting. But no matter what I have tried, the tag will not populate in the database. The function I am using is as follows:

// ---------- Node-RED Function: MQTT → InfluxDB v1.x ----------
//
// Input examples:
// meteorcams/nz0014/nextcapstart : "2025-10-26 07:11:02.642132 UTC"
// meteorcams/nz001p/meteorcount  : 123
//
// Output for InfluxDB v1.x Out node:
//   msg.measurement = "messagesV5"
//   msg.tags = { topic: "meteorcams/nz001p/meteorcount" }
//   msg.payload = {
//       value_num: 123,              // if numeric
//       value_str: "2025-10-26 ..."  // if string
//   }
//
// ------------------------------------------------------------

let topic = msg.topic || "";

// Normalise to lowercase, keep the FULL topic path (no truncation)
let topicTag = topic.toLowerCase();

// Build the field payload
let payload = {};
if (typeof msg.payload === "number" || (!isNaN(msg.payload) && msg.payload !== "")) {
    payload.value_num = Number(msg.payload);
} else {
    payload.value_str = String(msg.payload);
}

// Assign InfluxDB v1.x message structure
msg.measurement = "messagesV5";
msg.tags = { topic: topicTag };
msg.payload = payload;

return msg;

I need some human help. Open-AI just goes around in circles

Welcome to the group/forum.

For the first day or so you are limited to how many posts you can make.

I know that feeling. :wink:

(Sorry I can't help though)

1 Like

Look at the help text for the influx node in the right hand pane in the node red, that tells you how you need to construct msg.payload for fields and tags. If I remember correctly you need it to be an array of two elements, the first being an object containing the fields and the second being an object containing the tags. Check that though, I am not at my PC at the moment. If you ask AI how to build such a payload you may get better results

First, though, I recommend watching this playlist: Node-RED Essentials. The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain. AI can be very helpful, but it is much more helpful if you understand better what you want to do.

2 Likes

Thanks Colin.
Excellent advise. The notation used in Node-Red is only about 2 days old in my experience. I have relied on OpenAI to write me the code and have got good results so far. However it has been stubbonly difficult with this one. It goes in circles and even tells lies. :laughing:
I am aware of what you say about the two elements and looking back at the code I posted, I see

msg.tags = { topic: topicTag };
msg.payload = payload;

might be the wrong way around. When I get back to this I might try that. I'd kick myself if it is that simple.
Thanks for the link. I will watch that.
Peter

If you feed the output of the function into a Debug node then in the debug pane you need to see , in message.paylad, something like
[{field1: 32, field2: 7}, {tag1: "tag1_string", tag2: "tag2_strings"}]

Note that that is an array not a string. Also make sure the the fields are numbers (not strings) and the tags are strings.

For example, this is what one of my messages looks like

Here's some screen dumps of what I have


You need to get it into the format I posted.

Thanks Colin.. I solved it. You helped me focus on the array of 2 elements and I could see that msg.tag was outside the payload. The notation is still a bit too dense for me at this stage so I copied and pasted the help into ChatGPT which came up with this

// ---------- Node-RED Function: MQTT → InfluxDB v1.x ----------
//
// Input examples:
// meteorcams/nz0014/nextcapstart : "2025-10-26 07:11:02.642132 UTC"
// meteorcams/nz001p/meteorcount  : 123
//
// Output for InfluxDB v1.x Out node:
//   msg.measurement = "messagesV5"
//   msg.payload = [
//       { value_num: 123 },                       // fields object
//       { topic: "meteorcams/nz001p/meteorcount" } // tags object
//   ]
//
// ------------------------------------------------------------

let topic = msg.topic || "";

// Normalise to lowercase, keep the FULL topic path
let topicTag = topic.toLowerCase();

// Build the fields object
let fields = {};
if (typeof msg.payload === "number" || (!isNaN(msg.payload) && msg.payload !== "")) {
    fields.value_num = Number(msg.payload);
} else {
    fields.value_str = String(msg.payload);
}

// Build the tags object
let tags = { topic: topicTag };

// Assign measurement and payload array
msg.measurement = "messagesV5";
msg.payload = [fields, tags];

return msg;

and it works. I now have the tag topic in the database. It is still reporting a NaN error but I think I will ignore that for the moment as it has no effect on what I am doing. I will deal with that when I start to understand this notation a bit better.
Thanks for the HUMAN help!! :smiley:

What does the output of the function node look like in a debug node?

Looks like it should