# OPC-UA String to InfluxDB Object: Data conversion

**URL:** <https://discourse.nodered.org/t/opc-ua-string-to-influxdb-object-data-conversion/56912>\
**Category:** Industrial\
**Tags:** database, function-node\
**Created:** [19 January 2022 09:31 UTC](https://discourse.nodered.org/t/opc-ua-string-to-influxdb-object-data-conversion/56912 "2022-01-19T09:31:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![AgriSwanson](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/agriswanson/32/54528_2.png) [@AgriSwanson](https://discourse.nodered.org/u/AgriSwanson)\
**Post date:** [19 January 2022 09:31 UTC](https://discourse.nodered.org/t/opc-ua-string-to-influxdb-object-data-conversion/56912/1 "2022-01-19T09:31:16Z")

</div>

Hi,

Im working on a project where I am trying to log some data from a Siemens PLC to influx DB and then Grafana. In this setup - Node-Red gets the data from the PLC through OPC-UA. Since we are talking about 500+ signals, the inject node is configured to read a struct(UDT) from the PLC.

When I read from a flow sensor, i get the following result:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/a/5a50c2323384708c7cbaa3cd6a827ac14eaa62d0.png)

"53FT01" is my target in this example. This sensor is located in my control datablock "Ctr" within my "WaterSystem". The "53FT01" containes 5 signals:  
y - with the value of 0 [real]  
y filtered - with the value of 0 [real]  
YF - false [bool]  
AHH - false [bool]  
ALL - false [bool]

The problem I have is that i cant figure out how i can convert this string to a format that InfluxDB supports. Ideally, this data should be converted to the following:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/9/a9893e3135e093ef4a760cf5e27fc9c700cc8c0a.png)

Does anyone know how to do that?

