Skip to content
  • Recent
  • Tags
  • 0 Unread 0
  • Categories
  • Unreplied
  • Popular
  • GitHub
  • Docu
  • Hilfe
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Logo
  1. ioBroker Community Home
  2. Deutsch
  3. Skripten / Logik
  4. [Frage] Object werte in ein Array schreiben, Array auslesen und Wert zurück ins object schreiben

NEWS

  • UPDATE 31.10.: Amazon Alexa - ioBroker Skill läuft aus ?
    apollon77A
    apollon77
    48
    3
    8.1k

  • Monatsrückblick – September 2025
    BluefoxB
    Bluefox
    13
    1
    1.8k

  • Neues Video "KI im Smart Home" - ioBroker plus n8n
    BluefoxB
    Bluefox
    15
    1
    2.1k

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

Scheduled Pinned Locked Moved Skripten / Logik
6 Posts 2 Posters 1.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • DutchmanD Offline
    DutchmanD Offline
    Dutchman
    Developer Most Active Administrators
    wrote on last edited by
    #1

    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
    0
    • paul53P Offline
      paul53P Offline
      paul53
      wrote on last edited by
      #2

      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]
      

      Bitte verzichtet auf Chat-Nachrichten, denn die Handhabung ist grauenhaft !
      Produktiv: RPi 2 mit S.USV, HM-MOD-RPI und SLC-USB-Stick mit root fs

      1 Reply Last reply
      0
      • DutchmanD Offline
        DutchmanD Offline
        Dutchman
        Developer Most Active Administrators
        wrote on last edited by
        #3

        Super danke Paul ! werde ich heute Abend direkt probieren

        Sent from my iPhone using Tapatalk

        1 Reply Last reply
        0
        • DutchmanD Offline
          DutchmanD Offline
          Dutchman
          Developer Most Active Administrators
          wrote on last edited by
          #4

          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
          0
          • paul53P Offline
            paul53P Offline
            paul53
            wrote on last edited by
            #5

            @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);
            
            

            Bitte verzichtet auf Chat-Nachrichten, denn die Handhabung ist grauenhaft !
            Produktiv: RPi 2 mit S.USV, HM-MOD-RPI und SLC-USB-Stick mit root fs

            1 Reply Last reply
            0
            • DutchmanD Offline
              DutchmanD Offline
              Dutchman
              Developer Most Active Administrators
              wrote on last edited by
              #6

              @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
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              Support us

              ioBroker
              Community Adapters
              Donate

              307

              Online

              32.4k

              Users

              81.4k

              Topics

              1.3m

              Posts
              Community
              Impressum | Datenschutz-Bestimmungen | Nutzungsbedingungen
              ioBroker Community 2014-2025
              logo
              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Recent
              • Tags
              • Unread 0
              • Categories
              • Unreplied
              • Popular
              • GitHub
              • Docu
              • Hilfe