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. MQTT js zerlegen mit switsch

    NEWS

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

    • ioBroker goes Matter ... Matter Adapter in Stable

    • Monatsrückblick - April 2025

    MQTT js zerlegen mit switsch

    This topic has been deleted. Only users with topic management privileges can see it.
    • Jörg_
      Jörg_ last edited by

      Guten abend. 😉

      ich habe hier mehrere Xioami Temp. sensoren die Messwerte bekomme ich per TASMOTA MQTT als Json

      {"Time":"2022-12-04T13:44:13","ATCf44b8f":{"mac":"a4c138f44b8f","Temperature":19.9,"Humidity":51,"DewPoint":9.5,"Btn":1,"Battery":68,"RSSI":-42},"ATC589a51":{"mac":"a4c138589a51","Temperature":19.6,"Humidity":51.8,"DewPoint":9.4,"Btn":1,"Battery":70,"RSSI":-60},"TempUnit":"C"}
      

      jetzt wollte ich die in influxdb und grafana auswerten ,

      angefangen habe ich mit blocky script bin dann aber bei node red hängen geblieben

      [
          {
              "id": "7510054243d77b6d",
              "type": "switch",
              "z": "6f4022ddbb229204",
              "name": "",
              "property": "payload",
              "propertyType": "msg",
              "rules": [
                  {
                      "t": "eq",
                      "v": "payload.ATCf44b8f.mac.\"a4c138f44b8f\"",
                      "vt": "str"
                  },
                  {
                      "t": "eq",
                      "v": "a4c138589a51",
                      "vt": "str"
                  },
                  {
                      "t": "eq",
                      "v": "A4:C1:38:E3:C7:94",
                      "vt": "str"
                  },
                  {
                      "t": "eq",
                      "v": "A4:C1:38:CB:5C:EA ",
                      "vt": "str"
                  },
                  {
                      "t": "eq",
                      "v": "A4:C1:38:A6:00:5B",
                      "vt": "str"
                  }
              ],
              "checkall": "true",
              "repair": false,
              "outputs": 5,
              "x": 610,
              "y": 500,
              "wires": [
                  [
                      "ed08fe87332abba7",
                      "6e7fcdeaa794a00a"
                  ],
                  [],
                  [],
                  [],
                  []
              ]
          },
          {
              "id": "ed08fe87332abba7",
              "type": "function",
              "z": "6f4022ddbb229204",
              "name": "temp",
              "func": "msg.topic = \"Haus/Wohnzimmer/Temp.\"\nmsg.payload = msg.payload[\".ATCf44b8f.Temperature\"]\nreturn msg;\n\n\n",
              "outputs": 1,
              "noerr": 0,
              "initialize": "",
              "finalize": "",
              "libs": [],
              "x": 830,
              "y": 460,
              "wires": [
                  [
                      "13e50b962ea4b4ec",
                      "ced978bbcfb66860",
                      "f2ebf39d6ec29ac0"
                  ]
              ]
          },
          {
              "id": "13e50b962ea4b4ec",
              "type": "debug",
              "z": "6f4022ddbb229204",
              "name": "debug 18",
              "active": true,
              "tosidebar": true,
              "console": false,
              "tostatus": false,
              "complete": "false",
              "statusVal": "",
              "statusType": "auto",
              "x": 1060,
              "y": 460,
              "wires": []
          },
          {
              "id": "47e53c63b917864e",
              "type": "inject",
              "z": "6f4022ddbb229204",
              "name": "",
              "props": [
                  {
                      "p": "payload"
                  }
              ],
              "repeat": "",
              "crontab": "",
              "once": false,
              "onceDelay": 0.1,
              "topic": "",
              "payload": "{\"Time\":\"2022-12-04T13:44:13\",\"ATCf44b8f\":{\"mac\":\"a4c138f44b8f\",\"Temperature\":19.9,\"Humidity\":51,\"DewPoint\":9.5,\"Btn\":1,\"Battery\":68,\"RSSI\":-42},\"ATC589a51\":{\"mac\":\"a4c138589a51\",\"Temperature\":19.6,\"Humidity\":51.8,\"DewPoint\":9.4,\"Btn\":1,\"Battery\":70,\"RSSI\":-60},\"TempUnit\":\"C\"}",
              "payloadType": "str",
              "x": 170,
              "y": 520,
              "wires": [
                  [
                      "a5a4c266b13a0cca"
                  ]
              ]
          },
          {
              "id": "a43792495de3a27b",
              "type": "debug",
              "z": "6f4022ddbb229204",
              "name": "debug 19",
              "active": true,
              "tosidebar": true,
              "console": false,
              "tostatus": false,
              "complete": "false",
              "statusVal": "",
              "statusType": "auto",
              "x": 380,
              "y": 400,
              "wires": []
          },
          {
              "id": "a5a4c266b13a0cca",
              "type": "json",
              "z": "6f4022ddbb229204",
              "name": "",
              "property": "payload",
              "action": "",
              "pretty": true,
              "x": 390,
              "y": 520,
              "wires": [
                  [
                      "7510054243d77b6d",
                      "a43792495de3a27b"
                  ]
              ]
          },
          {
              "id": "6e7fcdeaa794a00a",
              "type": "debug",
              "z": "6f4022ddbb229204",
              "name": "debug 20",
              "active": true,
              "tosidebar": true,
              "console": false,
              "tostatus": false,
              "complete": "false",
              "statusVal": "",
              "statusType": "auto",
              "x": 800,
              "y": 380,
              "wires": []
          },
          {
              "id": "ced978bbcfb66860",
              "type": "influxdb out",
              "z": "6f4022ddbb229204",
              "influxdb": "eac63cf27267f4a5",
              "name": "",
              "measurement": "test_xioami",
              "precision": "",
              "retentionPolicy": "",
              "database": "database",
              "precisionV18FluxV20": "ms",
              "retentionPolicyV18Flux": "",
              "org": "Weiss",
              "bucket": "iobroker",
              "x": 1240,
              "y": 520,
              "wires": []
          },
          {
              "id": "f2ebf39d6ec29ac0",
              "type": "mqtt out",
              "z": "6f4022ddbb229204",
              "name": "",
              "topic": "Haus/",
              "qos": "2",
              "retain": "",
              "respTopic": "",
              "contentType": "",
              "userProps": "",
              "correl": "",
              "expiry": "",
              "broker": "33eaf0503bac882f",
              "x": 1050,
              "y": 560,
              "wires": []
          },
          {
              "id": "eac63cf27267f4a5",
              "type": "influxdb",
              "hostname": "127.0.0.1",
              "port": "8086",
              "protocol": "http",
              "database": "database",
              "name": "",
              "usetls": false,
              "tls": "",
              "influxdbVersion": "2.0",
              "url": "https://eu-central-1-1.aws.cloud2.influxdata.com/",
              "rejectUnauthorized": false
          },
          {
              "id": "33eaf0503bac882f",
              "type": "mqtt-broker",
              "name": "mqtt-server",
              "broker": "192.168.178.20",
              "port": "1883",
              "clientid": "node_red",
              "autoConnect": true,
              "usetls": false,
              "protocolVersion": "4",
              "keepalive": "60",
              "cleansession": true,
              "birthTopic": "nodered ",
              "birthQos": "0",
              "birthPayload": "nodered online",
              "birthMsg": {},
              "closeTopic": "",
              "closeQos": "0",
              "closePayload": "",
              "closeMsg": {},
              "willTopic": "nodered",
              "willQos": "0",
              "willPayload": "nodered offlline",
              "willMsg": {},
              "userProps": "",
              "sessionExpiry": ""
          }
      ]
      

      auch da gehst nicht weiter ich hänge bei der switsch node fest die da Js nach mac adressen filtern sollte da die sensoren in 3 gleichen String senden alle nach einnader

      Für Ideen wäre ich dankbar 😉

      Chaot mickym 2 Replies Last reply Reply Quote 0
      • Chaot
        Chaot @Jörg_ last edited by Chaot

        @jörg-w Ich verwende den Sonoff Adapter. Da ist das alles schon zerlegt.sensor.jpg

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

          @jörg-w Ist zwar ein komischer EingabeString. Aber Du musst den String in einzelne Objekte zerlegen:

          b730a03e-8fb7-4c4f-b6ff-6979fd8912f6-image.png

          [
             {
                 "id": "13e50b962ea4b4ec",
                 "type": "debug",
                 "z": "180b7e98628a2a9b",
                 "name": "Temperatur",
                 "active": true,
                 "tosidebar": true,
                 "console": false,
                 "tostatus": false,
                 "complete": "payload",
                 "targetType": "msg",
                 "statusVal": "",
                 "statusType": "auto",
                 "x": 1290,
                 "y": 340,
                 "wires": []
             },
             {
                 "id": "47e53c63b917864e",
                 "type": "inject",
                 "z": "180b7e98628a2a9b",
                 "name": "",
                 "props": [
                     {
                         "p": "payload"
                     }
                 ],
                 "repeat": "",
                 "crontab": "",
                 "once": false,
                 "onceDelay": 0.1,
                 "topic": "",
                 "payload": "{\"Time\":\"2022-12-04T13:44:13\",\"ATCf44b8f\":{\"mac\":\"a4c138f44b8f\",\"Temperature\":19.9,\"Humidity\":51,\"DewPoint\":9.5,\"Btn\":1,\"Battery\":68,\"RSSI\":-42},\"ATC589a51\":{\"mac\":\"a4c138589a51\",\"Temperature\":19.6,\"Humidity\":51.8,\"DewPoint\":9.4,\"Btn\":1,\"Battery\":70,\"RSSI\":-60},\"TempUnit\":\"C\"}",
                 "payloadType": "str",
                 "x": 170,
                 "y": 380,
                 "wires": [
                     [
                         "a5a4c266b13a0cca"
                     ]
                 ]
             },
             {
                 "id": "a43792495de3a27b",
                 "type": "debug",
                 "z": "180b7e98628a2a9b",
                 "name": "Eingabe",
                 "active": false,
                 "tosidebar": true,
                 "console": false,
                 "tostatus": false,
                 "complete": "payload",
                 "targetType": "msg",
                 "statusVal": "",
                 "statusType": "auto",
                 "x": 380,
                 "y": 260,
                 "wires": []
             },
             {
                 "id": "a5a4c266b13a0cca",
                 "type": "json",
                 "z": "180b7e98628a2a9b",
                 "name": "",
                 "property": "payload",
                 "action": "",
                 "pretty": true,
                 "x": 310,
                 "y": 380,
                 "wires": [
                     [
                         "a43792495de3a27b",
                         "afd48379794b7589"
                     ]
                 ]
             },
             {
                 "id": "afd48379794b7589",
                 "type": "split",
                 "z": "180b7e98628a2a9b",
                 "name": "",
                 "splt": "\\n",
                 "spltType": "str",
                 "arraySplt": 1,
                 "arraySpltType": "len",
                 "stream": false,
                 "addname": "topic",
                 "x": 450,
                 "y": 380,
                 "wires": [
                     [
                         "e4298053724078c1"
                     ]
                 ]
             },
             {
                 "id": "22cdf6ba3b189c5a",
                 "type": "switch",
                 "z": "180b7e98628a2a9b",
                 "name": "MAC Selektor",
                 "property": "payload.mac",
                 "propertyType": "msg",
                 "rules": [
                     {
                         "t": "eq",
                         "v": "a4c138f44b8f",
                         "vt": "str"
                     },
                     {
                         "t": "eq",
                         "v": "a4c138589a51",
                         "vt": "str"
                     }
                 ],
                 "checkall": "true",
                 "repair": false,
                 "outputs": 2,
                 "x": 900,
                 "y": 380,
                 "wires": [
                     [
                         "d71445fc881d262d"
                     ],
                     [
                         "483b49ba0a0bc7bd"
                     ]
                 ]
             },
             {
                 "id": "d71445fc881d262d",
                 "type": "change",
                 "z": "180b7e98628a2a9b",
                 "name": "Temperatur",
                 "rules": [
                     {
                         "t": "set",
                         "p": "payload",
                         "pt": "msg",
                         "to": "payload.Temperature",
                         "tot": "msg"
                     }
                 ],
                 "action": "",
                 "property": "",
                 "from": "",
                 "to": "",
                 "reg": false,
                 "x": 1090,
                 "y": 340,
                 "wires": [
                     [
                         "13e50b962ea4b4ec"
                     ]
                 ]
             },
             {
                 "id": "483b49ba0a0bc7bd",
                 "type": "change",
                 "z": "180b7e98628a2a9b",
                 "name": "Temperatur",
                 "rules": [
                     {
                         "t": "set",
                         "p": "payload",
                         "pt": "msg",
                         "to": "payload.Temperature",
                         "tot": "msg"
                     }
                 ],
                 "action": "",
                 "property": "",
                 "from": "",
                 "to": "",
                 "reg": false,
                 "x": 1090,
                 "y": 420,
                 "wires": [
                     [
                         "30fabc8007870f62"
                     ]
                 ]
             },
             {
                 "id": "30fabc8007870f62",
                 "type": "debug",
                 "z": "180b7e98628a2a9b",
                 "name": "Temperatur",
                 "active": true,
                 "tosidebar": true,
                 "console": false,
                 "tostatus": false,
                 "complete": "payload",
                 "targetType": "msg",
                 "statusVal": "",
                 "statusType": "auto",
                 "x": 1290,
                 "y": 420,
                 "wires": []
             },
             {
                 "id": "e4298053724078c1",
                 "type": "switch",
                 "z": "180b7e98628a2a9b",
                 "name": "Objekt enthält MAC Adresse",
                 "property": "payload.mac",
                 "propertyType": "msg",
                 "rules": [
                     {
                         "t": "nnull"
                     }
                 ],
                 "checkall": "true",
                 "repair": false,
                 "outputs": 1,
                 "x": 660,
                 "y": 380,
                 "wires": [
                     [
                         "22cdf6ba3b189c5a"
                     ]
                 ]
             }
          ]
          

          Wenn Du einzelne Datenpunkte aus Deinen JSONs haben willst, dann schau Dir mal meinen Flow an:

          https://forum.iobroker.net/topic/43856/json-oder-javascript-objekt-in-iobroker-datenpunkte-zerlegen

          Andere Adapter braucht es dafür nicht. Im Gegenteil, wenn Du auch noch andere MQTT-Kommunikation nutzen willst, stört der nur.

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

            Hier noch eine einfachere Lösung mit JSONATA - (da muss ich aber auch immer erst probieren).
            Damit spart man sich den switch und selectiert nur die Objekte, die eine mac Adresse haben:

            5b9aa87f-f317-4b9e-a88e-113a310ff721-image.png

            [
                {
                    "id": "c19076460e052cbe",
                    "type": "inject",
                    "z": "289f539dcc33814e",
                    "name": "",
                    "props": [
                        {
                            "p": "payload"
                        }
                    ],
                    "repeat": "",
                    "crontab": "",
                    "once": false,
                    "onceDelay": 0.1,
                    "topic": "",
                    "payload": "{\"Time\":\"2022-12-04T13:44:13\",\"ATCf44b8f\":{\"mac\":\"a4c138f44b8f\",\"Temperature\":19.9,\"Humidity\":51,\"DewPoint\":9.5,\"Btn\":1,\"Battery\":68,\"RSSI\":-42},\"ATC589a51\":{\"mac\":\"a4c138589a51\",\"Temperature\":19.6,\"Humidity\":51.8,\"DewPoint\":9.4,\"Btn\":1,\"Battery\":70,\"RSSI\":-60},\"TempUnit\":\"C\"}",
                    "payloadType": "json",
                    "x": 190,
                    "y": 180,
                    "wires": [
                        [
                            "9ee4e3eabd877e79",
                            "a739f00f949d5b6a"
                        ]
                    ]
                },
                {
                    "id": "9ee4e3eabd877e79",
                    "type": "change",
                    "z": "289f539dcc33814e",
                    "name": "",
                    "rules": [
                        {
                            "t": "set",
                            "p": "payload",
                            "pt": "msg",
                            "to": "payload.*[mac]",
                            "tot": "jsonata"
                        }
                    ],
                    "action": "",
                    "property": "",
                    "from": "",
                    "to": "",
                    "reg": false,
                    "x": 390,
                    "y": 180,
                    "wires": [
                        [
                            "9d293f1fc9c7c077",
                            "1117c4f5f591bdd7"
                        ]
                    ]
                },
                {
                    "id": "9d293f1fc9c7c077",
                    "type": "split",
                    "z": "289f539dcc33814e",
                    "name": "",
                    "splt": "\\n",
                    "spltType": "str",
                    "arraySplt": 1,
                    "arraySpltType": "len",
                    "stream": false,
                    "addname": "",
                    "x": 570,
                    "y": 180,
                    "wires": [
                        [
                            "4fa63fbcc9b634fe"
                        ]
                    ]
                },
                {
                    "id": "fbeaa56747a92be4",
                    "type": "debug",
                    "z": "289f539dcc33814e",
                    "name": "Temperatur",
                    "active": true,
                    "tosidebar": true,
                    "console": false,
                    "tostatus": false,
                    "complete": "payload",
                    "targetType": "msg",
                    "statusVal": "",
                    "statusType": "auto",
                    "x": 1130,
                    "y": 140,
                    "wires": []
                },
                {
                    "id": "4fa63fbcc9b634fe",
                    "type": "switch",
                    "z": "289f539dcc33814e",
                    "name": "MAC Selektor",
                    "property": "payload.mac",
                    "propertyType": "msg",
                    "rules": [
                        {
                            "t": "eq",
                            "v": "a4c138f44b8f",
                            "vt": "str"
                        },
                        {
                            "t": "eq",
                            "v": "a4c138589a51",
                            "vt": "str"
                        }
                    ],
                    "checkall": "true",
                    "repair": false,
                    "outputs": 2,
                    "x": 740,
                    "y": 180,
                    "wires": [
                        [
                            "eca4ca22ec99973c"
                        ],
                        [
                            "232981fe0da01a87"
                        ]
                    ]
                },
                {
                    "id": "eca4ca22ec99973c",
                    "type": "change",
                    "z": "289f539dcc33814e",
                    "name": "Temperatur",
                    "rules": [
                        {
                            "t": "set",
                            "p": "payload",
                            "pt": "msg",
                            "to": "payload.Temperature",
                            "tot": "msg"
                        }
                    ],
                    "action": "",
                    "property": "",
                    "from": "",
                    "to": "",
                    "reg": false,
                    "x": 930,
                    "y": 140,
                    "wires": [
                        [
                            "fbeaa56747a92be4"
                        ]
                    ]
                },
                {
                    "id": "232981fe0da01a87",
                    "type": "change",
                    "z": "289f539dcc33814e",
                    "name": "Temperatur",
                    "rules": [
                        {
                            "t": "set",
                            "p": "payload",
                            "pt": "msg",
                            "to": "payload.Temperature",
                            "tot": "msg"
                        }
                    ],
                    "action": "",
                    "property": "",
                    "from": "",
                    "to": "",
                    "reg": false,
                    "x": 930,
                    "y": 220,
                    "wires": [
                        [
                            "05ebbc61b6322189"
                        ]
                    ]
                },
                {
                    "id": "05ebbc61b6322189",
                    "type": "debug",
                    "z": "289f539dcc33814e",
                    "name": "Temperatur",
                    "active": true,
                    "tosidebar": true,
                    "console": false,
                    "tostatus": false,
                    "complete": "payload",
                    "targetType": "msg",
                    "statusVal": "",
                    "statusType": "auto",
                    "x": 1130,
                    "y": 220,
                    "wires": []
                },
                {
                    "id": "1117c4f5f591bdd7",
                    "type": "debug",
                    "z": "289f539dcc33814e",
                    "name": "Ergebnisarray",
                    "active": true,
                    "tosidebar": true,
                    "console": false,
                    "tostatus": false,
                    "complete": "payload",
                    "targetType": "msg",
                    "statusVal": "",
                    "statusType": "auto",
                    "x": 620,
                    "y": 240,
                    "wires": []
                },
                {
                    "id": "a739f00f949d5b6a",
                    "type": "debug",
                    "z": "289f539dcc33814e",
                    "name": "Input",
                    "active": true,
                    "tosidebar": true,
                    "console": false,
                    "tostatus": false,
                    "complete": "payload",
                    "targetType": "msg",
                    "statusVal": "",
                    "statusType": "auto",
                    "x": 370,
                    "y": 120,
                    "wires": []
                }
            ]
            

            Dein Objekt

            {"Time":"2022-12-04T13:44:13","ATCf44b8f":{"mac":"a4c138f44b8f","Temperature":19.9,"Humidity":51,"DewPoint":9.5,"Btn":1,"Battery":68,"RSSI":-42},"ATC589a51":{"mac":"a4c138589a51","Temperature":19.6,"Humidity":51.8,"DewPoint":9.4,"Btn":1,"Battery":70,"RSSI":-60},"TempUnit":"C"}
            

            wird mit folgendem JSONATA ein Array mit Objekten (die einen Textschlüssel mit Eigenschaft "mac" - geht nur bei Typ Text - bei Zahlen benötigt man numerische Vergleiche) reduziert.

            payload.*[mac]
            

            will man im Übrigen auf Eigenschaften mit Zahlen als Wert selektieren, benötigt man die $exists() Funktion

            *[$exists(Temperature)]
            

            selektiert alle Objekte, die eine Eigenschaft Temperature enthalten.

            das Ergebnisarray kann dann einfach mit einer split Node in einzelne Nachrichten aufgeteilt werden:

            [{"mac":"a4c138f44b8f","Temperature":19.9,"Humidity":51,"DewPoint":9.5,"Btn":1,"Battery":68,"RSSI":-42},{"mac":"a4c138589a51","Temperature":19.6,"Humidity":51.8,"DewPoint":9.4,"Btn":1,"Battery":70,"RSSI":-60}]
            
            Jörg_ 1 Reply Last reply Reply Quote 1
            • Jörg_
              Jörg_ @mickym last edited by Jörg_

              @mickym

              ging ja schneller als gedacht 🙂

              peferkt besten Dank läuft jetzt wie gewüscht 😉 !!!

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

              Support us

              ioBroker
              Community Adapters
              Donate

              1.1k
              Online

              31.7k
              Users

              79.7k
              Topics

              1.3m
              Posts

              3
              5
              296
              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