Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. Skripten / Logik
    4. [Frage] Object werte in ein Array schreiben, Array auslesen und Wert zurück ins object schreiben

    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

    [Frage] Object werte in ein Array schreiben, Array auslesen und Wert zurück ins object schreiben

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

      Hi zusammen,

      Mal wieder leider eine kleine Herausforderung wofür ich nicht gut genug im Scripten bin.

      Situation :

      ioBroker Installation muss fasiert umziehen von Host A nach B, beiden Installationen laufen bereits parallel.

      Durch Benutzung von Scripten (Energy Messung und andere) muss ich sowohl den object Baum aber auch die Werte der objecte übertragen.

      Die Export Funktion im Admin liefert aber leider nur den Baum ohne Werte.

      Jetzt hab ich mir folgende Lösung einfallen lassen:

      1. gewünschte objecte von A exportieren

      2. diese auf B importieren

      3. auf A per Script die Werte in ein object als Array schreiben (objectname/Wert)

      4. diese Array auf Host B bringen

      5. per Script Array auslesen und die Werte wieder in die objecte setzen

      Könnte mir bitte jemand auf die Sprünge helfen wie man das anstellen könnte ?

      Die Array Geschichte schreiben und auslesen sprengt leider meine eigene Kompetenz Zone 😞

      Ich würde gerne im Script den Baum* angeben wobei der gesamte Inhalt Dan in die Array geschrieben wird, und bei Option 2 dieses Array ausgelesen und Werte zurück ins object schreiben.

      Danke schonmal im voraus !

      ~Dutch

      Sent from my iPhone using Tapatalk

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

        So könntest Du die IDs + Werte in eine JSON-Datei schreiben:

        const tree = 'dp.0.';
        
        const path = '';
        const fn = path + tree + 'json';
        const dps = $(tree + '*');
        const fs = require('fs');
        
        var arr = [];
        
        dps.each(function(id, i) {
            var obj = {};
            obj.id = id;
            obj.val = getState(id).val;
            arr.push(obj);
        });
        
        writeFile(null, fn, JSON.stringify(arr));
        
        

        und so wieder einlesen:

        `const tree = 'dp.0.';
        
        const path = '';
        const fn = path + tree + 'json';
        const dps = $(tree + '*');
        const fs = require('fs');
        
        readFile(null, fn, function(err, data) {
            var arr = JSON.parse(data);
            for(let i = 0; i < arr.length; i++) {
                setState(arr[i].id, arr[i].val, true);
            }
        });` [/i][/i]
        
        1 Reply Last reply Reply Quote 0
        • Dutchman
          Dutchman Developer Most Active Administrators last edited by

          Super danke Paul ! werde ich heute Abend direkt probieren

          Sent from my iPhone using Tapatalk

          1 Reply Last reply Reply Quote 0
          • Dutchman
            Dutchman Developer Most Active Administrators last edited by

            Leider geht da was schief, habe logging punkte hinzugefügt :

            log("Start object export");
            const tree = 'javascript.0.Strom.';
            
            const path = '/tmp/';
            const fn = path + tree + 'json';
            const dps = $(tree + '*');
            const fs = require('fs');
            
            var arr = [];
            
            dps.each(function(id, i) {
                log("Object : " & id & "with value " & getState(id).val & " written to JSON");
                var obj = {};
                obj.id = id;
                obj.val = getState(id).val;
                arr.push(obj);
            
            });
            
            writeFile(null, fn, JSON.stringify(arr));
            log("Finished object export");
            
            

            Die log Ausgabe lautet :

            javascript.1	2018-09-05 22:42:21.684	info	script.js.Test_Omgeving.JSON_Export: Finished object export
            javascript.1	2018-09-05 22:42:21.683	info	script.js.Test_Omgeving.JSON_Export: 0
            javascript.1	2018-09-05 22:42:21.683	info	script.js.Test_Omgeving.JSON_Export: 0
            javascript.1	2018-09-05 22:42:21.683	info	script.js.Test_Omgeving.JSON_Export: 0
            javascript.1	2018-09-05 22:42:21.683	info	script.js.Test_Omgeving.JSON_Export: 0
            javascript.1	2018-09-05 22:42:21.682	info	script.js.Test_Omgeving.JSON_Export: 0
            javascript.1	2018-09-05 22:42:21.681	info	script.js.Test_Omgeving.JSON_Export: 0
            javascript.1	2018-09-05 22:42:21.681	info	script.js.Test_Omgeving.JSON_Export: 0
            javascript.1	2018-09-05 22:42:21.678	info	script.js.Test_Omgeving.JSON_Export: Start object export
            
            

            Der wert 0 im log wundert mich, es wird auch keine dabei erzeugt in /tmp/

            Dutch

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

              @Dutchman:

              es wird auch keine dabei erzeugt in /tmp/ `
              Lass mal path als Leerstring und dann schau mal unter iobroker-data/files/javascript
              @Dutchman:

              Der wert 0 im log wundert mich, `
              Ja, mich auch. Ändere mal das Log (so hatte ich es getestet):

                  log(id + ': ' + getState(id).val);
              
              
              1 Reply Last reply Reply Quote 0
              • Dutchman
                Dutchman Developer Most Active Administrators last edited by

                @paul53:

                Lass mal path als Leerstring und dann schau mal unter iobroker-data/files/javascript

                Danke, da stand die Datei dan auch also will er wohl nicht ausserhalb dieses Verzeichnisses arbeiten ?

                Ja, mich auch. Ändere mal das Log (so hatte ich es getestet):

                    log(id + ': ' + getState(id).val);
                
                ```` `  
                

                mein blöder Fehler, + anstatt & … jetzt klappts danke dir !!!

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

                Support us

                ioBroker
                Community Adapters
                Donate

                815
                Online

                31.7k
                Users

                79.9k
                Topics

                1.3m
                Posts

                2
                6
                1376
                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