Node-Red Sparkplub send JSON datatype

I am trying to set up function to send a JSON Obect to MQTT Broker using sparkplug B.

The function is:

var jsonObject = msg.payload;

// Convert JSON object to string
var jsonString = JSON.stringify(jsonObject);

var sparkplugBMessage = {
“metrics”: [
{
“name”:“RECIPE”,
“type”:12,
“value”: jsonString
}
]
};
// Set the stringified JSON as the new payload
msg.payload = sparkplugBMessage;
//msg.js=jsonObject
//msg.jsstring=jsonString
// Return the modified message
return msg;

The debug results are:

There is a little more going on here than what is in your post here.

The last part of the debug is what makes me think that… the error is that you cant convert some types (like ints) to upper case, so hence you are getting the TypeError typeString.toUpperCase is not a function.

The .toUpperCase command is not in your post, so must be elsewhere in your code?

The type error is related to the setting the data type in the node-red-contrib-sparkplug-plus node.

If i use the node with a data type of int or float it the message goes to the broker with no problem. The sparkplub b spec has the data type of 12 (string) and 16 (data set). I have tried both data types with with issues. I have tried to stringify the json and send it as a string.

Essensially the type errror is coming from the contrib-sparkplug-plus node. I not sure why,

Can you share a screenshot of the flow itself?

I’m wondering if it goes straight from this function node to the sparkplug node, and specifically which spB node are you using, the “device” node or the “out” node?

Also, is there a reason you are defining the type by the “type” property in the metrics? Have you tried removing that line?
Another thing to try would be to use the built-in dynamic metric definitions that you can see half way down on the node package page.

Terry first thanks. You gave me few things to try and now it starting to work.

Flow is pasted below. The link in takes and API call from postman to node red.

I am using the “device node” now and made the folloiwng adjustments to the metrics tab.

You asked: Also, is there a reason you are defining the type by the “type” property in the metrics? Have you tried removing that line?
Removing the type property in metrics removed the error. I am having a hard time understanding the where and when to use the type property. Is there a “preferred node” to communicate with broker ie should I be using the “out” node instead of the “device” node?

Glad you guys got it sorted.
On tip that took me a little while to really sink in…

image

Anything in blue in the debug tab is not a string.
Hence not being able to convert it to upper case…

Makes sense now. I misread / misunderstood the specs for the node and how to set the type. Next step is to install the Cirrus Snowflake Connector to have the MQTT data autopopulate into Snowflake and establish two way dynamic commication between our data warehouse and the epic.

Thanks for your help

2 Likes

Looking further into the datatype, it should only be used when initially creating the tag, updating the value goes to that existing tag so there’s no need to specify it each time.

Also just for future reference for anyone that comes to this thread in the future, there’s a list all the types and their associated integer code in section 14.2 of the Sparkplug Specification Rev 2.2

// Indexes of Data Types
// Unknown placeholder for future expansion.
Unknown = 0;
// Basic Types
Int8 = 1;
Int16 = 2;
Int32 = 3;
Int64 = 4;
UInt8 = 5;
UInt16 = 6;
UInt32 = 7;
UInt64 = 8;
Float = 9;
Double = 10;
Boolean = 11;
String = 12;
DateTime = 13;
Text = 14;
// Additional Metric Types
UUID = 15;
DataSet = 16;
Bytes = 17;
File = 18;
Template = 19;

// Additional PropertyValue Types
PropertySet = 20;
PropertySetList = 21;

1 Like