Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. Skripten / Logik
    4. Node-Red
    5. SPS => Node-red => MYSQL mehrere Daten

    NEWS

    • ioBroker@Smart Living Forum Solingen, 14.06. - Agenda added

    • ioBroker goes Matter ... Matter Adapter in Stable

    • Monatsrückblick - April 2025

    SPS => Node-red => MYSQL mehrere Daten

    This topic has been deleted. Only users with topic management privileges can see it.
    • T
      Tim84 last edited by

      Hallo Leute,

      ich möchte folgende Aufgabe mit Node-Red bei mir zu Hause realisieren. Ich habe an meiner Wärmepumpe ein Siemens PAC3200 verbaut, diesen lese ich mit einer SPS (1500CPU) aus und schreibe die Werte in einen Datenbaustein. Auf der SPS läuft ein OPC-UA Server, den ich mit Node-Red auslese und dann in eine MYSQL DB schreibe. Das funktioniert mit einzelnen Werte auch schon ohne Probleme.

      ein wert.jpg
      Ob das alles so optimal ist weiß ich nicht 😉
      Jetzt möchte ich aber die 21 Werte aus dem Datenbaustein alles auslesen und in die SQL Datenbank schreiben.
      SPS-DB.jpg
      Ich habe da schon diverse Sachen ausprobiert, aber nichts hat so funktioniert. Ich hatte mir überleget für alle Daten einen eigenen payload anzulegen z.B. msgL1 usw. und die Variablen dann per Topic String mit "insert in" in die Datenbank zu schreiben.
      Daten schreiben sql.jpg
      Daten lesen.jpg

      Das kann ja eigentlich nicht so schwer sein. Für einen Tip oder ein Beispiel mit zwei Daten wäre ich dankbar.

      Lg Tim

      [
          {
              "id": "26d40d242fae4652",
              "type": "tab",
              "label": "Flow 1",
              "disabled": false,
              "info": "",
              "env": []
          },
          {
              "id": "21a603b8ddfa0115",
              "type": "function",
              "z": "26d40d242fae4652",
              "name": "Abfrage Werte",
              "func": "\nvar msgL1 = {};\nvar msgL2 = {};\nvar msgL3 = {};\nvar msgI1 = {};\nvar msgI2 = {};\nvar msgI3 = {};\nvar msgPg = {};\n\n\nmsgL1.topic = `ns=3;s=\"DB-Test\".PacHeizung.\"Spannung L1-N\"`;\nmsgL2.topic = `ns=3;s=\"DB-Test\".PacHeizung.\"Spannung L2-N\"; datatype = objekt`;\nmsgL3.topic = `ns=3;s=\"DB-Test\".PacHeizung.\"Spannung L3-N\"; datatype = objekt`;\nmsgI1.topic = `ns=3;s=\"DB-Test\".PacHeizung.\"Strom L1\"; datatype = objekt`;\nmsgI2.topic = `ns=3;s=\"DB-Test\".PacHeizung.\"Strom L2\"; datatype = objekt`;\nmsgI3.topic = `ns=3;s=\"DB-Test\".PacHeizung.\"Strom L3\"; datatype = objekt`;\nmsgPg.topic = `ns=3;s=\"DB-Test\".PacHeizung.\"Gesamtwirkleistung\"; datatype = objekt`;\n\nreturn [msgL1, msgL2, msgL3, msgI1, msgI2, msgI3, msgPg];",
              "outputs": 7,
              "timeout": 0,
              "noerr": 0,
              "initialize": "",
              "finalize": "",
              "libs": [],
              "x": 300,
              "y": 180,
              "wires": [
                  [
                      "896c18aa8bcbf184"
                  ],
                  [],
                  [],
                  [],
                  [],
                  [],
                  [
                      "896c18aa8bcbf184"
                  ]
              ]
          },
          {
              "id": "62ee487dbd535d54",
              "type": "inject",
              "z": "26d40d242fae4652",
              "name": "",
              "props": [
                  {
                      "p": "payload"
                  },
                  {
                      "p": "topic",
                      "vt": "str"
                  }
              ],
              "repeat": "",
              "crontab": "",
              "once": false,
              "onceDelay": 0.1,
              "topic": "",
              "payload": "",
              "payloadType": "date",
              "x": 100,
              "y": 180,
              "wires": [
                  [
                      "21a603b8ddfa0115"
                  ]
              ]
          },
          {
              "id": "896c18aa8bcbf184",
              "type": "OpcUa-Client",
              "z": "26d40d242fae4652",
              "endpoint": "aa0b0b1ec465be5d",
              "action": "read",
              "deadbandtype": "a",
              "deadbandvalue": 1,
              "time": 10,
              "timeUnit": "s",
              "certificate": "n",
              "localfile": "",
              "localkeyfile": "",
              "securitymode": "None",
              "securitypolicy": "None",
              "folderName4PKI": "",
              "useTransport": false,
              "maxChunkCount": 1,
              "maxMessageSize": 8192,
              "receiveBufferSize": 8192,
              "sendBufferSize": 8192,
              "name": "SPS",
              "x": 570,
              "y": 180,
              "wires": [
                  [
                      "9feef9c4efb5ecc1",
                      "6b2e635dedb8a4e0",
                      "337b4b50896ff60e",
                      "678f94dc25a7a009"
                  ],
                  []
              ]
          },
          {
              "id": "9feef9c4efb5ecc1",
              "type": "function",
              "z": "26d40d242fae4652",
              "name": "insertValue",
              "func": "/*\nmsg.payload = {}\nvar msgL1={};\n//var msgL2={};\n//var msgL3={};\n//var msgI1={};\n//var msgI2={};\nvar msgI3={};\n//var msgPg={};\n\n//msg.sps = msg.payload;\nmsg.payload.WERT1=msgL1;\nmsg.payload.WERT2=msgI3;\nmsg.topic =\"INSERT INTO test (`r-wert-2`,`r-wert1`) VALUES (:WERT1,:WERT2);\"\nreturn msg;\n\nmsg.sps = msg.payload;\nmsg.payload = {}\nmsg.payload.WERT1 = msg.sps;\nmsg.topic = \"INSERT INTO test (`r-wert-2`) VALUES (:WERT1);\"\nreturn msg; \n*/\n\nmsg.payload = {}\nvar msgL1 = {};\nvar msgPg={};\n\nif (msgL1.topic == `ns= 3; s = \"DB-Test\".PacHeizung.\"Spannung L1-N\"`)\n{ \n    msgL1.payload=msg.payload;\n    //msgL1.topic=\"\"\n    //return [msgL1,null];\n}\n\nif (msgPg.topic == `ns=3;s=\"DB - Test\".PacHeizung.\"Gesamtwirkleistung\"; datatype = objekt`) \n{\n    msgPg.payload = msg.payload;\n    //msgL1.topic=\"\"\n    //return [msgL1,null];\n}\n\nmsg.sps = msg.payload;\nmsg.payload.WERT1 = msgL1;\nmsg.payload.WERT2 = msgPg;\nmsg.topic = \"INSERT INTO test (`r-wert-2`,`r-wert1`) VALUES (:WERT1,:WERT2);\"\n\nreturn [msgL1, msgPg];\n",
              "outputs": 1,
              "timeout": 0,
              "noerr": 0,
              "initialize": "",
              "finalize": "",
              "libs": [],
              "x": 810,
              "y": 180,
              "wires": [
                  [
                      "d4d90c319292393d",
                      "4b7e50ec17c817af",
                      "ebfaad7c20f6ab22"
                  ]
              ]
          },
          {
              "id": "d4d90c319292393d",
              "type": "debug",
              "z": "26d40d242fae4652",
              "name": "insert payload",
              "active": true,
              "tosidebar": true,
              "console": false,
              "tostatus": false,
              "complete": "payload",
              "targetType": "msg",
              "statusVal": "",
              "statusType": "auto",
              "x": 1000,
              "y": 60,
              "wires": []
          },
          {
              "id": "4b7e50ec17c817af",
              "type": "debug",
              "z": "26d40d242fae4652",
              "name": "insert topic",
              "active": true,
              "tosidebar": true,
              "console": false,
              "tostatus": false,
              "complete": "true",
              "targetType": "full",
              "statusVal": "",
              "statusType": "auto",
              "x": 990,
              "y": 20,
              "wires": []
          },
          {
              "id": "6b2e635dedb8a4e0",
              "type": "debug",
              "z": "26d40d242fae4652",
              "name": "sps topic",
              "active": true,
              "tosidebar": true,
              "console": false,
              "tostatus": false,
              "complete": "topic",
              "targetType": "msg",
              "statusVal": "",
              "statusType": "auto",
              "x": 760,
              "y": 20,
              "wires": []
          },
          {
              "id": "337b4b50896ff60e",
              "type": "debug",
              "z": "26d40d242fae4652",
              "name": "sps payload",
              "active": true,
              "tosidebar": true,
              "console": false,
              "tostatus": false,
              "complete": "payload",
              "targetType": "msg",
              "statusVal": "",
              "statusType": "auto",
              "x": 770,
              "y": 60,
              "wires": []
          },
          {
              "id": "ebfaad7c20f6ab22",
              "type": "debug",
              "z": "26d40d242fae4652",
              "name": "insert komplett",
              "active": true,
              "tosidebar": true,
              "console": false,
              "tostatus": false,
              "complete": "true",
              "targetType": "full",
              "statusVal": "",
              "statusType": "auto",
              "x": 1000,
              "y": 100,
              "wires": []
          },
          {
              "id": "678f94dc25a7a009",
              "type": "debug",
              "z": "26d40d242fae4652",
              "name": "sps komplett",
              "active": true,
              "tosidebar": true,
              "console": false,
              "tostatus": false,
              "complete": "true",
              "targetType": "full",
              "statusVal": "",
              "statusType": "auto",
              "x": 770,
              "y": 100,
              "wires": []
          },
          {
              "id": "2dd8113895eb655b",
              "type": "mysql",
              "z": "26d40d242fae4652",
              "mydb": "a210a46b27a492e3",
              "name": "SQL  DB",
              "x": 1160,
              "y": 180,
              "wires": [
                  []
              ]
          },
          {
              "id": "aa0b0b1ec465be5d",
              "type": "OpcUa-Endpoint",
              "endpoint": "opc.tcp://192.168.178.3:4840",
              "secpol": "None",
              "secmode": "None",
              "none": true,
              "login": false,
              "usercert": false,
              "usercertificate": "",
              "userprivatekey": ""
          },
          {
              "id": "a210a46b27a492e3",
              "type": "MySQLdatabase",
              "name": "SQL1",
              "host": "127.0.0.1",
              "port": "3306",
              "db": "node-red-test",
              "tz": "",
              "charset": "UTF8"
          }
      ]
      
      mickym 1 Reply Last reply Reply Quote 0
      • mickym
        mickym Most Active @Tim84 last edited by mickym

        @tim84 Ich würde halt alles in einem Objekt wegspeichern (JSON String) und nicht das Objekt auseinanderreißen. Wenn aus der SPS Node ein Objekt rauskommt. Leider wird hier wieder mit irgendwelchen Code und Function Nodes rumhantiert. Wie gesagt, wenn ein Objekt rauskommt, würde ich das auch insgesamt wegschreiben.

        Oder zumindest würde ich alles in einer payload sammeln. Ggf. mit einer JOIN Node. Wie gesagt ohne Daten hilft dieser Flow nichts. ich hab auch kein SPS.

        T 1 Reply Last reply Reply Quote 0
        • T
          Tim84 @mickym last edited by

          @mickym Danke für die Antwort.

          Ich brauche am ende ja einen String den ich zur SQL schicke. Ich denke es müsste so aussehen:

          msg.topic ="INSERT INTO test (`SpannungL1-N`,`SpannungL2-N`,`SpannungL3-N´) VALUES (:WERT1,:WERT2,:WERT3);"
          

          Vorher muss ich die Daten aus der SPS auslesen und in Variablen (hier als Beispiel WERT1, WERT2 und WERT3) speichern. Könnte das so ablaufen:

          SPS(Datenbaustein) ein Wert auslesen => Payload in Variable1 speichern,
          danach
          SPS(Datenbaustein) ein Wert auslesen => Payload in Variable2 speichern,
          usw....

          Auf der SPS läuft ein OPC_UA Server, entweder ich hole die Daten nacheinander oder gleichzeitig. Am liebsten würde ich das alles ohne Funktionen machen. Du sagtest alles in ein Objekt speichern (JASON String), wie geht so etwas? Was bringt mir das?

          LG Tim

          mickym 1 Reply Last reply Reply Quote 0
          • mickym
            mickym Most Active @Tim84 last edited by

            @tim84 Nun frag doch alles gleichzeitig ab in Deiner Node (oder halt soviel Du willst) , dann hast Du doch bereits alle Werte in einer payload in einem Objekt. Das kannst Du doch mit einer json Node in einen String verwandeln und als einen Wert in die Datenbank wegschreiben.

            T 1 Reply Last reply Reply Quote 0
            • T
              Tim84 @mickym last edited by

              @mickym Genau das habe ich ja versucht.
              Unbenannt.jpg

              Wie funktioniert das denn mit einer Jason Node? Alle Werte mit einem mal auszulesen wäre natürlich die beste Variante.

              Danke schonmal für die Hilfe 😉

              mickym 1 Reply Last reply Reply Quote 0
              • mickym
                mickym Most Active @Tim84 last edited by mickym

                @tim84 Nun die functionNode - würde ich mir ehrlich gesagt sparen. Wenn Du hinter die SPS Node dir die DebugNodes anschaust bzw. weißt dass Du 7 Werte abfragst, dann kommt doch jedesmal ein unterschiedliches topic raus.

                Wenn Du also anstelle Deine Funktion Node (im Moment kannst Du es ja parallel machen) eine JOIN Node hängst und sagst es müssen alle 7 Nachrichten angekommen sein (und diese auch ein unterschiedliches topic haben), dann hast Du erst mal alles in einem Objekt.

                Wie gesagt wenn Du es in der SPS Node nicht schaffst alles aufeinmal abzufragen - dann halt mit einer JOIN Node in ein Objekt sammeln. Ich habe das mal exemplarisch gemacht:

                bf8f4f3d-5971-4220-a6fc-7db1b92d8308-image.png

                Wenn alle Werte eingetrudelt sind, dann gibt die JOIN Node erst das komplette Objekt frei und das kannst Du mit einer JSON Node in einen String verwandeln und so in die Datenbank schreiben. Genauso kannst Du es bei wieder auslesen aus dem String wieder in ein Objekt zurückverwandeln.

                Hier der Flow zum Ausprobieren:

                [
                   {
                       "id": "901ac30b4a5b8f8c",
                       "type": "change",
                       "z": "517cd16e4d7e6ae4",
                       "name": "Wert1",
                       "rules": [
                           {
                               "t": "set",
                               "p": "topic",
                               "pt": "msg",
                               "to": "Wert1",
                               "tot": "str"
                           },
                           {
                               "t": "set",
                               "p": "payload",
                               "pt": "msg",
                               "to": "$random()",
                               "tot": "jsonata"
                           }
                       ],
                       "action": "",
                       "property": "",
                       "from": "",
                       "to": "",
                       "reg": false,
                       "x": 590,
                       "y": 2700,
                       "wires": [
                           [
                               "a680c9c4dc75437c"
                           ]
                       ]
                   },
                   {
                       "id": "76127f4ac134f071",
                       "type": "change",
                       "z": "517cd16e4d7e6ae4",
                       "name": "Wert2",
                       "rules": [
                           {
                               "t": "set",
                               "p": "topic",
                               "pt": "msg",
                               "to": "Wert2",
                               "tot": "str"
                           },
                           {
                               "t": "set",
                               "p": "payload",
                               "pt": "msg",
                               "to": "$random()",
                               "tot": "jsonata"
                           }
                       ],
                       "action": "",
                       "property": "",
                       "from": "",
                       "to": "",
                       "reg": false,
                       "x": 590,
                       "y": 2740,
                       "wires": [
                           [
                               "a680c9c4dc75437c"
                           ]
                       ]
                   },
                   {
                       "id": "801994a60d9b9707",
                       "type": "change",
                       "z": "517cd16e4d7e6ae4",
                       "name": "Wert3",
                       "rules": [
                           {
                               "t": "set",
                               "p": "topic",
                               "pt": "msg",
                               "to": "Wert3",
                               "tot": "str"
                           },
                           {
                               "t": "set",
                               "p": "payload",
                               "pt": "msg",
                               "to": "$random()",
                               "tot": "jsonata"
                           }
                       ],
                       "action": "",
                       "property": "",
                       "from": "",
                       "to": "",
                       "reg": false,
                       "x": 590,
                       "y": 2780,
                       "wires": [
                           [
                               "a680c9c4dc75437c"
                           ]
                       ]
                   },
                   {
                       "id": "d1f7f72e3139393f",
                       "type": "change",
                       "z": "517cd16e4d7e6ae4",
                       "name": "Wert4",
                       "rules": [
                           {
                               "t": "set",
                               "p": "topic",
                               "pt": "msg",
                               "to": "Wert4",
                               "tot": "str"
                           },
                           {
                               "t": "set",
                               "p": "payload",
                               "pt": "msg",
                               "to": "$random()",
                               "tot": "jsonata"
                           }
                       ],
                       "action": "",
                       "property": "",
                       "from": "",
                       "to": "",
                       "reg": false,
                       "x": 590,
                       "y": 2820,
                       "wires": [
                           [
                               "a680c9c4dc75437c"
                           ]
                       ]
                   },
                   {
                       "id": "771b4c70f9901304",
                       "type": "change",
                       "z": "517cd16e4d7e6ae4",
                       "name": "Wert5",
                       "rules": [
                           {
                               "t": "set",
                               "p": "topic",
                               "pt": "msg",
                               "to": "Wert5",
                               "tot": "str"
                           },
                           {
                               "t": "set",
                               "p": "payload",
                               "pt": "msg",
                               "to": "$random()",
                               "tot": "jsonata"
                           }
                       ],
                       "action": "",
                       "property": "",
                       "from": "",
                       "to": "",
                       "reg": false,
                       "x": 590,
                       "y": 2860,
                       "wires": [
                           [
                               "a680c9c4dc75437c"
                           ]
                       ]
                   },
                   {
                       "id": "d9edd02fbbc2d08b",
                       "type": "change",
                       "z": "517cd16e4d7e6ae4",
                       "name": "Wert6",
                       "rules": [
                           {
                               "t": "set",
                               "p": "topic",
                               "pt": "msg",
                               "to": "Wert6",
                               "tot": "str"
                           },
                           {
                               "t": "set",
                               "p": "payload",
                               "pt": "msg",
                               "to": "$random()",
                               "tot": "jsonata"
                           }
                       ],
                       "action": "",
                       "property": "",
                       "from": "",
                       "to": "",
                       "reg": false,
                       "x": 590,
                       "y": 2900,
                       "wires": [
                           [
                               "a680c9c4dc75437c"
                           ]
                       ]
                   },
                   {
                       "id": "2540f33fb2060205",
                       "type": "change",
                       "z": "517cd16e4d7e6ae4",
                       "name": "Wert7",
                       "rules": [
                           {
                               "t": "set",
                               "p": "topic",
                               "pt": "msg",
                               "to": "Wert7",
                               "tot": "str"
                           },
                           {
                               "t": "set",
                               "p": "payload",
                               "pt": "msg",
                               "to": "$random()",
                               "tot": "jsonata"
                           }
                       ],
                       "action": "",
                       "property": "",
                       "from": "",
                       "to": "",
                       "reg": false,
                       "x": 590,
                       "y": 2940,
                       "wires": [
                           [
                               "a680c9c4dc75437c"
                           ]
                       ]
                   },
                   {
                       "id": "211826c4f22bb378",
                       "type": "inject",
                       "z": "517cd16e4d7e6ae4",
                       "name": "",
                       "props": [
                           {
                               "p": "trigger",
                               "v": "true",
                               "vt": "bool"
                           }
                       ],
                       "repeat": "",
                       "crontab": "",
                       "once": false,
                       "onceDelay": 0.1,
                       "topic": "",
                       "x": 350,
                       "y": 2800,
                       "wires": [
                           [
                               "901ac30b4a5b8f8c",
                               "76127f4ac134f071",
                               "d1f7f72e3139393f",
                               "771b4c70f9901304",
                               "d9edd02fbbc2d08b",
                               "2540f33fb2060205",
                               "801994a60d9b9707"
                           ]
                       ]
                   },
                   {
                       "id": "70c86d75b857e71d",
                       "type": "debug",
                       "z": "517cd16e4d7e6ae4",
                       "name": "Einzelabfragen",
                       "active": true,
                       "tosidebar": true,
                       "console": false,
                       "tostatus": false,
                       "complete": "payload",
                       "targetType": "msg",
                       "statusVal": "",
                       "statusType": "auto",
                       "x": 900,
                       "y": 2880,
                       "wires": []
                   },
                   {
                       "id": "8f011400430d9104",
                       "type": "join",
                       "z": "517cd16e4d7e6ae4",
                       "name": "",
                       "mode": "custom",
                       "build": "object",
                       "property": "payload",
                       "propertyType": "msg",
                       "key": "topic",
                       "joiner": "\\n",
                       "joinerType": "str",
                       "accumulate": false,
                       "timeout": "",
                       "count": "7",
                       "reduceRight": false,
                       "reduceExp": "",
                       "reduceInit": "",
                       "reduceInitType": "",
                       "reduceFixup": "",
                       "x": 870,
                       "y": 2820,
                       "wires": [
                           [
                               "85e3168072c64808",
                               "bf5a298324354c6b"
                           ]
                       ]
                   },
                   {
                       "id": "85e3168072c64808",
                       "type": "debug",
                       "z": "517cd16e4d7e6ae4",
                       "name": "Ein Objekt mit allen Werten",
                       "active": true,
                       "tosidebar": true,
                       "console": false,
                       "tostatus": false,
                       "complete": "payload",
                       "targetType": "msg",
                       "statusVal": "",
                       "statusType": "auto",
                       "x": 1100,
                       "y": 2780,
                       "wires": []
                   },
                   {
                       "id": "bf5a298324354c6b",
                       "type": "json",
                       "z": "517cd16e4d7e6ae4",
                       "name": "",
                       "property": "payload",
                       "action": "",
                       "pretty": false,
                       "x": 1050,
                       "y": 2820,
                       "wires": [
                           [
                               "c479da5181e0f8a4"
                           ]
                       ]
                   },
                   {
                       "id": "c479da5181e0f8a4",
                       "type": "debug",
                       "z": "517cd16e4d7e6ae4",
                       "name": "Als String in die DB",
                       "active": true,
                       "tosidebar": true,
                       "console": false,
                       "tostatus": false,
                       "complete": "payload",
                       "targetType": "msg",
                       "statusVal": "",
                       "statusType": "auto",
                       "x": 1270,
                       "y": 2820,
                       "wires": []
                   },
                   {
                       "id": "a680c9c4dc75437c",
                       "type": "junction",
                       "z": "517cd16e4d7e6ae4",
                       "x": 760,
                       "y": 2820,
                       "wires": [
                           [
                               "70c86d75b857e71d",
                               "8f011400430d9104"
                           ]
                       ]
                   }
                ]
                

                Du musst nicht msg.topic als Schlüssel nehmen du kannst auch Dein spstopic als Schlüssel für die Eigenschaften des Objektes verwenden.

                T 1 Reply Last reply Reply Quote 0
                • T
                  Tim84 @mickym last edited by

                  @mickym 0 Vielen Dank. Ich schaue es mir an den Feiertagen an.

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post

                  Support us

                  ioBroker
                  Community Adapters
                  Donate

                  1.0k
                  Online

                  31.7k
                  Users

                  79.7k
                  Topics

                  1.3m
                  Posts

                  2
                  7
                  605
                  Loading More Posts
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes
                  Reply
                  • Reply as topic
                  Log in to reply
                  Community
                  Impressum | Datenschutz-Bestimmungen | Nutzungsbedingungen
                  The ioBroker Community 2014-2023
                  logo