thomassorensen2650 / node-red-contrib-mqtt-sparkplug-plus

A node that makes it simple to implement MQTT Sparkplug in Node-Red
21 stars 6 forks source link

Set metrics programatically #3

Closed alex-controlx closed 2 years ago

alex-controlx commented 2 years ago

Firstly, thank you for developing this valuable node!

A feature required in mqtt sparkplug device node: set metrics in programatically or via import and export from a csv file.

I have around 500 metrics and setting them up via the UI is very tedious.

Programatically it can be by sending a payload with an array of names and types for metrics.

thomassorensen2650 commented 2 years ago

Hi Alex,

I think the easiest way would be to export the flow to JSON and edit the object there directly.

I've added an undocumented and untested feature with no input checks whatsoever to version v1.1.0. It only works if you set the definition before birth is send. use with extreme caution, but you should be able to set metric definition like this:

Edit: updated example to show correct syntax

msg.definition = {
    "TEST": {
        "dataType": "Int32"
    }
}

msg.payload = {
    "metrics" : [
    {
        name : "TEST",
        value : 1
    }
    ]
}
return msg;

I'll see if I can get it fully implemented in a future release.

alex-controlx commented 2 years ago

Thank you for doing that. msg.definition will simplify my work.

I guess dataType property should have a value from the array at https://github.com/thomassorensen2650/node-red-contrib-mqtt-sparkplug-plus/blob/0543284ece8091372f22047b5f6e024bb0bb26b8/mqtt-sparkplug-plus.html#L355

On start I'll be sending the metrics definitions, then all metrics at last value, which should trigger the Device Birth message.

thomassorensen2650 commented 2 years ago

Correct, you can also send definition and the first payload in the same message.

thomassorensen2650 commented 2 years ago

This functionality has been fully implemented in version 1.2.0