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. String Werte von Payload Gesamtwert extrahieren

    NEWS

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

    • ioBroker goes Matter ... Matter Adapter in Stable

    • Monatsrückblick - April 2025

    String Werte von Payload Gesamtwert extrahieren

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

      Hallo,

      ich habe einen Payload Wert als String der eine gewisse Gesamtlänge hat (wie in dem Beispiel 21 Zeichen).
      a6f5d6a1-9684-42e0-86bd-4112e311b6d6-grafik.png

      Problem daran ist, dass diverse Werte in einem String enthalten sind. Diese möchte ich aber einzeln filtern. Die einzelnen Werte haben aber eine bestimmte Länge: Von 0 bis Zeichen 8 = Wert1, Zeichen 9 bis 16 = Wert 2, 17 bis 23 = Wert 3 usw.

      Wie extrahiere ich hier die Werte heraus um sie danach als einzelnen Payload Wert zu haben?
      Muss ich dies über eine Function machen (und wenn ja, wie?) oder gibt es hier auch eine andere Möglichkeit?

      Anbei der Flow:

      [
          {
              "id": "a55fe9ca249ccdfc",
              "type": "tab",
              "label": "Flow 2",
              "disabled": false,
              "info": "",
              "env": []
          },
          {
              "id": "6bc19d64a873f88d",
              "type": "inject",
              "z": "a55fe9ca249ccdfc",
              "name": "",
              "props": [
                  {
                      "p": "payload"
                  },
                  {
                      "p": "topic",
                      "vt": "str"
                  }
              ],
              "repeat": "",
              "crontab": "",
              "once": false,
              "onceDelay": 0.1,
              "topic": "Teststring",
              "payload": "AAA     50     909   ",
              "payloadType": "str",
              "x": 380,
              "y": 260,
              "wires": [
                  [
                      "aba432bff1dc6402"
                  ]
              ]
          },
          {
              "id": "aba432bff1dc6402",
              "type": "debug",
              "z": "a55fe9ca249ccdfc",
              "name": "debug 21",
              "active": true,
              "tosidebar": true,
              "console": false,
              "tostatus": false,
              "complete": "false",
              "statusVal": "",
              "statusType": "auto",
              "x": 580,
              "y": 260,
              "wires": []
          }
      ]
      

      So wäre zumindest mein Ansatz in einer Function Node. Aber wie gesagt, evtl. gibt es ja noch eine bessere/schönere Lösung.

      let test = msg.payload;
      test = test.substring(0, 3);
      
      msg.payload = test;
      
      return msg;
      

      Vielen Dank

      Marc Berg 1 Reply Last reply Reply Quote 0
      • Marc Berg
        Marc Berg Most Active @_R_A_L_F_ last edited by Marc Berg

        @_r_a_l_f_
        Ganz ohne Skripting ginge es so, vorausgesetzt, die Längen sind konstant:

        [
            {
                "id": "dbc903ba0c1b42de",
                "type": "inject",
                "z": "256f595eacfe9f56",
                "name": "",
                "props": [
                    {
                        "p": "payload"
                    }
                ],
                "repeat": "",
                "crontab": "",
                "once": false,
                "onceDelay": 0.1,
                "topic": "",
                "payload": "   345    568    654",
                "payloadType": "str",
                "x": 330,
                "y": 220,
                "wires": [
                    [
                        "e58495251b0981a1"
                    ]
                ]
            },
            {
                "id": "24902cc7240130e1",
                "type": "change",
                "z": "256f595eacfe9f56",
                "name": "",
                "rules": [
                    {
                        "t": "set",
                        "p": "payload",
                        "pt": "msg",
                        "to": "$trim(payload)",
                        "tot": "jsonata"
                    }
                ],
                "action": "",
                "property": "",
                "from": "",
                "to": "",
                "reg": false,
                "x": 670,
                "y": 220,
                "wires": [
                    [
                        "46871b01accaa1e7"
                    ]
                ]
            },
            {
                "id": "e58495251b0981a1",
                "type": "split",
                "z": "256f595eacfe9f56",
                "name": "",
                "splt": "8",
                "spltType": "len",
                "arraySplt": 1,
                "arraySpltType": "len",
                "stream": false,
                "addname": "",
                "x": 490,
                "y": 220,
                "wires": [
                    [
                        "24902cc7240130e1"
                    ]
                ]
            },
            {
                "id": "46871b01accaa1e7",
                "type": "debug",
                "z": "256f595eacfe9f56",
                "name": "debug 9",
                "active": true,
                "tosidebar": true,
                "console": false,
                "tostatus": false,
                "complete": "true",
                "targetType": "full",
                "statusVal": "",
                "statusType": "auto",
                "x": 860,
                "y": 220,
                "wires": []
            }
        ]
        

        e4f79f69-f892-4838-80d7-88973ebed31d-grafik.png

        _ 1 Reply Last reply Reply Quote 0
        • _
          _R_A_L_F_ @Marc Berg last edited by

          @marc-berg Ok, Danke für den Ansatz aber leider sind die Werte unterschiedlich lang 😞

          Marc Berg 1 Reply Last reply Reply Quote 0
          • Marc Berg
            Marc Berg Most Active @_R_A_L_F_ last edited by Marc Berg

            @_r_a_l_f_ dann so, falls Leerzeichen dazwischen sein sollten

            [
                {
                    "id": "dbc903ba0c1b42de",
                    "type": "inject",
                    "z": "256f595eacfe9f56",
                    "name": "",
                    "props": [
                        {
                            "p": "payload"
                        }
                    ],
                    "repeat": "",
                    "crontab": "",
                    "once": false,
                    "onceDelay": 0.1,
                    "topic": "",
                    "payload": "   345    568    654",
                    "payloadType": "str",
                    "x": 550,
                    "y": 280,
                    "wires": [
                        [
                            "e58495251b0981a1"
                        ]
                    ]
                },
                {
                    "id": "e58495251b0981a1",
                    "type": "split",
                    "z": "256f595eacfe9f56",
                    "name": "",
                    "splt": " ",
                    "spltType": "str",
                    "arraySplt": 1,
                    "arraySpltType": "len",
                    "stream": false,
                    "addname": "",
                    "x": 710,
                    "y": 280,
                    "wires": [
                        [
                            "7411fd2b0d345418"
                        ]
                    ]
                },
                {
                    "id": "46871b01accaa1e7",
                    "type": "debug",
                    "z": "256f595eacfe9f56",
                    "name": "debug 9",
                    "active": true,
                    "tosidebar": true,
                    "console": false,
                    "tostatus": false,
                    "complete": "true",
                    "targetType": "full",
                    "statusVal": "",
                    "statusType": "auto",
                    "x": 1020,
                    "y": 280,
                    "wires": []
                },
                {
                    "id": "7411fd2b0d345418",
                    "type": "switch",
                    "z": "256f595eacfe9f56",
                    "name": "",
                    "property": "payload",
                    "propertyType": "msg",
                    "rules": [
                        {
                            "t": "neq",
                            "v": "",
                            "vt": "str"
                        }
                    ],
                    "checkall": "true",
                    "repair": false,
                    "outputs": 1,
                    "x": 850,
                    "y": 280,
                    "wires": [
                        [
                            "46871b01accaa1e7"
                        ]
                    ]
                }
            ]
            
            _ 1 Reply Last reply Reply Quote 0
            • mickym
              mickym Most Active last edited by mickym

              Hier meine Lösung:

              92583597-886b-49bb-8307-75a888ff2a2b-image.png

              [
                 {
                     "id": "6bc19d64a873f88d",
                     "type": "inject",
                     "z": "7e6af0015415146d",
                     "name": "",
                     "props": [
                         {
                             "p": "payload"
                         },
                         {
                             "p": "topic",
                             "vt": "str"
                         }
                     ],
                     "repeat": "",
                     "crontab": "",
                     "once": false,
                     "onceDelay": 0.1,
                     "topic": "Teststring",
                     "payload": "AAA     50     909   ",
                     "payloadType": "str",
                     "x": 280,
                     "y": 4140,
                     "wires": [
                         [
                             "cc9364eb8e5dd8f0"
                         ]
                     ]
                 },
                 {
                     "id": "aba432bff1dc6402",
                     "type": "debug",
                     "z": "7e6af0015415146d",
                     "name": "debug 21",
                     "active": true,
                     "tosidebar": true,
                     "console": false,
                     "tostatus": false,
                     "complete": "false",
                     "statusVal": "",
                     "statusType": "auto",
                     "x": 860,
                     "y": 4140,
                     "wires": []
                 },
                 {
                     "id": "cc9364eb8e5dd8f0",
                     "type": "change",
                     "z": "7e6af0015415146d",
                     "name": "",
                     "rules": [
                         {
                             "t": "set",
                             "p": "payload",
                             "pt": "msg",
                             "to": "$split($trim(payload), \" \")\t\t",
                             "tot": "jsonata"
                         }
                     ],
                     "action": "",
                     "property": "",
                     "from": "",
                     "to": "",
                     "reg": false,
                     "x": 530,
                     "y": 4140,
                     "wires": [
                         [
                             "d477d73f796189d7"
                         ]
                     ]
                 },
                 {
                     "id": "d477d73f796189d7",
                     "type": "split",
                     "z": "7e6af0015415146d",
                     "name": "",
                     "splt": "\\n",
                     "spltType": "str",
                     "arraySplt": 1,
                     "arraySpltType": "len",
                     "stream": false,
                     "addname": "",
                     "x": 710,
                     "y": 4140,
                     "wires": [
                         [
                             "aba432bff1dc6402"
                         ]
                     ]
                 }
              ]
              

              Die Längen des Strings oder die Anzahl Leerzeichen ist egal.

              _ 1 Reply Last reply Reply Quote 0
              • _
                _R_A_L_F_ @Marc Berg last edited by

                @marc-berg Danke dir für den Lösungsansatz 😉

                1 Reply Last reply Reply Quote 0
                • _
                  _R_A_L_F_ @mickym last edited by

                  @mickym Danke dir schon mal. Gibt es hier auch die Möglichkeit den einzelnen Werten einen separaten Topic zu vergeben?

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

                    @_r_a_l_f_ sagte in String Werte von Payload Gesamtwert extrahieren:

                    @mickym Danke dir schon mal. Gibt es hier auch die Möglichkeit den einzelnen Werten einen separaten Topic zu vergeben?

                    Klar:

                    96ca3979-fec8-4a4f-b52a-666ee2511e46-image.png

                    [
                       {
                           "id": "6bc19d64a873f88d",
                           "type": "inject",
                           "z": "7e6af0015415146d",
                           "name": "",
                           "props": [
                               {
                                   "p": "payload"
                               },
                               {
                                   "p": "topic",
                                   "vt": "str"
                               }
                           ],
                           "repeat": "",
                           "crontab": "",
                           "once": false,
                           "onceDelay": 0.1,
                           "topic": "Teststring",
                           "payload": "AAA     50                          909   ",
                           "payloadType": "str",
                           "x": 230,
                           "y": 4140,
                           "wires": [
                               [
                                   "cc9364eb8e5dd8f0"
                               ]
                           ]
                       },
                       {
                           "id": "aba432bff1dc6402",
                           "type": "debug",
                           "z": "7e6af0015415146d",
                           "name": "debug 21",
                           "active": true,
                           "tosidebar": true,
                           "console": false,
                           "tostatus": false,
                           "complete": "false",
                           "statusVal": "",
                           "statusType": "auto",
                           "x": 860,
                           "y": 4140,
                           "wires": []
                       },
                       {
                           "id": "cc9364eb8e5dd8f0",
                           "type": "change",
                           "z": "7e6af0015415146d",
                           "name": "",
                           "rules": [
                               {
                                   "t": "set",
                                   "p": "payload",
                                   "pt": "msg",
                                   "to": "$split($trim(payload), \" \")\t\t",
                                   "tot": "jsonata"
                               },
                               {
                                   "t": "set",
                                   "p": "payload",
                                   "pt": "msg",
                                   "to": "{\"Wert1\" : payload[0],\t\"Wert2\" : payload[1],\t\"Wert3\" : payload[2]}",
                                   "tot": "jsonata"
                               }
                           ],
                           "action": "",
                           "property": "",
                           "from": "",
                           "to": "",
                           "reg": false,
                           "x": 410,
                           "y": 4140,
                           "wires": [
                               [
                                   "d477d73f796189d7",
                                   "2145a3f7a983bbaf"
                               ]
                           ]
                       },
                       {
                           "id": "d477d73f796189d7",
                           "type": "split",
                           "z": "7e6af0015415146d",
                           "name": "",
                           "splt": "\\n",
                           "spltType": "str",
                           "arraySplt": 1,
                           "arraySpltType": "len",
                           "stream": false,
                           "addname": "topic",
                           "x": 650,
                           "y": 4140,
                           "wires": [
                               [
                                   "aba432bff1dc6402"
                               ]
                           ]
                       },
                       {
                           "id": "2145a3f7a983bbaf",
                           "type": "debug",
                           "z": "7e6af0015415146d",
                           "name": "Array",
                           "active": true,
                           "tosidebar": true,
                           "console": false,
                           "tostatus": false,
                           "complete": "payload",
                           "targetType": "msg",
                           "statusVal": "",
                           "statusType": "auto",
                           "x": 610,
                           "y": 4100,
                           "wires": []
                       }
                    ]
                    

                    _ mickym 2 Replies Last reply Reply Quote 0
                    • _
                      _R_A_L_F_ @mickym last edited by

                      @mickym Cool, das schaut doch prima aus. Danke euch @Marc-Berg beiden 😉

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

                        @mickym Falls keine Leerzeichen drin sind urn die Werte zu trennen, und die Positionen so fix sind, dann kannst natürlich auch die payload entsprechend splitten:

                        {"Wert1" : $trim($substring(payload, 0, 8)),
                        "Wert2" : $trim($substring(payload, 8, 8)),
                        "Wert3" : $trim($substring(payload, 16, 8))}
                        
                        
                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post

                        Support us

                        ioBroker
                        Community Adapters
                        Donate

                        541
                        Online

                        31.7k
                        Users

                        79.7k
                        Topics

                        1.3m
                        Posts

                        3
                        10
                        346
                        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