Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. Skripten / Logik
    4. JavaScript
    5. json teilen

    NEWS

    • Neuer Blog: Fotos und Eindrücke aus Solingen

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

    • ioBroker goes Matter ... Matter Adapter in Stable

    json teilen

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

      Ok dann bin ich raus. 😉 der Meister schreibt.

      1 Reply Last reply Reply Quote 0
      • W
        wolfgangkt @paul53 last edited by

        @paul53 Erstmal vielen Dank. Das Script läuft fehlerfrei durch aber es wird nichts geschrieben.

        const idJson = 'device-watcher.0.zigbee.listAll'; // Original
        const idJson1 = '0_userdata.0.Meine_Datenpunkte.Teil1'; // Teil 1
        const idJson2 = '0_userdata.0.Meine_Datenpunkte.Teil2'; // Teil 2
         
        on(idJson, function(dp) {
            let arr = JSON.parse(dp.state.val);
            const arr2 = [];
            for(let i = 30; i < arr.length - 30; i++) {
                arr2.push(arr[i]);
            }
            arr.length = 30;
            setState(idJson1, JSON.stringify(arr), true);
            setState(idJson2, JSON.stringify(arr2), true);
        });
        
        
        paul53 1 Reply Last reply Reply Quote 0
        • paul53
          paul53 @wolfgangkt last edited by

          @wolfgangkt sagte: es wird nichts geschrieben.

          Es wird nur bei Änderung im Original-JSON getriggert.

          1 Reply Last reply Reply Quote 0
          • W
            wolfgangkt last edited by

            @paul53 jetzt hagelt es Fehler

            2023-02-21 21:39:49.080 - info: device-watcher.0 (219822) Got terminate signal TERMINATE_YOURSELF
            2023-02-21 21:39:49.080 - info: device-watcher.0 (219822) cleaned everything up...
            2023-02-21 21:39:49.081 - info: device-watcher.0 (219822) terminating
            2023-02-21 21:39:49.081 - info: device-watcher.0 (219822) Terminated (ADAPTER_REQUESTED_TERMINATION): Without reason
            2023-02-21 21:39:53.468 - info: device-watcher.0 (219989) starting. Version 2.4.0 in /opt/iobroker/node_modules/iobroker.device-watcher, node: v16.19.1, js-controller: 4.0.24
            2023-02-21 21:39:53.482 - info: device-watcher.0 (219989) Number of selected adapters: 8. Loading data from: Fritzdect, Harmony, HmiP, Ping, Shelly, Sonoff, Wled, Zigbee ...
            2023-02-21 21:39:54.917 - error: javascript.0 (579) Error in callback: SyntaxError: Unexpected token o in JSON at position 1
            2023-02-21 21:39:54.918 - error: javascript.0 (579) at JSON.parse (:null:null)
            2023-02-21 21:39:54.918 - error: javascript.0 (579) at Object. (script.js.Testen.Teilen:6:20)
            2023-02-21 21:39:54.918 - error: javascript.0 (579) at Object.callback (/opt/iobroker/node_modules/iobroker.javascript/lib/sandbox.js:1244:38)
            2023-02-21 21:39:54.918 - error: javascript.0 (579) at Object.stateChange (/opt/iobroker/node_modules/iobroker.javascript/main.js:593:29)
            2023-02-21 21:39:54.918 - error: javascript.0 (579) at Immediate._onImmediate (/opt/iobroker/node_modules/@iobroker/js-controller-adapter/build/lib/adapter/adapter.js:5381:56)
            2023-02-21 21:39:54.918 - error: javascript.0 (579) at processImmediate (node:internal/timers:466:21)
            2023-02-21 21:39:55.327 - error: javascript.0 (579) Error in callback: SyntaxError: Unexpected token o in JSON at position 1
            2023-02-21 21:39:55.327 - error: javascript.0 (579) at JSON.parse (:null:null)
            2023-02-21 21:39:55.327 - error: javascript.0 (579) at Object. (script.js.Testen.Teilen:6:20)
            2023-02-21 21:39:55.327 - error: javascript.0 (579) at Object.callback (/opt/iobroker/node_modules/iobroker.javascript/lib/sandbox.js:1244:38)
            2023-02-21 21:39:55.328 - error: javascript.0 (579) at Object.stateChange (/opt/iobroker/node_modules/iobroker.javascript/main.js:593:29)
            2023-02-21 21:39:55.328 - error: javascript.0 (579) at Immediate._onImmediate (/opt/iobroker/node_modules/@iobroker/js-controller-adapter/build/lib/adapter/adapter.js:5381:56)
            2023-02-21 21:39:55.328 - error: javascript.0 (579) at processImmediate (node:internal/timers:466:21)
            
            
            paul53 1 Reply Last reply Reply Quote 0
            • paul53
              paul53 @wolfgangkt last edited by

              @wolfgangkt sagte: Unexpected token o in JSON at position 1

              Das Original-JSON lässt sich nicht parsen, da es offenbar fehlerhaft ist.

              W 1 Reply Last reply Reply Quote 0
              • W
                wolfgangkt @paul53 last edited by

                @paul53 schade. Ich danke Dir trotzdem für deine Hilfe.

                paul53 1 Reply Last reply Reply Quote 0
                • paul53
                  paul53 @wolfgangkt last edited by paul53

                  @wolfgangkt
                  Das JSON, das Du gepostet hast, lässt sich parsen. Teil 2 beginnt mit "Küchenfenster geschlossen".

                  W 1 Reply Last reply Reply Quote 0
                  • W
                    wolfgangkt @paul53 last edited by

                    @paul53 in dem Datenpunkt steht eigentlich immer das gleiche.
                    liegt es vielleicht an dem Datenpunkt selbst ?

                    {
                      "type": "state",
                      "common": {
                        "name": {
                          "en": "List of all devices",
                          "de": "Liste aller Geräte",
                          "ru": "Список всех устройств",
                          "pt": "Lista de todos os dispositivos",
                          "nl": "List van alle apparaten",
                          "fr": "Liste de tous les dispositifs",
                          "it": "Elenco di tutti i dispositivi",
                          "es": "Lista de todos los dispositivos",
                          "pl": "Lista wszystkich urządzeń",
                          "zh-cn": "所有装置清单"
                        },
                        "type": "array",
                        "role": "json",
                        "read": true,
                        "write": false
                      },
                      "native": {},
                      "from": "system.adapter.device-watcher.0",
                      "user": "system.user.admin",
                      "ts": 1676195799090,
                      "_id": "device-watcher.0.zigbee.listAll",
                      "acl": {
                        "object": 1636,
                        "state": 1636,
                        "owner": "system.user.admin",
                        "ownerGroup": "system.group.administrator"
                      }
                    }
                    
                    paul53 1 Reply Last reply Reply Quote 0
                    • paul53
                      paul53 @wolfgangkt last edited by

                      @wolfgangkt
                      Der Datenpunkt ist vom Typ "array", kann also nicht geparst werden. Ändere Zeile 6 in

                          let arr = dp.state.val;
                      
                      W 1 Reply Last reply Reply Quote 0
                      • W
                        wolfgangkt @paul53 last edited by

                        @paul53 jetzt funktioniert es. Vielen Dank für die Hilfe 👍 👍 👍

                        1 Reply Last reply Reply Quote 0
                        • W
                          wolfgangkt last edited by

                          @paul53 zu früh gefreut. In den 2. Datenpunkt werden nur 3 Datensätze geschrieben.

                          paul53 1 Reply Last reply Reply Quote 0
                          • paul53
                            paul53 @wolfgangkt last edited by paul53

                            @wolfgangkt sagte: In den 2. Datenpunkt werden nur 3 Datensätze geschrieben.

                            Ändere Zeile 8:

                                for(let i = 30; i < arr.length; i++) {
                            
                            W 1 Reply Last reply Reply Quote 0
                            • W
                              wolfgangkt @paul53 last edited by

                              @paul53 jetzt funktionierts. Besten Dank Dank nochmal.

                              haus-automatisierung 1 Reply Last reply Reply Quote 0
                              • haus-automatisierung
                                haus-automatisierung Developer Most Active @wolfgangkt last edited by

                                @wolfgangkt Hier noch eine etwas kürzere Alternative:

                                const idJson = '0_userdata.0.testjson'; // Original
                                const idJson1 = '0_userdata.0.json1'; // Teil 1
                                const idJson2 = '0_userdata.0.json2'; // Teil 2
                                  
                                on({ id: idJson, change: 'ne' }, (obj) => {
                                    const arr = JSON.parse(obj.state.val);
                                
                                    setState(idJson1, JSON.stringify(arr.slice(0, 30)), true);
                                    setState(idJson2, JSON.stringify(arr.slice(30)), true);
                                 });
                                
                                W 1 Reply Last reply Reply Quote 0
                                • W
                                  wolfgangkt @haus-automatisierung last edited by

                                  @haus-automatisierung Danke, kann ich auch nochmal probieren. Das Script von @paul53 funktioniert einwandfrei. Die 2 Tabellen in der VIS laufen schon zu meiner Zufriedenheit.

                                  paul53 1 Reply Last reply Reply Quote 0
                                  • paul53
                                    paul53 @wolfgangkt last edited by paul53

                                    @wolfgangkt sagte: kann ich auch nochmal probieren.

                                    Dann lass das Parsen in Zeile 6 weg (Datenpunkt liefert bereits ein Array).

                                    W 1 Reply Last reply Reply Quote 0
                                    • W
                                      wolfgangkt @paul53 last edited by

                                      @paul53 eine Frage hätte ich noch. Wie kann ich aus Original json bestimme Werte herausfiltern z.B alle Wassermelder und in einen eigenen Datenpunkt schreiben.

                                      haus-automatisierung 1 Reply Last reply Reply Quote 0
                                      • haus-automatisierung
                                        haus-automatisierung Developer Most Active @wolfgangkt last edited by

                                        @wolfgangkt z.B. mit Filter-Funktionen

                                        arr.filter(v => v.Device.startsWith('Wassermelder'));
                                        

                                        Also

                                        setState(idJson1, JSON.stringify(arr.filter(v => v.Device.startsWith('Wassermelder'))), true);
                                        
                                        W 1 Reply Last reply Reply Quote 0
                                        • W
                                          wolfgangkt @haus-automatisierung last edited by

                                          @haus-automatisierung Super 👍 Vielen Dank. Klappt einwandfrei.

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

                                          Support us

                                          ioBroker
                                          Community Adapters
                                          Donate

                                          674
                                          Online

                                          31.9k
                                          Users

                                          80.1k
                                          Topics

                                          1.3m
                                          Posts

                                          6
                                          37
                                          1744
                                          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