# Read values from S7 component to mysql query

**URL:** <https://discourse.nodered.org/t/read-values-from-s7-component-to-mysql-query/17048>\
**Category:** General\
**Created:** [23 October 2019 10:39 UTC](https://discourse.nodered.org/t/read-values-from-s7-component-to-mysql-query/17048 "2019-10-23T10:39:08Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![ViruSoft](https://avatars.discourse-cdn.com/v4/letter/v/c77e96/32.png) [@ViruSoft](https://discourse.nodered.org/u/ViruSoft)\
**Post date:** [23 October 2019 10:39 UTC](https://discourse.nodered.org/t/read-values-from-s7-component-to-mysql-query/17048/1 "2019-10-23T10:39:08Z")

</div>

Hi, I am new at Node-Red.  
I make a project but, I can read values for write to mysql.  
Every thing is working only I can't find correct way.

Now: I am using S7 component to read data from PLC, I can watch on dashboard,  
how can I get data to mysql query.

S7 read data = DB1,Real4.

//I try to write qurey;  
var str1 = msg.payload;  
str1 = str1[0]['DB1,Real4'];  
msg.payload = str1;  
msg.topic = "INSERT into Temps(Temp1) VALUES ('str1')"; //  
return msg;

Now Node-red giveing error =  
[error] [function:SQL Query] TyperError: Cannot read property 'DB1,Real4' of undefined

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [23 October 2019 10:42 UTC](https://discourse.nodered.org/t/read-values-from-s7-component-to-mysql-query/17048/2 "2019-10-23T10:42:47Z")

</div>

and what does an example of msg.payload look like?

---

<div class="post-metadata">

**Author:** ![ViruSoft](https://avatars.discourse-cdn.com/v4/letter/v/c77e96/32.png) [@ViruSoft](https://discourse.nodered.org/u/ViruSoft)\
**Post date:** [23 October 2019 11:06 UTC](https://discourse.nodered.org/t/read-values-from-s7-component-to-mysql-query/17048/3 "2019-10-23T11:06:00Z")

</div>

msg.payload = str1;

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [23 October 2019 11:10 UTC](https://discourse.nodered.org/t/read-values-from-s7-component-to-mysql-query/17048/4 "2019-10-23T11:10:20Z")

</div>

if msg.payload really does equal str1  
then it isn't an array so your line  
`str1 = str1[0]['DB1,Real4'];` makes no sense

---

<div class="post-metadata">

**Author:** ![ViruSoft](https://avatars.discourse-cdn.com/v4/letter/v/c77e96/32.png) [@ViruSoft](https://discourse.nodered.org/u/ViruSoft)\
**Post date:** [23 October 2019 11:20 UTC](https://discourse.nodered.org/t/read-values-from-s7-component-to-mysql-query/17048/5 "2019-10-23T11:20:23Z")

</div>

how, can I solve?

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [23 October 2019 11:26 UTC](https://discourse.nodered.org/t/read-values-from-s7-component-to-mysql-query/17048/6 "2019-10-23T11:26:29Z")

</div>

I'd start by reading the docs especially the "working with messages"

When you have done that, then add debug nodes to your flow to understand what message you are actually sending through your flow, as it doesn't look to be what you think it is.

---

<div class="post-metadata">

**Author:** ![ViruSoft](https://avatars.discourse-cdn.com/v4/letter/v/c77e96/32.png) [@ViruSoft](https://discourse.nodered.org/u/ViruSoft)\
**Post date:** [23 October 2019 11:45 UTC](https://discourse.nodered.org/t/read-values-from-s7-component-to-mysql-query/17048/7 "2019-10-23T11:45:16Z")

</div>

[  
{  
"id": "e5a6eef7.11771",  
"type": "tab",  
"label": "Flow 1",  
"disabled": false,  
"info": ""  
},  
{  
"id": "1a3ac55b.d95c2b",  
"type": "ui\_text\_input",  
"z": "e5a6eef7.11771",  
"name": "",  
"label": "Havuz 3 Sıcaklık",  
"tooltip": "",  
"group": "4d86b472.71234c",  
"order": 3,  
"width": 0,  
"height": 0,  
"passthru": true,  
"mode": "number",  
"delay": 300,  
"topic": "",  
"x": 410,  
"y": 120,  
"wires": [  
[  
"e6ced9bd.45c368"  
]  
]  
},  
{  
"id": "fcca8334.20aa3",  
"type": "ui\_text\_input",  
"z": "e5a6eef7.11771",  
"name": "",  
"label": "Havuz 1 Sıcaklık",  
"tooltip": "",  
"group": "4d86b472.71234c",  
"order": 1,  
"width": 0,  
"height": 0,  
"passthru": true,  
"mode": "number",  
"delay": 300,  
"topic": "",  
"x": 410,  
"y": 40,  
"wires": [  
[  
"19eaee6e.2b5192"  
]  
]  
},  
{  
"id": "e83cde1d.b1998",  
"type": "ui\_text\_input",  
"z": "e5a6eef7.11771",  
"name": "",  
"label": "Havuz 2 Sıcaklık",  
"tooltip": "",  
"group": "4d86b472.71234c",  
"order": 2,  
"width": 0,  
"height": 0,  
"passthru": true,  
"mode": "number",  
"delay": 300,  
"topic": "",  
"x": 410,  
"y": 80,  
"wires": [  
[  
"21b262d4.1348ee"  
]  
]  
},  
{  
"id": "e34c47ef.90d4b8",  
"type": "s7 in",  
"z": "e5a6eef7.11771",  
"endpoint": "cbbd9acd.931358",  
"mode": "single",  
"variable": "Sicaklık 1",  
"diff": false,  
"name": "",  
"x": 100,  
"y": 40,  
"wires": [  
[  
"fcca8334.20aa3",  
"81bfdf.a5c8602"  
]  
]  
},  
{  
"id": "db61f3a0.477db",  
"type": "s7 in",  
"z": "e5a6eef7.11771",  
"endpoint": "cbbd9acd.931358",  
"mode": "single",  
"variable": "Sicaklık 2",  
"diff": false,  
"name": "",  
"x": 100,  
"y": 80,  
"wires": [  
[  
"e83cde1d.b1998"  
]  
]  
},  
{  
"id": "74177299.019e6c",  
"type": "s7 in",  
"z": "e5a6eef7.11771",  
"endpoint": "cbbd9acd.931358",  
"mode": "single",  
"variable": "Sicaklık 3",  
"diff": false,  
"name": "",  
"x": 100,  
"y": 120,  
"wires": [  
[  
"1a3ac55b.d95c2b"  
]  
]  
},  
{  
"id": "9c6cea66.980d08",  
"type": "s7 out",  
"z": "e5a6eef7.11771",  
"endpoint": "cbbd9acd.931358",  
"variable": "Ust Pozisyon",  
"name": "",  
"x": 650,  
"y": 580,  
"wires":   
},  
{  
"id": "ae4f2e8c.bb35a",  
"type": "ui\_switch",  
"z": "e5a6eef7.11771",  
"name": "",  
"label": "Üst Pozisyon",  
"tooltip": "",  
"group": "4d86b472.71234c",  
"order": 5,  
"width": "0",  
"height": "0",  
"passthru": true,  
"decouple": "false",  
"topic": "",  
"style": "",  
"onvalue": "true",  
"onvalueType": "bool",  
"onicon": "",  
"oncolor": "",  
"offvalue": "false",  
"offvalueType": "bool",  
"officon": "",  
"offcolor": "",  
"x": 330,  
"y": 580,  
"wires": [  
[  
"9c6cea66.980d08"  
]  
]  
},  
{  
"id": "7e8ab299.d453dc",  
"type": "ui\_switch",  
"z": "e5a6eef7.11771",  
"name": "",  
"label": "Alt Pozisyon",  
"tooltip": "",  
"group": "4d86b472.71234c",  
"order": 6,  
"width": "0",  
"height": "0",  
"passthru": true,  
"decouple": "false",  
"topic": "",  
"style": "",  
"onvalue": "true",  
"onvalueType": "bool",  
"onicon": "",  
"oncolor": "",  
"offvalue": "false",  
"offvalueType": "bool",  
"officon": "",  
"offcolor": "",  
"x": 330,  
"y": 620,  
"wires": [  
[  
"51c2d502.bc7ffc"  
]  
]  
},  
{  
"id": "51c2d502.bc7ffc",  
"type": "s7 out",  
"z": "e5a6eef7.11771",  
"endpoint": "cbbd9acd.931358",  
"variable": "Alt Pozisyon",  
"name": "",  
"x": 650,  
"y": 620,  
"wires":   
},  
{  
"id": "19eaee6e.2b5192",  
"type": "s7 out",  
"z": "e5a6eef7.11771",  
"endpoint": "cbbd9acd.931358",  
"variable": "Sicaklık 1",  
"name": "",  
"x": 680,  
"y": 40,  
"wires":   
},  
{  
"id": "21b262d4.1348ee",  
"type": "s7 out",  
"z": "e5a6eef7.11771",  
"endpoint": "cbbd9acd.931358",  
"variable": "Sicaklık 2",  
"name": "",  
"x": 690,  
"y": 80,  
"wires":   
},  
{  
"id": "e6ced9bd.45c368",  
"type": "s7 out",  
"z": "e5a6eef7.11771",  
"endpoint": "cbbd9acd.931358",  
"variable": "Sicaklık 3",  
"name": "",  
"x": 680,  
"y": 120,  
"wires":   
},  
{  
"id": "54010d27.a7aa84",  
"type": "s7 in",  
"z": "e5a6eef7.11771",  
"endpoint": "cbbd9acd.931358",  
"mode": "single",  
"variable": "Sicaklık 1",  
"diff": false,  
"name": "",  
"x": 420,  
"y": 460,  
"wires": [  
[  
"c23bcae2.265248"  
]  
]  
},  
{  
"id": "c23bcae2.265248",  
"type": "ui\_chart",  
"z": "e5a6eef7.11771",  
"name": "",  
"group": "4d86b472.71234c",  
"order": 9,  
"width": 0,  
"height": 0,  
"label": "Sıcaklık1/Sıcaklık2",  
"chartType": "pie",  
"legend": "true",  
"xformat": "HH:mm:ss",  
"interpolate": "linear",  
"nodata": "",  
"dot": true,  
"ymin": "",  
"ymax": "",  
"removeOlder": "2",  
"removeOlderPoints": "",  
"removeOlderUnit": "1",  
"cutout": "50",  
"useOneColor": false,  
"colors": [  
"#0080ff",  
"#ff0000",  
"#ff7f0e",  
"#2ca02c",  
"#98df8a",  
"#d62728",  
"#ff9896",  
"#9467bd",  
"#c5b0d5"  
],  
"useOldStyle": true,  
"outputs": 1,  
"x": 650,  
"y": 480,  
"wires": [  
  
]  
},  
{  
"id": "3fa40b11.416ba4",  
"type": "s7 in",  
"z": "e5a6eef7.11771",  
"endpoint": "cbbd9acd.931358",  
"mode": "single",  
"variable": "Ust Pozisyon",  
"diff": false,  
"name": "",  
"x": 140,  
"y": 580,  
"wires": [  
[  
"ae4f2e8c.bb35a"  
]  
]  
},  
{  
"id": "d4f41d3d.49699",  
"type": "s7 in",  
"z": "e5a6eef7.11771",  
"endpoint": "cbbd9acd.931358",  
"mode": "single",  
"variable": "Alt Pozisyon",  
"diff": false,  
"name": "",  
"x": 140,  
"y": 640,  
"wires": [  
[  
"7e8ab299.d453dc"  
]  
]  
},  
{  
"id": "35ca352c.f2927a",  
"type": "s7 in",  
"z": "e5a6eef7.11771",  
"endpoint": "cbbd9acd.931358",  
"mode": "single",  
"variable": "Sicaklık 3",  
"diff": false,  
"name": "",  
"x": 420,  
"y": 500,  
"wires": [  
[  
"c23bcae2.265248"  
]  
]  
},  
{  
"id": "3288ffab.4b723",  
"type": "mysql",  
"z": "e5a6eef7.11771",  
"mydb": "884b10b0.746d7",  
"name": "",  
"x": 640,  
"y": 360,  
"wires": [  
[  
"adeb306d.754ed"  
]  
]  
},  
{  
"id": "adeb306d.754ed",  
"type": "debug",  
"z": "e5a6eef7.11771",  
"name": "",  
"active": true,  
"tosidebar": true,  
"console": false,  
"tostatus": false,  
"complete": "payload",  
"targetType": "msg",  
"x": 650,  
"y": 400,  
"wires":   
},  
{  
"id": "45b211b6.5d71c",  
"type": "function",  
"z": "e5a6eef7.11771",  
"name": "SQL Query",  
"func": "msg.topic = "INSERT into Temps(Temp1) VALUES ('"+srt0+"')";\nreturn msg;",  
"outputs": 1,  
"noerr": 0,  
"x": 650,  
"y": 320,  
"wires": [  
[  
"3288ffab.4b723"  
]  
]  
},  
{  
"id": "34f7799f.c8efa6",  
"type": "inject",  
"z": "e5a6eef7.11771",  
"name": "",  
"topic": "",  
"payload": "",  
"payloadType": "date",  
"repeat": "",  
"crontab": "",  
"once": false,  
"onceDelay": 0.1,  
"x": 360,  
"y": 280,  
"wires": [  
[  
"45b211b6.5d71c"  
]  
]  
},  
{  
"id": "81bfdf.a5c8602",  
"type": "change",  
"z": "e5a6eef7.11771",  
"name": "",  
"rules": [  
{  
"t": "set",  
"p": "deneme",  
"pt": "global",  
"to": "srt0",  
"tot": "str"  
}  
],  
"action": "",  
"property": "",  
"from": "",  
"to": "",  
"reg": false,  
"x": 630,  
"y": 260,  
"wires": [  
[  
"45b211b6.5d71c"  
]  
]  
},  
{  
"id": "4d86b472.71234c",  
"type": "ui\_group",  
"z": "",  
"name": "HAT-1 Kontrol",  
"tab": "7cfecf52.cfe63",  
"order": 1,  
"disp": true,  
"width": "6",  
"collapse": true  
},  
{  
"id": "cbbd9acd.931358",  
"type": "s7 endpoint",  
"z": "",  
"transport": "iso-on-tcp",  
"address": "192.168.0.1",  
"port": "102",  
"rack": "0",  
"slot": "1",  
"localtsaphi": "01",  
"localtsaplo": "00",  
"remotetsaphi": "01",  
"remotetsaplo": "00",  
"connmode": "rack-slot",  
"adapterauto": true,  
"adapterport": "",  
"busaddr": "",  
"adapteraddr": "0",  
"cycletime": "2000",  
"timeout": "1500",  
"verbose": "off",  
"name": "",  
"vartable": [  
{  
"addr": "M223.0",  
"name": "Ust Pozisyon"  
},  
{  
"addr": "DB1,Real4",  
"name": "Sicaklık 1"  
},  
{  
"addr": "DB1,Real12",  
"name": "Sicaklık 2"  
},  
{  
"addr": "DB1,Real20",  
"name": "Sicaklık 3"  
},  
{  
"addr": "M223.2",  
"name": "Alt Pozisyon"  
}  
]  
},  
{  
"id": "884b10b0.746d7",  
"type": "MySQLdatabase",  
"z": "",  
"host": "192.168.0.250",  
"port": "3306",  
"db": "TrOtom",  
"tz": ""  
},  
{  
"id": "7cfecf52.cfe63",  
"type": "ui\_tab",  
"z": "",  
"name": "SCADA",  
"icon": "dashboard",  
"disabled": false,  
"hidden": false  
}  
]

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [23 October 2019 13:50 UTC](https://discourse.nodered.org/t/read-values-from-s7-component-to-mysql-query/17048/8 "2019-10-23T13:50:36Z")

</div>

Please export your flow again, [read this post](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506) and edit your post.

First determine the debug output of your node.

This line:  
`msg.topic = "INSERT into Temps(Temp1) VALUES ('str1')"; //`

`str1` inserted as a string, not the data/object/array that you are looking for.

It should be something like

`VALUES ('"+str1+"')"`

---

<div class="post-metadata">

**Author:** ![ViruSoft](https://avatars.discourse-cdn.com/v4/letter/v/c77e96/32.png) [@ViruSoft](https://discourse.nodered.org/u/ViruSoft)\
**Post date:** [23 October 2019 15:16 UTC](https://discourse.nodered.org/t/read-values-from-s7-component-to-mysql-query/17048/9 "2019-10-23T15:16:18Z")

</div>

yes I know, I already fix it, and I write my known methods but not working,  
I'd export all flow to here.  
Thanks for your help.

---

<div class="post-metadata">

**Author:** ![ViruSoft](https://avatars.discourse-cdn.com/v4/letter/v/c77e96/32.png) [@ViruSoft](https://discourse.nodered.org/u/ViruSoft)\
**Post date:** [28 October 2019 20:33 UTC](https://discourse.nodered.org/t/read-values-from-s7-component-to-mysql-query/17048/10 "2019-10-28T20:33:41Z")

</div>

![Node1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/3/300903e9f9b78c82cdbbbacdfc3b47eeffb05136.jpeg)

I need to help an Urgent please.

split function not working,  
I need to get all datas values and split as data1, data2, data3....  
so, write by sql query to data rows.

Thank you.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [28 October 2019 20:40 UTC](https://discourse.nodered.org/t/read-values-from-s7-component-to-mysql-query/17048/11 "2019-10-28T20:40:09Z")

</div>

First determine what the input is.

Next it states: m.split, which is not shown in your screenshots, where does m.split come from?

---

<div class="post-metadata">

**Author:** ![ViruSoft](https://avatars.discourse-cdn.com/v4/letter/v/c77e96/32.png) [@ViruSoft](https://discourse.nodered.org/u/ViruSoft)\
**Post date:** [28 October 2019 21:02 UTC](https://discourse.nodered.org/t/read-values-from-s7-component-to-mysql-query/17048/12 "2019-10-28T21:02:05Z")

</div>

I am trying in function node, and try every thing wrote at internet from pages.  
I have only one function node and 5 data input,  
I need to sperate datas and write diffrent rows.

this is my trys;

let m = msg.payload  
let s = m.split(';')  
let out =[]  
for(x=0;x\<s.length;x++){  
parts = s[x].split(':')  
out.push({payload:{[parts[0]]:parseFloat(parts[1])}})  
}

return out

var myVars=msg.payload.split(";");  
var result={};  
myVars.forEach(function(v){  
var parts=v.split(":");  
result [parts[0]]=parts[1];  
})  
msg.payload=result;

var keys=Object.keys(msg.payload);  
var msgs=keys.map(function(key){  
return {topic: key , payload: msg.payload[key]};  
})

var output=msg.payload[0].split(";");  
var msg1={payload:(output[0])};  
var msg2={payload:(ouput[1])};  
var msg3={payload:(output[2])};

return [msg1,msg2,msg3];

//var outputMsgs = [];  
//var words = msg.payload.split(" ");  
// for (var w in words) {  
// outputMsgs.push({payload:words[w]});  
// }  
// return [outputMsgs];

//doSomeAsyncWork(msg, function(result) {  
// node.send({payload:result});  
//});  
//return;

//var temp = msg.payload.value[0];  
[//msg.topic](https://msg.topic) = "INSERT into Temps(Temp1) VALUES ('"+temp+"')";  
//return msg;

//var newMsg = msg.payload.split(',');  
// temp1 = { payload: newMsg[0]};  
//var temp2 = { payload: newMsg[1]};

[//msg.topic](https://msg.topic) = "INSERT into Temps(Temp1,Temp2) VALUES ('"+temp1+","+temp2+"')";  
//return msg;

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [28 October 2019 21:10 UTC](https://discourse.nodered.org/t/read-values-from-s7-component-to-mysql-query/17048/13 "2019-10-28T21:10:35Z")

</div>

what is going into the function ? can you add a debug node to show us the msg.payload (and its type) going in ?

---

<div class="post-metadata">

**Author:** ![ViruSoft](https://avatars.discourse-cdn.com/v4/letter/v/c77e96/32.png) [@ViruSoft](https://discourse.nodered.org/u/ViruSoft)\
**Post date:** [28 October 2019 21:30 UTC](https://discourse.nodered.org/t/read-values-from-s7-component-to-mysql-query/17048/14 "2019-10-28T21:30:51Z")

</div>

![Node1%20(2)](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/0/00a5029cfd1d3537eb21611120b0e2fddadb2dbc.jpeg)  
 ![Node2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/f/fa660e71f767c435e48cd167d85d7ecbbba089f8.jpeg)

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [28 October 2019 21:59 UTC](https://discourse.nodered.org/t/read-values-from-s7-component-to-mysql-query/17048/15 "2019-10-28T21:59:17Z")

</div>

So you are receiving an object with several properties, one of which is a numeric value, so a simple string split isn't going to work. Maybe read this post [https://nodered.org/docs/user-guide/messages](https://nodered.org/docs/user-guide/messages) then you can work out how to access the part(s) you want.
