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. JSON oder JavaScript Objekt in iobroker Datenpunkte zerlegen

    NEWS

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

    • ioBroker goes Matter ... Matter Adapter in Stable

    • Monatsrückblick - April 2025

    JSON oder JavaScript Objekt in iobroker Datenpunkte zerlegen

    This topic has been deleted. Only users with topic management privileges can see it.
    • mickym
      mickym Most Active @Bernd1967 last edited by mickym

      @bernd1967 Mal unabhängig davon, dass man so eine Struktur im iobroker auch einfach exportieren kann, aber was stimmt denn mit dem Ergebnis nicht? - Das schaut doch richtig aus?

      Das was Du vielleicht als Problem ansiehst - ist ggf. gar keines. Falls Du erwartet hast, dass Du unter devices eine hierarchische Struktur bekommst - kann nicht funktionieren, da das kein Objekt ist. Das Problem ist eher die list Node - die gibt auch wenn Du ANY als Filter hast nur die states aus. Da müsste man halt wieder ein Issue aufmachen.

      Für das Wiederherstellen der Struktur spielt das aber keine Rolle - diese wird, wenn Du meinen Subflow nimmst wieder 1:1 richtig hergestellt:

      45868363-176a-4f42-bc07-55e405f9f246-image.png

      Du siehst ja, dass Du aus der List node nur 2 Objekte rausbekommst:

      c8664fab-7997-4c08-8de8-b2a77251a259-image.png

      Auch wenn ich das im iobroker direkt exportiere bekommst Du nur 2 Objekte zurück:

      {
       "0_userdata.0.objRoot.devices.554-6546532-36565-654654.Test": {
         "common": {
           "read": true,
           "write": true,
           "desc": "Created by Node-Red",
           "role": "state",
           "name": "devices.554-6546532-36565-654654.Test",
           "type": "string"
         },
         "native": {},
         "type": "state",
         "from": "system.adapter.node-red.0",
         "user": "system.user.admin",
         "ts": 1646489307670,
         "_id": "0_userdata.0.objRoot.devices.554-6546532-36565-654654.Test",
         "acl": {
           "object": 1636,
           "state": 1636,
           "owner": "system.user.admin",
           "ownerGroup": "system.group.administrator"
         }
       },
       "0_userdata.0.objRoot.version": {
         "common": {
           "read": true,
           "write": true,
           "desc": "Created by Node-Red",
           "role": "state",
           "name": "version",
           "type": "number"
         },
         "native": {},
         "type": "state",
         "from": "system.adapter.node-red.0",
         "user": "system.user.admin",
         "ts": 1646489307667,
         "_id": "0_userdata.0.objRoot.version",
         "acl": {
           "object": 1636,
           "state": 1636,
           "owner": "system.user.admin",
           "ownerGroup": "system.group.administrator"
         }
       }
      }
      

      Falls Du eine Objekthierarchie in Deinem JSON wiederherstellen möchtest, dann ist da nicht banal und muss man quasi selbst programmieren.

      Mit einer Flow Variablen und einer function Node ist das dann sicher am Einfachsten. Hier mal eine Möglichkeit:

      0820c6f6-f5cc-435b-acbb-de46b5d1509a-image.png

      [
         {
             "id": "fe734f5c8eab6919",
             "type": "ioBroker list",
             "z": "54b226bc.0793e8",
             "name": "",
             "topic": "0_userdata.0.objRoot.*",
             "objType": "",
             "regex": "",
             "asArray": "false",
             "onlyIDs": "true",
             "withValues": "true",
             "x": 340,
             "y": 2620,
             "wires": [
                 [
                     "6a6e0bcf573c21d2"
                 ]
             ]
         },
         {
             "id": "81d74c3a4c8d8c69",
             "type": "inject",
             "z": "54b226bc.0793e8",
             "name": "Trigger",
             "props": [
                 {
                     "p": "payload"
                 }
             ],
             "repeat": "",
             "crontab": "",
             "once": false,
             "onceDelay": 0.1,
             "topic": "",
             "payload": "true",
             "payloadType": "bool",
             "x": 150,
             "y": 2620,
             "wires": [
                 [
                     "fe734f5c8eab6919"
                 ]
             ]
         },
         {
             "id": "6a6e0bcf573c21d2",
             "type": "change",
             "z": "54b226bc.0793e8",
             "name": "",
             "rules": [
                 {
                     "t": "set",
                     "p": "payload",
                     "pt": "msg",
                     "to": "payload.val",
                     "tot": "msg"
                 },
                 {
                     "t": "change",
                     "p": "topic",
                     "pt": "msg",
                     "from": ".*objRoot\\.(.*)$",
                     "fromt": "re",
                     "to": "$1",
                     "tot": "str"
                 }
             ],
             "action": "",
             "property": "",
             "from": "",
             "to": "",
             "reg": false,
             "x": 570,
             "y": 2620,
             "wires": [
                 [
                     "c54cee7f0f242594"
                 ]
             ]
         },
         {
             "id": "c54cee7f0f242594",
             "type": "function",
             "z": "54b226bc.0793e8",
             "name": "",
             "func": "flow.set (\"objRoot.\" + msg.topic, msg.payload);\nreturn msg;",
             "outputs": 1,
             "noerr": 0,
             "initialize": "",
             "finalize": "",
             "libs": [],
             "x": 760,
             "y": 2620,
             "wires": [
                 [
                     "1d53b3a03cb1e658"
                 ]
             ]
         },
         {
             "id": "1d53b3a03cb1e658",
             "type": "change",
             "z": "54b226bc.0793e8",
             "name": "",
             "rules": [
                 {
                     "t": "set",
                     "p": "payload",
                     "pt": "msg",
                     "to": "objRoot",
                     "tot": "flow"
                 }
             ],
             "action": "",
             "property": "",
             "from": "",
             "to": "",
             "reg": false,
             "x": 950,
             "y": 2620,
             "wires": [
                 [
                     "22c29b67c34b4b4f"
                 ]
             ]
         },
         {
             "id": "eef15bb0d92eb826",
             "type": "debug",
             "z": "54b226bc.0793e8",
             "name": "",
             "active": true,
             "tosidebar": true,
             "console": false,
             "tostatus": false,
             "complete": "false",
             "statusVal": "",
             "statusType": "auto",
             "x": 1490,
             "y": 2620,
             "wires": []
         },
         {
             "id": "22c29b67c34b4b4f",
             "type": "trigger",
             "z": "54b226bc.0793e8",
             "name": "",
             "op1": "",
             "op2": "",
             "op1type": "nul",
             "op2type": "payl",
             "duration": "250",
             "extend": true,
             "overrideDelay": false,
             "units": "ms",
             "reset": "",
             "bytopic": "all",
             "topic": "topic",
             "outputs": 1,
             "x": 1160,
             "y": 2620,
             "wires": [
                 [
                     "90872318bacb6745"
                 ]
             ]
         },
         {
             "id": "90872318bacb6745",
             "type": "json",
             "z": "54b226bc.0793e8",
             "name": "",
             "property": "payload",
             "action": "",
             "pretty": false,
             "x": 1330,
             "y": 2620,
             "wires": [
                 [
                     "eef15bb0d92eb826"
                 ]
             ]
         }
      ]
      

      damit bekommst dann dieses Ergebnis:

      {"version":3,"devices":{"554-6546532-36565-654654":{"Test":""}}}
      
      B 1 Reply Last reply Reply Quote 0
      • B
        Bernd1967 @mickym last edited by Bernd1967

        @mickym sagte in JSON String oder Java Object in IOBroker Struktur:

        hierarchische Struktur

        Ja genau 🙂 , das hat mich irritiert, die hierarchische Struktur......
        Mit deinem Beispiel klappte es
        Danke

        Nachtrag: bei komplexen Strukturen entstehen allerding viele Fehler 😞
        Ist doch wohl nicht so einfach.

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

          @bernd1967 Ja - entweder muss man das halt wirklich auflösen oder Du schickst mal ein Beispiel wo es Probleme gibt, dass man es nachvollziehen kann.

          Das Problem ist, dass nach der Aufsplittung halt bestimmte Originalinformationen für immer verloren sind, es werden ja Arrays und Objekte in gleicher Weise behandelt.

          Die Frage ist halt wirklich braucht man diese Hierarchie oder kann man nicht mit dem Original leben - wie Du es am Anfang gemacht hast. Ich habe allerdings eine recht komplexe Struktur durchlaufen lassen und das ging fehlerfrei.

          Also müsste man das wohl anhand Deines Beispiels, das nicht läuft nachvollziehen.
          Ich hab den Flow nochmal angepasst - sodass die function Node zum Aufbau der Objektstruktur ausreicht und diese nach jeder Ausgabe gelöscht wird - damit es da kein Durcheinander gibt.

          652774ca-03c7-4f1c-9d00-6eddad862acd-image.png

          [
             {
                 "id": "d11089310692b73f",
                 "type": "change",
                 "z": "54b226bc.0793e8",
                 "name": "",
                 "rules": [
                     {
                         "t": "set",
                         "p": "payload",
                         "pt": "msg",
                         "to": "payload.val",
                         "tot": "msg"
                     },
                     {
                         "t": "change",
                         "p": "topic",
                         "pt": "msg",
                         "from": ".*objRoot\\.(.*)$",
                         "fromt": "re",
                         "to": "$1",
                         "tot": "str"
                     }
                 ],
                 "action": "",
                 "property": "",
                 "from": "",
                 "to": "",
                 "reg": false,
                 "x": 570,
                 "y": 1980,
                 "wires": [
                     [
                         "e4a1e5e5e9dc34ee"
                     ]
                 ]
             },
             {
                 "id": "e4a1e5e5e9dc34ee",
                 "type": "function",
                 "z": "54b226bc.0793e8",
                 "name": "",
                 "func": "if (msg.reset) {\n    context.set(\"objRoot\",undefined);\n    return null;\n}\ncontext.set (\"objRoot.\" + msg.topic, msg.payload);\nmsg.payload=context.get(\"objRoot\");\nreturn msg;",
                 "outputs": 1,
                 "noerr": 0,
                 "initialize": "",
                 "finalize": "",
                 "libs": [],
                 "x": 760,
                 "y": 1980,
                 "wires": [
                     [
                         "5200812ceca93736"
                     ]
                 ]
             },
             {
                 "id": "a9d3e44f37652c2e",
                 "type": "debug",
                 "z": "54b226bc.0793e8",
                 "name": "",
                 "active": false,
                 "tosidebar": true,
                 "console": false,
                 "tostatus": false,
                 "complete": "false",
                 "statusVal": "",
                 "statusType": "auto",
                 "x": 1270,
                 "y": 2040,
                 "wires": []
             },
             {
                 "id": "5200812ceca93736",
                 "type": "trigger",
                 "z": "54b226bc.0793e8",
                 "name": "",
                 "op1": "",
                 "op2": "",
                 "op1type": "nul",
                 "op2type": "payl",
                 "duration": "250",
                 "extend": true,
                 "overrideDelay": false,
                 "units": "ms",
                 "reset": "",
                 "bytopic": "all",
                 "topic": "topic",
                 "outputs": 1,
                 "x": 940,
                 "y": 1980,
                 "wires": [
                     [
                         "82c103eb2cf2057f",
                         "4aa8543b3675c7d1",
                         "0f757a3fa3b26e8f"
                     ]
                 ]
             },
             {
                 "id": "82c103eb2cf2057f",
                 "type": "json",
                 "z": "54b226bc.0793e8",
                 "name": "",
                 "property": "payload",
                 "action": "str",
                 "pretty": false,
                 "x": 1130,
                 "y": 1980,
                 "wires": [
                     [
                         "a9d3e44f37652c2e",
                         "45d69ff41aed483a"
                     ]
                 ]
             },
             {
                 "id": "0d87b95094bc6985",
                 "type": "ioBroker list",
                 "z": "54b226bc.0793e8",
                 "name": "",
                 "topic": "0_userdata.0.objRoot.*",
                 "objType": "",
                 "regex": "",
                 "asArray": "false",
                 "onlyIDs": "true",
                 "withValues": "true",
                 "x": 340,
                 "y": 1980,
                 "wires": [
                     [
                         "d11089310692b73f"
                     ]
                 ]
             },
             {
                 "id": "272ea867090d9156",
                 "type": "inject",
                 "z": "54b226bc.0793e8",
                 "name": "Trigger",
                 "props": [
                     {
                         "p": "payload"
                     }
                 ],
                 "repeat": "",
                 "crontab": "",
                 "once": false,
                 "onceDelay": 0.1,
                 "topic": "",
                 "payload": "true",
                 "payloadType": "bool",
                 "x": 150,
                 "y": 1980,
                 "wires": [
                     [
                         "0d87b95094bc6985"
                     ]
                 ]
             },
             {
                 "id": "4aa8543b3675c7d1",
                 "type": "debug",
                 "z": "54b226bc.0793e8",
                 "name": "",
                 "active": true,
                 "tosidebar": true,
                 "console": false,
                 "tostatus": false,
                 "complete": "false",
                 "statusVal": "",
                 "statusType": "auto",
                 "x": 1170,
                 "y": 1920,
                 "wires": []
             },
             {
                 "id": "45d69ff41aed483a",
                 "type": "file",
                 "z": "54b226bc.0793e8",
                 "name": "",
                 "filename": "/home/iobroker/output.json",
                 "appendNewline": true,
                 "createDir": false,
                 "overwriteFile": "true",
                 "encoding": "none",
                 "x": 1340,
                 "y": 1980,
                 "wires": [
                     []
                 ]
             },
             {
                 "id": "0f757a3fa3b26e8f",
                 "type": "change",
                 "z": "54b226bc.0793e8",
                 "name": "",
                 "rules": [
                     {
                         "t": "set",
                         "p": "reset",
                         "pt": "msg",
                         "to": "true",
                         "tot": "bool"
                     }
                 ],
                 "action": "",
                 "property": "",
                 "from": "",
                 "to": "",
                 "reg": false,
                 "x": 780,
                 "y": 2060,
                 "wires": [
                     [
                         "e4a1e5e5e9dc34ee"
                     ]
                 ]
             },
             {
                 "id": "4420acc0840bbd1a",
                 "type": "inject",
                 "z": "54b226bc.0793e8",
                 "name": "",
                 "props": [
                     {
                         "p": "payload"
                     },
                     {
                         "p": "topic",
                         "vt": "str"
                     }
                 ],
                 "repeat": "",
                 "crontab": "",
                 "once": false,
                 "onceDelay": 0.1,
                 "topic": "reset",
                 "payload": "true",
                 "payloadType": "bool",
                 "x": 560,
                 "y": 2060,
                 "wires": [
                     [
                         "0f757a3fa3b26e8f"
                     ]
                 ]
             }
          ]
          

          B 1 Reply Last reply Reply Quote 0
          • B
            Bernd1967 @mickym last edited by

            @mickym
            Danke für deine Mühe, ich werde das später nochmal ausprobieren.

            Der Fehler der immer wieder auftaucht ist übrigens folgender:
            [error] [function:e4a1e5e5e9dc34ee] Error: Invalid property expression: unexpected { at position 67

            Ein Beispiel kann ich Dir jetzt nicht geben weil dort sensible Daten vorhanden sind.
            Die müßte ich erstmal rausfiltern und das ist aufwendig.
            Ich kopiere im Moment alles manuelle mit einem JSON Editor.

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

              @bernd1967 Warum speicherst Du den JSON String vor der Aufteilung eigentlich nicht gleich in eine Datei?

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

                @apollon77

                Um diesen Flow mal mit der Erstellung von Folder Objekten zu testen habe ich mal die Objekte aus folgendem Ast meines mqtt Adapters exportiert:

                475fecb3-b9c2-4208-994d-a273e8f016df-image.png

                Mit den Eigenschaften der Objekte ergibt es dann folgenden JSON String:

                {
                 "mqtt.1.zigbee2mqtt.temperatur.kueche": {
                   "common": {
                     "name": "zigbee2mqtt/temperatur/kueche",
                     "write": true,
                     "read": true,
                     "role": "variable",
                     "desc": "mqtt client variable",
                     "type": "string"
                   },
                   "native": {
                     "topic": "zigbee2mqtt/temperatur/kueche"
                   },
                   "type": "state",
                   "from": "system.adapter.mqtt.1",
                   "user": "system.user.admin",
                   "ts": 1645439171879,
                   "_id": "mqtt.1.zigbee2mqtt.temperatur.kueche",
                   "acl": {
                     "object": 1636,
                     "state": 1636,
                     "owner": "system.user.admin",
                     "ownerGroup": "system.group.administrator"
                   }
                 },
                 "mqtt.1.zigbee2mqtt.temperatur.kueche.availability": {
                   "common": {
                     "name": "zigbee2mqtt/temperatur/kueche/availability",
                     "write": true,
                     "read": true,
                     "role": "variable",
                     "desc": "mqtt client variable",
                     "type": "string"
                   },
                   "native": {
                     "topic": "zigbee2mqtt/temperatur/kueche/availability"
                   },
                   "type": "state",
                   "from": "system.adapter.mqtt.1",
                   "user": "system.user.admin",
                   "ts": 1645495334609,
                   "_id": "mqtt.1.zigbee2mqtt.temperatur.kueche.availability",
                   "acl": {
                     "object": 1636,
                     "state": 1636,
                     "owner": "system.user.admin",
                     "ownerGroup": "system.group.administrator"
                   }
                 },
                 "mqtt.1.zigbee2mqtt.temperatur.kueche.gefrierfach": {
                   "common": {
                     "name": "zigbee2mqtt/temperatur/kueche/gefrierfach",
                     "write": true,
                     "read": true,
                     "role": "variable",
                     "desc": "mqtt client variable",
                     "type": "string"
                   },
                   "native": {
                     "topic": "zigbee2mqtt/temperatur/kueche/gefrierfach"
                   },
                   "type": "state",
                   "from": "system.adapter.mqtt.1",
                   "user": "system.user.admin",
                   "ts": 1645447173980,
                   "_id": "mqtt.1.zigbee2mqtt.temperatur.kueche.gefrierfach",
                   "acl": {
                     "object": 1636,
                     "state": 1636,
                     "owner": "system.user.admin",
                     "ownerGroup": "system.group.administrator"
                   }
                 },
                 "mqtt.1.zigbee2mqtt.temperatur.kueche.gefrierfach.availability": {
                   "common": {
                     "name": "zigbee2mqtt/temperatur/kueche/gefrierfach/availability",
                     "write": true,
                     "read": true,
                     "role": "variable",
                     "desc": "mqtt client variable",
                     "type": "string"
                   },
                   "native": {
                     "topic": "zigbee2mqtt/temperatur/kueche/gefrierfach/availability"
                   },
                   "type": "state",
                   "from": "system.adapter.mqtt.1",
                   "user": "system.user.admin",
                   "ts": 1645495334777,
                   "_id": "mqtt.1.zigbee2mqtt.temperatur.kueche.gefrierfach.availability",
                   "acl": {
                     "object": 1636,
                     "state": 1636,
                     "owner": "system.user.admin",
                     "ownerGroup": "system.group.administrator"
                   }
                 },
                 "mqtt.1.zigbee2mqtt.temperatur.kueche.kuehlschrank": {
                   "common": {
                     "name": "zigbee2mqtt/temperatur/kueche/kuehlschrank",
                     "write": true,
                     "read": true,
                     "role": "variable",
                     "desc": "mqtt client variable",
                     "type": "string"
                   },
                   "native": {
                     "topic": "zigbee2mqtt/temperatur/kueche/kuehlschrank"
                   },
                   "type": "state",
                   "from": "system.adapter.mqtt.1",
                   "user": "system.user.admin",
                   "ts": 1645446952602,
                   "_id": "mqtt.1.zigbee2mqtt.temperatur.kueche.kuehlschrank",
                   "acl": {
                     "object": 1636,
                     "state": 1636,
                     "owner": "system.user.admin",
                     "ownerGroup": "system.group.administrator"
                   }
                 },
                 "mqtt.1.zigbee2mqtt.temperatur.kueche.kuehlschrank.availability": {
                   "common": {
                     "name": "zigbee2mqtt/temperatur/kueche/kuehlschrank/availability",
                     "write": true,
                     "read": true,
                     "role": "variable",
                     "desc": "mqtt client variable",
                     "type": "string"
                   },
                   "native": {
                     "topic": "zigbee2mqtt/temperatur/kueche/kuehlschrank/availability"
                   },
                   "type": "state",
                   "from": "system.adapter.mqtt.1",
                   "user": "system.user.admin",
                   "ts": 1645495334777,
                   "_id": "mqtt.1.zigbee2mqtt.temperatur.kueche.kuehlschrank.availability",
                   "acl": {
                     "object": 1636,
                     "state": 1636,
                     "owner": "system.user.admin",
                     "ownerGroup": "system.group.administrator"
                   }
                 }
                }
                

                Dieser wird nun als States die einzelnen Endpunkte aufdröseln und erzeugt die Topics die dann über die iobroker -out Node geschrieben werden:

                543741bd-c78d-4da8-84fb-bf2583096f0c-image.png

                Die Struktur ist dann:

                673d5a7f-bc18-4d68-a9a0-457575793b55-image.png

                Die fehlenden Objekte müsste also die iobroker-Node selbst erzeugen.

                134bf836-7ab4-4d8b-bbcd-9d6b50216873-image.png

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

                  So ich habe die Subflow Node etwas erweitert, indem man das topic bei Benutzung von Wildcards mit verwenden kann.

                  d87cceb6-0fa8-47d8-9561-737ce1afd1af-image.png

                  Default ist natürlich false, um das bisherige Verhalten zu erhalten. Der Hilfetext wurde wie folgt angepasst:

                  fe1ae134-5aa1-4ac4-9985-e8ce1d8dcf80-image.png

                  Ich will das mal an einem Beispiel demonstrieren. Wenn man anstelle des zigbee Adapter das zigbee2mqtt Projekt verwenden will und die JSON Objekte ebenfalls als einzelne Datenpunkt verfügbar haben will, kann man wie folgt vorgehen.

                  Ich zeige mal wie ich meine Bewegungsmelder unter zigbee2mqtt angelegt habe:

                  fb337adf-bb20-4a85-bd49-cf714b8d454c-image.png

                  Die Bewegungsmelder geben unter dem mqtt Adapter den Status als JSON Strings zurück, um das nun mit dieser Node alles auf einmal als Objektbaum im iobroker darzustellen, kann man das wie folgt erreichen:

                  Über die Wildcards liefert die mqtt-In Nodes alle Daten vom Bewegungsmelder - ob man oder wie man das topic modifiziert, kann man selbst entscheiden.

                  9e667342-6b82-438f-9d3e-1c49fddcd6a8-image.png

                  In der Subflow Node setzt man keepTopic auf true:

                  41730041-2be3-402b-bf2a-cc0c244b211e-image.png

                  Somit wird automatisch für alle Bewegungsmelder einzelne Datenpunkte unter 0_userdata.0 erstellt, also ein ganzer Objektbaum: 🙂

                  80534df5-8b4b-4c52-909f-faa6e314ae21-image.png

                  ? 1 Reply Last reply Reply Quote 0
                  • ?
                    A Former User @mickym last edited by

                    @mickym ich habe das Ganze mal ausprobiert und habe folgendes Problem. Wenn ich die gesamte Ordnerstruktur wie in deinem Beispiel einlese erhalte ich folgendes...

                    komplett.png

                    Also die gleiche Ordnerstruktur wie sie mein zigbee2mqtt anlegt. Wenn ich aber nur einen Sensor auslese dann habe ich das gewünschte Ergebnis.

                    einzeln.png

                    Was mache ich falsch?

                    node1.png node2.png node3.png node4.png

                    ? mickym 4 Replies Last reply Reply Quote 0
                    • ?
                      A Former User @Guest last edited by

                      @noah3112 Ach ja das mit dem Script habe ich auch gemacht. Ist das überhaupt noch nötig?

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

                        @noah3112 Also im Prinzip bist Du halt verantwortlich, dass Du das topic so aus Deinen msg.topics präparierst, dass das topic so erstellt wird.

                        Das topic wird nach dem top eingefügt - dann erfolgen die topics des Objektes. Ich hatte ja das Beispiel des Bewegungsmelders gebracht. Du kannst auch zigbee/# nehmen. Letztlich siehst Du ja wenn Du nach der subflow Node eine debug Node machst erst mal welche topics rauskommen.

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

                          @noah3112 Welches Script meinst Du?

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

                            @noah3112 Ich hab mal mein ganzen zigbee2mqtt Baum ausgelesen Indem ich als topic zigbee2mqtt/# nehme.

                            47e53d4d-7b22-4ade-b823-870e0220a461-image.png

                            [
                                {
                                    "id": "6e802f1553b18149",
                                    "type": "subflow",
                                    "name": "JSON or Obj to IOBroker",
                                    "info": "# Creates an IOBroker tree\n\nThis node creates an IOBroker tree out of an Java-Object or JSON String.\n\nThe object tree will be created under 0_userdata.0\nIn addition to the JSON-String or Java Object as `msg.payload` it is necessary to specify a `msg.top` properity in addition to the msg-Object.\n\nThe object tree will be created under 0_userdata.0\n\nExisting `msg.topic` entries will be deleted.\nAn iobroker-out node has to be appended to this subflow node. It is not part of the subflow itself. No topic should be specified in the iobroker out node.\n\nIs `msg.top` property isn't defined, the `top` property of the subflow-node is used. \n\nIn the properties of the subflow node a new property `keepTopic` has been added. Default is _false_ to keep the current behaviour. If set to _true_ then the originial topic will be placed between the `top` property of the subflow node and the property of the analyzed JSON object.\n\n**Attention:**\nIf msg.top and top is empty, all msg.topics (msg.topic) will be directly prefixed with 0_userdata.0. . \n\n# Erstellt einen Objektbaum im ioBroker\n\nDiese Node erstellt einen Objektbaum im ioBroker aus einem JAVA Objekt bzw. einem JSON String. \n\nDer Baum wird in jedem Fall unter 0_userdata.0 erstellt und zwar unter dem Topic der in` msg.top` mitgegeben wurde. In der `msg.payload` befindet sich dann der JSON String oder das entsprechende Objekt.\n\nExistierende `msg.topic `Einträge werden gelöscht.\nEin entsprechende iobroker-out Node muss an den Flow angehängt werden. Sie ist nicht Bestandteil des Subflows. In dieser iobroker-out Node darf kein Topic angegeben werden. \n\nFalls msg.top nicht definiert wurde, wird der `top`-Wert der Subflow-Node verwendet.\n\nIn den Eigenschaften der Subflow-Node wurde ein neuer Parameter `keepTopic` hinzugefügt. Standardwert ist _false_, um das bisherige Verhalten beizubehalten. Setzt man die Eigenschaft auf _true_, dann wird das originale Topic zwischen der `top` Eigenschaft der Subflow-Node und Eigenschaft des analysierten JSON Objektes eingefügt.\n\n**Achtung:**\nWenn top und msg.top leer ist, werden alle msg.topics (msg.topic) direkt unter dem Präfix 0_userdata.0., angelegt bzw. ausgegeben. ",
                                    "category": "",
                                    "in": [
                                        {
                                            "x": 60,
                                            "y": 160,
                                            "wires": [
                                                {
                                                    "id": "554b8c663bcb46c2"
                                                }
                                            ]
                                        }
                                    ],
                                    "out": [
                                        {
                                            "x": 2320,
                                            "y": 280,
                                            "wires": [
                                                {
                                                    "id": "f5d52c6a57d08904",
                                                    "port": 0
                                                },
                                                {
                                                    "id": "74c895ce724750de",
                                                    "port": 0
                                                }
                                            ]
                                        }
                                    ],
                                    "env": [
                                        {
                                            "name": "top",
                                            "type": "str",
                                            "value": "objRoot"
                                        },
                                        {
                                            "name": "keepTopic",
                                            "type": "bool",
                                            "value": "false"
                                        }
                                    ],
                                    "meta": {},
                                    "color": "#E2D96E",
                                    "icon": "node-red/batch.svg"
                                },
                                {
                                    "id": "3e11e8338f694832",
                                    "type": "split",
                                    "z": "6e802f1553b18149",
                                    "name": "split object",
                                    "splt": "\\n",
                                    "spltType": "str",
                                    "arraySplt": 1,
                                    "arraySpltType": "len",
                                    "stream": false,
                                    "addname": "key",
                                    "x": 1370,
                                    "y": 160,
                                    "wires": [
                                        [
                                            "0562a4249c8b856b"
                                        ]
                                    ]
                                },
                                {
                                    "id": "0562a4249c8b856b",
                                    "type": "change",
                                    "z": "6e802f1553b18149",
                                    "name": "add key to topic",
                                    "rules": [
                                        {
                                            "t": "set",
                                            "p": "stateName",
                                            "pt": "msg",
                                            "to": "key",
                                            "tot": "msg"
                                        },
                                        {
                                            "t": "set",
                                            "p": "topic",
                                            "pt": "msg",
                                            "to": "topic  & '.' & key",
                                            "tot": "jsonata"
                                        },
                                        {
                                            "t": "change",
                                            "p": "topic",
                                            "pt": "msg",
                                            "from": " ",
                                            "fromt": "str",
                                            "to": "_",
                                            "tot": "str"
                                        }
                                    ],
                                    "action": "",
                                    "property": "",
                                    "from": "",
                                    "to": "",
                                    "reg": false,
                                    "x": 1560,
                                    "y": 160,
                                    "wires": [
                                        [
                                            "ddc90985bef0fafa"
                                        ]
                                    ]
                                },
                                {
                                    "id": "ddc90985bef0fafa",
                                    "type": "switch",
                                    "z": "6e802f1553b18149",
                                    "name": "is type?",
                                    "property": "payload",
                                    "propertyType": "msg",
                                    "rules": [
                                        {
                                            "t": "istype",
                                            "v": "array",
                                            "vt": "array"
                                        },
                                        {
                                            "t": "istype",
                                            "v": "object",
                                            "vt": "object"
                                        },
                                        {
                                            "t": "else"
                                        }
                                    ],
                                    "checkall": "true",
                                    "repair": false,
                                    "outputs": 3,
                                    "x": 1740,
                                    "y": 160,
                                    "wires": [
                                        [
                                            "bfce19b206660fbe"
                                        ],
                                        [
                                            "3e11e8338f694832"
                                        ],
                                        [
                                            "1a8c03d866b85b12"
                                        ]
                                    ]
                                },
                                {
                                    "id": "bfce19b206660fbe",
                                    "type": "split",
                                    "z": "6e802f1553b18149",
                                    "name": "split array",
                                    "splt": "\\n",
                                    "spltType": "str",
                                    "arraySplt": 1,
                                    "arraySpltType": "len",
                                    "stream": false,
                                    "addname": "",
                                    "x": 780,
                                    "y": 280,
                                    "wires": [
                                        [
                                            "e89927810c6d75ec"
                                        ]
                                    ]
                                },
                                {
                                    "id": "e89927810c6d75ec",
                                    "type": "change",
                                    "z": "6e802f1553b18149",
                                    "name": "add index to topic",
                                    "rules": [
                                        {
                                            "t": "set",
                                            "p": "topic",
                                            "pt": "msg",
                                            "to": "topic  & '.' & parts.index",
                                            "tot": "jsonata"
                                        }
                                    ],
                                    "action": "",
                                    "property": "",
                                    "from": "",
                                    "to": "",
                                    "reg": false,
                                    "x": 970,
                                    "y": 280,
                                    "wires": [
                                        [
                                            "a4d1a5d04564dc77"
                                        ]
                                    ]
                                },
                                {
                                    "id": "f5d52c6a57d08904",
                                    "type": "change",
                                    "z": "6e802f1553b18149",
                                    "name": "finalize msg.topic",
                                    "rules": [
                                        {
                                            "t": "set",
                                            "p": "top",
                                            "pt": "msg",
                                            "to": "'0_userdata.0.' & top",
                                            "tot": "jsonata"
                                        },
                                        {
                                            "t": "set",
                                            "p": "topic",
                                            "pt": "msg",
                                            "to": "top & '.' & topic",
                                            "tot": "jsonata"
                                        },
                                        {
                                            "t": "change",
                                            "p": "topic",
                                            "pt": "msg",
                                            "from": "..",
                                            "fromt": "str",
                                            "to": ".",
                                            "tot": "str"
                                        }
                                    ],
                                    "action": "",
                                    "property": "",
                                    "from": "",
                                    "to": "",
                                    "reg": false,
                                    "x": 2170,
                                    "y": 240,
                                    "wires": [
                                        []
                                    ]
                                },
                                {
                                    "id": "c863dd7d651b2272",
                                    "type": "switch",
                                    "z": "6e802f1553b18149",
                                    "name": "is type?",
                                    "property": "payload",
                                    "propertyType": "msg",
                                    "rules": [
                                        {
                                            "t": "istype",
                                            "v": "json",
                                            "vt": "json"
                                        },
                                        {
                                            "t": "istype",
                                            "v": "object",
                                            "vt": "object"
                                        },
                                        {
                                            "t": "istype",
                                            "v": "array",
                                            "vt": "array"
                                        },
                                        {
                                            "t": "else"
                                        }
                                    ],
                                    "checkall": "true",
                                    "repair": false,
                                    "outputs": 4,
                                    "x": 580,
                                    "y": 160,
                                    "wires": [
                                        [
                                            "158930afddd0780b"
                                        ],
                                        [
                                            "3e11e8338f694832"
                                        ],
                                        [
                                            "bfce19b206660fbe"
                                        ],
                                        [
                                            "a4d1a5d04564dc77"
                                        ]
                                    ]
                                },
                                {
                                    "id": "158930afddd0780b",
                                    "type": "json",
                                    "z": "6e802f1553b18149",
                                    "name": "",
                                    "property": "payload",
                                    "action": "",
                                    "pretty": false,
                                    "x": 770,
                                    "y": 120,
                                    "wires": [
                                        [
                                            "3649300b4c233b10"
                                        ]
                                    ]
                                },
                                {
                                    "id": "1a8c03d866b85b12",
                                    "type": "switch",
                                    "z": "6e802f1553b18149",
                                    "name": "is msg.top != null",
                                    "property": "top",
                                    "propertyType": "msg",
                                    "rules": [
                                        {
                                            "t": "nnull"
                                        },
                                        {
                                            "t": "null"
                                        }
                                    ],
                                    "checkall": "true",
                                    "repair": false,
                                    "outputs": 2,
                                    "x": 1950,
                                    "y": 280,
                                    "wires": [
                                        [
                                            "f5d52c6a57d08904"
                                        ],
                                        [
                                            "74c895ce724750de"
                                        ]
                                    ]
                                },
                                {
                                    "id": "e023fe88445ce43e",
                                    "type": "change",
                                    "z": "6e802f1553b18149",
                                    "name": "",
                                    "rules": [
                                        {
                                            "t": "delete",
                                            "p": "topic",
                                            "pt": "msg"
                                        }
                                    ],
                                    "action": "",
                                    "property": "",
                                    "from": "",
                                    "to": "",
                                    "reg": false,
                                    "x": 350,
                                    "y": 200,
                                    "wires": [
                                        [
                                            "c863dd7d651b2272"
                                        ]
                                    ]
                                },
                                {
                                    "id": "3649300b4c233b10",
                                    "type": "switch",
                                    "z": "6e802f1553b18149",
                                    "name": "is array?",
                                    "property": "payload",
                                    "propertyType": "msg",
                                    "rules": [
                                        {
                                            "t": "istype",
                                            "v": "array",
                                            "vt": "array"
                                        },
                                        {
                                            "t": "else"
                                        }
                                    ],
                                    "checkall": "true",
                                    "repair": false,
                                    "outputs": 2,
                                    "x": 900,
                                    "y": 120,
                                    "wires": [
                                        [
                                            "bfce19b206660fbe"
                                        ],
                                        [
                                            "3e11e8338f694832"
                                        ]
                                    ]
                                },
                                {
                                    "id": "1b8480cd2df7ba3f",
                                    "type": "comment",
                                    "z": "6e802f1553b18149",
                                    "name": "Array",
                                    "info": "",
                                    "x": 600,
                                    "y": 280,
                                    "wires": []
                                },
                                {
                                    "id": "b3541807672be040",
                                    "type": "comment",
                                    "z": "6e802f1553b18149",
                                    "name": "object",
                                    "info": "",
                                    "x": 1340,
                                    "y": 100,
                                    "wires": []
                                },
                                {
                                    "id": "a4d1a5d04564dc77",
                                    "type": "switch",
                                    "z": "6e802f1553b18149",
                                    "name": "is type?",
                                    "property": "payload",
                                    "propertyType": "msg",
                                    "rules": [
                                        {
                                            "t": "istype",
                                            "v": "object",
                                            "vt": "object"
                                        },
                                        {
                                            "t": "istype",
                                            "v": "array",
                                            "vt": "array"
                                        },
                                        {
                                            "t": "else"
                                        }
                                    ],
                                    "checkall": "true",
                                    "repair": false,
                                    "outputs": 3,
                                    "x": 1160,
                                    "y": 280,
                                    "wires": [
                                        [
                                            "3e11e8338f694832"
                                        ],
                                        [
                                            "bfce19b206660fbe"
                                        ],
                                        [
                                            "1a8c03d866b85b12"
                                        ]
                                    ]
                                },
                                {
                                    "id": "74c895ce724750de",
                                    "type": "change",
                                    "z": "6e802f1553b18149",
                                    "name": "finalize msg.topic",
                                    "rules": [
                                        {
                                            "t": "set",
                                            "p": "top",
                                            "pt": "msg",
                                            "to": "top",
                                            "tot": "env"
                                        },
                                        {
                                            "t": "set",
                                            "p": "top",
                                            "pt": "msg",
                                            "to": "'0_userdata.0.' & top",
                                            "tot": "jsonata"
                                        },
                                        {
                                            "t": "set",
                                            "p": "topic",
                                            "pt": "msg",
                                            "to": "top & '.' & topic",
                                            "tot": "jsonata"
                                        },
                                        {
                                            "t": "change",
                                            "p": "topic",
                                            "pt": "msg",
                                            "from": "..",
                                            "fromt": "str",
                                            "to": ".",
                                            "tot": "str"
                                        }
                                    ],
                                    "action": "",
                                    "property": "",
                                    "from": "",
                                    "to": "",
                                    "reg": false,
                                    "x": 2170,
                                    "y": 320,
                                    "wires": [
                                        []
                                    ]
                                },
                                {
                                    "id": "554b8c663bcb46c2",
                                    "type": "switch",
                                    "z": "6e802f1553b18149",
                                    "name": "",
                                    "property": "keepTopic",
                                    "propertyType": "env",
                                    "rules": [
                                        {
                                            "t": "true"
                                        },
                                        {
                                            "t": "false"
                                        }
                                    ],
                                    "checkall": "true",
                                    "repair": false,
                                    "outputs": 2,
                                    "x": 170,
                                    "y": 160,
                                    "wires": [
                                        [
                                            "e30ba9f0483285e4"
                                        ],
                                        [
                                            "e023fe88445ce43e"
                                        ]
                                    ]
                                },
                                {
                                    "id": "e30ba9f0483285e4",
                                    "type": "change",
                                    "z": "6e802f1553b18149",
                                    "name": "",
                                    "rules": [
                                        {
                                            "t": "change",
                                            "p": "topic",
                                            "pt": "msg",
                                            "from": "/",
                                            "fromt": "str",
                                            "to": ".",
                                            "tot": "str"
                                        }
                                    ],
                                    "action": "",
                                    "property": "",
                                    "from": "",
                                    "to": "",
                                    "reg": false,
                                    "x": 350,
                                    "y": 120,
                                    "wires": [
                                        [
                                            "c863dd7d651b2272"
                                        ]
                                    ]
                                },
                                {
                                    "id": "6f9ad0fae408d97f",
                                    "type": "subflow:6e802f1553b18149",
                                    "z": "393214410c8f2f15",
                                    "name": "",
                                    "env": [
                                        {
                                            "name": "top",
                                            "value": "zigbee",
                                            "type": "str"
                                        },
                                        {
                                            "name": "keepTopic",
                                            "value": "true",
                                            "type": "bool"
                                        }
                                    ],
                                    "x": 790,
                                    "y": 460,
                                    "wires": [
                                        [
                                            "997fd3a2eec1d4d4",
                                            "ad50aeda7750ebf1"
                                        ]
                                    ]
                                },
                                {
                                    "id": "73ad04043864791c",
                                    "type": "change",
                                    "z": "393214410c8f2f15",
                                    "name": "",
                                    "rules": [
                                        {
                                            "t": "change",
                                            "p": "topic",
                                            "pt": "msg",
                                            "from": "zigbee2mqtt\\/(.*)$",
                                            "fromt": "re",
                                            "to": "$1",
                                            "tot": "str"
                                        }
                                    ],
                                    "action": "",
                                    "property": "",
                                    "from": "",
                                    "to": "",
                                    "reg": false,
                                    "x": 550,
                                    "y": 460,
                                    "wires": [
                                        [
                                            "6f9ad0fae408d97f",
                                            "5bfd9417fad9ef9a"
                                        ]
                                    ]
                                },
                                {
                                    "id": "997fd3a2eec1d4d4",
                                    "type": "debug",
                                    "z": "393214410c8f2f15",
                                    "name": "",
                                    "active": true,
                                    "tosidebar": true,
                                    "console": false,
                                    "tostatus": false,
                                    "complete": "false",
                                    "statusVal": "",
                                    "statusType": "auto",
                                    "x": 1030,
                                    "y": 400,
                                    "wires": []
                                },
                                {
                                    "id": "5bfd9417fad9ef9a",
                                    "type": "debug",
                                    "z": "393214410c8f2f15",
                                    "name": "",
                                    "active": true,
                                    "tosidebar": true,
                                    "console": false,
                                    "tostatus": false,
                                    "complete": "true",
                                    "targetType": "full",
                                    "statusVal": "",
                                    "statusType": "auto",
                                    "x": 750,
                                    "y": 400,
                                    "wires": []
                                },
                                {
                                    "id": "ad50aeda7750ebf1",
                                    "type": "ioBroker out",
                                    "z": "393214410c8f2f15",
                                    "name": "",
                                    "topic": "",
                                    "ack": "true",
                                    "autoCreate": "true",
                                    "stateName": "",
                                    "role": "",
                                    "payloadType": "",
                                    "readonly": "",
                                    "stateUnit": "",
                                    "stateMin": "",
                                    "stateMax": "",
                                    "x": 1020,
                                    "y": 460,
                                    "wires": []
                                }
                            ]
                            

                            Ich hab meinen zigbee2mqtt halt nach Funktionen gegliedert - aber wird alles aufgelöst.

                            50792a04-2fcb-4028-98e3-f29eac875a07-image.png,

                            die states werden auch korrekt aufgeschlüsselt:

                            bbaa56a9-2476-4084-bfdf-1c128159e45c-image.png

                            Importiere ggf- nochmals neu - damit Du auch die aktuellste Version hast.

                            Die Subflow Node muss halt keepTopic auf true sein:

                            ad3004fd-babd-4eec-88da-84348a0d2481-image.png

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

                              @noah3112 Wahrscheinlich hast Du Dein zigbee2mqtt struktur anders aufgebaut. Wie gesagt nutze mal zigbee2mqtt/# als input - die Change Node um das Topic zu isolieren, habe ich angepasst. Das mit dem Bewegungsmelder war nur ein Beispiel wenn man selektieren möchte und nicht den ganzen Baum abbilden will.

                              1 Reply Last reply Reply Quote 0
                              • ?
                                A Former User @mickym last edited by

                                @mickym sagte in JSON oder JavaScript Objekt in iobroker Datenpunkte zerlegen:

                                @waldmensch Generell muss man seit dem Admin5 - einmal auch immer noch das Skript von @fastfoot drüberlaufen lassen - das automatische Erstellen von Objekten im Admin5 lässt wohl auf sich warten.

                                Also dieses Skript importieren - und einmal ausführen:

                                /**
                                * Zweck:       Korrigiert übergeordnete Ordnerstrukturen eines Datenpunkts
                                * Datum:      07.08.2021
                                * Autor:       @fastfoot
                                */
                                
                                //              In den Settings der Javascript-Instanz muss setObject erlaubt sein!!!
                                
                                let arr = [],
                                  id = '';
                                
                                const ids = $('0_userdata.0.*');
                                ids.each(idTmp => {
                                  arr=idTmp.split('.');
                                  arr.splice(arr.length-1);
                                  id=arr.join('.');
                                  if(arr[0] === '0_userdata' || arr[0] === 'alias' || arr[0] === 'mqtt' || arr[0] === 'javascript'){
                                      correctObject(id, arr);
                                  }
                                })
                                
                                function correctObject(id,arr){
                                  if(arr.length === 2) return;
                                  if(!existsObject(id)){
                                      let obj = {};
                                      obj = {
                                          type: 'folder',
                                          common:{
                                              name: arr[arr.length - 1]
                                          }
                                      }
                                      extendObject(id, obj, e => {
                                          if (e) log('Fehler beim Schreiben des Objektes: '+ id + ' ' + e);
                                      })
                                  }
                                  arr.splice(arr.length - 1);
                                  id = arr.join('.');
                                  correctObject(id, arr);
                                }
                                

                                Muss man nur einmal machen - wenn die Objekte angelegt sind - kann der Flow ungehindert weiter in die einzelnen Datenpunkte schreiben.

                                Das war mit dem Script gemeint.

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

                                  @noah3112 Nein das brauchst Du nicht mehr. Das hat unser großer Meister gefixt wenn Du NodeRed Adapter Version 3.0 und höher verwendest, das heißt die Folder-Objekte werden korrekt angelegt (da unter 0_userdata.0). 😉

                                  ? 1 Reply Last reply Reply Quote 0
                                  • ?
                                    A Former User @mickym last edited by

                                    @mickym Bei mir ist es auch nach Funktionen gegliedert. Das war der Versuch mit den Umweltsensoren. Ich werde mal alles anwählen und schauen was passiert. keepTopic war auch auf true hatte vergessen davon ein Screenshot zu machen.

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

                                      @noah3112 Ja bei Dir musst halt ggf. zigbee/# verwenden und dann musst das topic halt mit dem regex Ausdruck richtig rausselektieren:

                                      de17eb3c-ec1f-4d87-9580-36a17ea8616c-image.png

                                      Wie gesagt - wichtig ist, dass das topic richtig aufbereitet wird, um es korrekt in dem Subflow zu verwenden.

                                      ? 1 Reply Last reply Reply Quote 0
                                      • ?
                                        A Former User @mickym last edited by

                                        @mickym so jetzt mal das gesamte Paket mit den von dir gerade gesendeten Flow laufen lassen. Das gleiche Ergebnis ich bekomme nur den state angezeigt. Komisch im debug Fenster bekomme ich wesentlich mehr Daten angezeigt diese werden nur nicht aufgelöst und dargestellt.

                                        mickym ? 2 Replies Last reply Reply Quote 0
                                        • mickym
                                          mickym Most Active @Guest last edited by

                                          @noah3112 Vielleicht browser refresh? - Wenn im debug alles rauskommt, dann müsste es auch so angelegt werden.

                                          1 Reply Last reply Reply Quote 0
                                          • ?
                                            A Former User @Guest last edited by

                                            @noah3112 Sorry das stimmt so nicht bei einigen Sensoren bzw Aktoren sind mehrere Daten vorhanden. Kann es sein das ich einfach nur warten muss bis alle mal gesendet haben?

                                            mickym 2 Replies 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

                                            node-red
                                            13
                                            114
                                            18487
                                            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