```auto
[{"id":"9a3c1db7.d762b","type":"tab","label":"Flow 3","disabled":false,"info":""},{"id":"69ef9b23.eb4224","type":"debug","z":"9a3c1db7.d762b","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":650,"y":380,"wires":[]},{"id":"9aeebc5.ad6994","type":"function","z":"9a3c1db7.d762b","name":"multiple measurement points","func":"\n\nmsg.payload = [\n {\n measurement: \"M_WaterSystem_Ctrl\",\n tags:{\n location:\"53FT01 \"\n },\n fields: {\n Y: 0 ,\n Y_Filtered:0,\n YF: false,\n AHH:false ,\n ALL:false \n },\n timestamp: new Date()\n },\n\n];\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":380,"y":380,"wires":[["69ef9b23.eb4224"]]},{"id":"483fb851.b5b168","type":"inject","z":"9a3c1db7.d762b","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"ns=3;s=\"M_WaterSystem_Ctrl\".\"Ctrl\".53FT01","payload":"","payloadType":"date","x":130,"y":380,"wires":[["9aeebc5.ad6994"]]},{"id":"f812dfbc.ab6d8","type":"debug","z":"9a3c1db7.d762b","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"OPC","targetType":"msg","statusVal":"","statusType":"auto","x":320,"y":280,"wires":[]},{"id":"e1553bc2.fc21d8","type":"inject","z":"9a3c1db7.d762b","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"},{"p":"OPC","v":"{\"y\":0,\"y Filtered\":0,\"YF\":false,\"AHH\":false,\"ALL\":false}","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"ns=3;s=\"M_WaterSystem_Ctrl\".\"Ctrl\".\"53FT01\"","payload":"","payloadType":"date","x":130,"y":280,"wires":[["f812dfbc.ab6d8"]]},{"id":"d15033e4.36f9f","type":"comment","z":"9a3c1db7.d762b","name":"This is the output string i get from my OPCUA block","info":"This is the output string i get from my OPCUA block","x":230,"y":240,"wires":[]},{"id":"86d5cb47.3d3fe8","type":"comment","z":"9a3c1db7.d762b","name":"This is what i ideally should convert the OPC output to","info":"","x":240,"y":340,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![mikakaraila](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mikakaraila/32/37496_2.png) [@mikakaraila](https://discourse.nodered.org/u/mikakaraila)\
**Post date:** [6 March 2022 20:40 UTC](https://discourse.nodered.org/t/opc-ua-string-to-influxdb-object-data-conversion/56912/2 "2022-03-06T20:40:47Z")

</div>

Add function node to transform msg to newmsg:

// msg.payload = timestamp; // new Date()  
// msg.topic = ns=3;s="M\_WaterSystem\_Ctrl"."Ctrl"."53FT01";  
// msg.OPCU = {"y":0,"y Filtered":0,"YF":false,"AHH":false,"ALL":false};

var mea = msg.topic.substring(7);  
var l = msg.topic.lastIndexOf(".");  
var loc = msg.topic.substring(l+1);  
var fields = JSON.parse(msg.OPC);

var newmsg = {};

newmsg.topic = 'ns=3;s="M\_WaterSystem\_Ctrl"."Ctrl".53FT01';

var payload =  
{  
measurement: mea,  
tags:{  
location: loc  
},  
fields: {  
Y: fields.y,  
Y\_Filtered: fields["y Filtered"],  
YF: fields.YF,  
AHH: fields.AHH ,  
ALL: fields.ALL  
},  
timestamp: new Date()  
};  
newmsg.payload = ;  
newmsg.payload[0] = payload;

return newmsg;

---

<div class="post-metadata">

**Author:** ![mikakaraila](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mikakaraila/32/37496_2.png) [@mikakaraila](https://discourse.nodered.org/u/mikakaraila)\
**Post date:** [6 March 2022 20:46 UTC](https://discourse.nodered.org/t/opc-ua-string-to-influxdb-object-data-conversion/56912/3 "2022-03-06T20:46:01Z")

</div>

[{"id":"9a3c1db7.d762b","type":"tab","label":"Flow ","disabled":false,"info":""},{"id":"69ef9b23.eb4224","type":"debug","z":"9a3c1db7.d762b","name":"WANTED","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":720,"y":520,"wires":},{"id":"9aeebc5.ad6994","type":"function","z":"9a3c1db7.d762b","name":"multiple measurement points","func":"\n\nmsg.payload = [\n {\n measurement: "M\_WaterSystem\_Ctrl",\n tags:{\n location:"53FT01 "\n },\n fields: {\n Y: 0 ,\n Y\_Filtered:0,\n YF: false,\n AHH:false ,\n ALL:false \n },\n timestamp: new Date()\n },\n\n];\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":,"x":460,"y":520,"wires":[["69ef9b23.eb4224"]]},{"id":"483fb851.b5b168","type":"inject","z":"9a3c1db7.d762b","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"ns=3;s="M\_WaterSystem\_Ctrl"."Ctrl".53FT01","payload":"","payloadType":"date","x":210,"y":520,"wires":[["9aeebc5.ad6994"]]},{"id":"f812dfbc.ab6d8","type":"debug","z":"9a3c1db7.d762b","name":"OPC","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"OPC","targetType":"msg","statusVal":"","statusType":"auto","x":710,"y":280,"wires":},{"id":"e1553bc2.fc21d8","type":"inject","z":"9a3c1db7.d762b","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"},{"p":"OPC","v":"{"y":0,"y Filtered":0,"YF":false,"AHH":false,"ALL":false}","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"ns=3;s="M\_WaterSystem\_Ctrl"."Ctrl"."53FT01"","payload":"","payloadType":"date","x":130,"y":280,"wires":[["f812dfbc.ab6d8","0262c68aa0f98207"]]},{"id":"d15033e4.36f9f","type":"comment","z":"9a3c1db7.d762b","name":"This is the output string i get from my OPCUA block","info":"This is the output string i get from my OPCUA block","x":230,"y":240,"wires":},{"id":"86d5cb47.3d3fe8","type":"comment","z":"9a3c1db7.d762b","name":"This is what i ideally should convert the OPC output to","info":"","x":320,"y":480,"wires":},{"id":"0262c68aa0f98207","type":"function","z":"9a3c1db7.d762b","name":"","func":"// msg.payload = timestamp; // new Date()\n// msg.topic = ns=3;s="M\_WaterSystem\_Ctrl"."Ctrl"."53FT01";\n// msg.OPCU = {"y":0,"y Filtered":0,"YF":false,"AHH":false,"ALL":false};\n\nvar mea = msg.topic.substring(7);\nvar l = msg.topic.lastIndexOf(".");\nvar loc = msg.topic.substring(l+1);\nvar fields = JSON.parse(msg.OPC);\n\n\nvar newmsg = {};\n\nnewmsg.topic = 'ns=3;s="M\_WaterSystem\_Ctrl"."Ctrl".53FT01';\n\nvar payload =\n {\n measurement: mea,\n tags:{\n location: loc\n },\n fields: {\n Y: fields.y,\n Y\_Filtered: fields["y Filtered"],\n YF: fields.YF,\n AHH: fields.AHH ,\n ALL: fields.ALL \n },\n timestamp: new Date()\n };\nnewmsg.payload = ;\nnewmsg.payload[0] = payload;\n\nreturn newmsg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":,"x":510,"y":360,"wires":[["d546b2c09c7f0316"]]},{"id":"d546b2c09c7f0316","type":"debug","z":"9a3c1db7.d762b","name":"FIX","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":710,"y":360,"wires":}]

---

<div class="post-metadata">

**Author:** ![Giu-seppe](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/giu-seppe/32/40489_2.png) [@Giu-seppe](https://discourse.nodered.org/u/Giu-seppe)\
**Post date:** [5 May 2022 13:03 UTC](https://discourse.nodered.org/t/opc-ua-string-to-influxdb-object-data-conversion/56912/4 "2022-05-05T13:03:40Z")

</div>

This JSON is incorrect by copying and pasting it

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [4 July 2022 13:04 UTC](https://discourse.nodered.org/t/opc-ua-string-to-influxdb-object-data-conversion/56912/5 "2022-07-04T13:04:04Z")

</div>

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