Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. Skripten / Logik
    4. JavaScript
    5. Pullen mehrerer DP aus history via Javascript

    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

    Pullen mehrerer DP aus history via Javascript

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

      Hi,
      ich brauch mal n Schubs in die richtige Richtung, hab mich gestern Nacht festgefahren.

      Ich möchte diverse Energie DP tabellarisch darstellen.

      Ich rufe das so aus dem History Adapter ab:

         sendTo('history.0', 'getHistory', {
              id: 'javascript.0.Verbrauch.pv_daily',
              options: {
                  start: end - (3600 * 1000 * 24 * 7),   //7 Tage hier eine variable einbauen aus Datenpunkt nach gewählter länge
                  end: end,
                  aggregate: 'onchange'
              }
          }, function (result) {
              for (var j = 0; j < result.result.length; j++) {
                  const dateObj = new Date(result.result[j].ts);
                  const month = dateObj.getUTCMonth() + 1; // months from 1-12
                  const day = dateObj.getUTCDate();
                  const year = dateObj.getUTCFullYear();
                  const newDate = year + "-" + month + "-" + day;
      
                  date = newDate;
                  data = result.result[j].val;
      
              }
          });
      

      Das würde ich jetzt gerne in einer For schleife für x DP nacheinander aufrufen.

      for (var i = 0; i < ArrayOfSensors.length; i += 2) {
          //log(i);
      
          sendTo('history.0', 'getHistory', {
              id: 'javascript.0.Verbrauch.' + ArrayOfSensors[i] + '_daily',
              options: {
                  start: end - (3600 * 1000 * 24 * 7),   //7 Tage hier eine variable einbauen aus DAtenpunkt nach gewählter länge
                  end: end,
                  aggregate: 'onchange'
              }
          }, function (result) {
              for (var j = 0; j < result.result.length; j++) {
                  const dateObj = new Date(result.result[j].ts);
                  const month = dateObj.getUTCMonth() + 1; // months from 1-12
                  const day = dateObj.getUTCDate();
                  const year = dateObj.getUTCFullYear();
                  const newDate = year + "-" + month + "-" + day;
      
                  date = newDate;
                  data = result.result[j].val;
      
                  matrix[i][j] = data;
      
              }
          });
      }
      

      Leider kommt die Antwort asynchron so, dass ich jedes mal die vorherige Frage überschreibe und nicht das bekomme was ich will.

      Wie sorge ich dafür, dass er nacheinander die Antworten bekommt und erst nach erhalt der vorherigen Daten den nächsten DP abfragt?
      mit nem Timeout geht es nicht...

      Besten Dank schonmal
      Nils

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

        @jmeister79 Da ich gestern praktisch die gleiche Frage beantwortet habe, verweise ich einfach mal darauf:

        https://forum.iobroker.net/topic/73260/gültigkeitsbereich-variablen/

        jmeister79 1 Reply Last reply Reply Quote 0
        • jmeister79
          jmeister79 @haus-automatisierung last edited by jmeister79

          @haus-automatisierung

          1000 Dank.
          das klappt auf den ersten blick auch.
          Wobei ich ein Problem hab.

          Wenn ich mir bei der Rückgabe den Rückgabe Array ansehe bekomme ich für jeden DP auch einen anderen werte Array.
          Schaue ich mir am Ende die Matrix an, ist jede Zeile gleich. Und zwar die letzte wird immer wiederholt.

          var ArrayOfSensors = [
              "Stromzaehler", "kWh",
              "Verbraucher", "kWh",
              "Einspeisung", "kWh",
              "Entfeuchter", "kWh",
              "Photovoltaik", "kWh",
              "Fronius", "Wh",
              "Wallbox", "kWh",
              "Trockner", "Wh",
              "Waschmaschine", "Wh"
          ];
          
          var date = "leer"
          var data = "leer"
          var line = "leer"
          var content = ""
          var end = Date.now();
          var matri3x = [0, 1];
          var matrix = [[0, 0], [0, 0]];
          
          createState("javascript.0.Helper.EnergyWidget", 'leer', { type: 'string', name: 'Energie Bilanz Widget', unit: '' });
          matrix[1][3] = "horst";
          
          async function start(i) {
              return new Promise(resolve => {
              sendTo('history.0', 'getHistory', {
                  id: 'javascript.0.Verbrauch.' + ArrayOfSensors[i] + '_daily',
                  options: {
                      start: end - (3600 * 1000 * 24 * 7),   //7 Tage hier eine variable einbauen aus DAtenpunkt nach gewählter länge
                      end: end,
                      aggregate: 'onchange'
                  }
              }, function (result) {
                  for (var j = 0; j < result.result.length; j++) {
                      const dateObj = new Date(result.result[j].ts);
                      const month = dateObj.getUTCMonth() + 1; // months from 1-12
                      const day = dateObj.getUTCDate();
                      const year = dateObj.getUTCFullYear();
                      const date = year + "-" + month + "-" + day;
          
                      matrix[j] = result.result[j].val;
                  }
                  //log(i)
                // log(matrix)
                      resolve(matrix)
              });
          
              });
          }
          var endmatrix = [];
          
          async function wrapper() {
          for (var i = 0; i < ArrayOfSensors.length; i += 2) {
              endmatrix[i/2]  =  await start(i);
             log(endmatrix[i/2])
          }
          for (var i = 0; i < ArrayOfSensors.length; i += 2) {
             log(endmatrix[i/2])
          }
          }
          
          wrapper();
          

          Logbuch

          11:40:13.199	info	javascript.0 (6543) Start javascript script.js.System_(Intern).EnergieBilanzWidget
          11:40:13.211	info	javascript.0 (6543) script.js.System_(Intern).EnergieBilanzWidget: registered 0 subscriptions, 0 schedules, 0 messages, 0 logs and 0 file subscriptions
          11:40:13.270	info	javascript.0 (6543) script.js.System_(Intern).EnergieBilanzWidget: [ null, 19.83, 9.17, 10.12, 8.05, 9.22, 10.08, 8.51 ]
          11:40:13.322	info	javascript.0 (6543) script.js.System_(Intern).EnergieBilanzWidget: [ null, 21.21, 25.91, 29.86, 20.19, 23.98, 19.25, 12.32 ]
          11:40:13.383	info	javascript.0 (6543) script.js.System_(Intern).EnergieBilanzWidget: [ null, 19.51, 2.97, 2.77, 3.35, 2.55, 3.32, 0.79 ]
          11:40:13.423	info	javascript.0 (6543) script.js.System_(Intern).EnergieBilanzWidget: [ null, 0, 0, 0, 0, 0, 0, 0 ]
          11:40:13.463	info	javascript.0 (6543) script.js.System_(Intern).EnergieBilanzWidget: [ null, 20.88, 19.71, 22.51, 15.49, 17.31, 12.49, 4.6 ]
          11:40:13.498	info	javascript.0 (6543) script.js.System_(Intern).EnergieBilanzWidget: [ null, 20.88, 19.74, 22.46, 15.56, 17.37, 11.54, 4.31 ]
          11:40:13.534	info	javascript.0 (6543) script.js.System_(Intern).EnergieBilanzWidget: [ null, 13.35, 10.49, 13.35, 9.96, 12.22, 3.87, 0 ]
          11:40:13.571	info	javascript.0 (6543) script.js.System_(Intern).EnergieBilanzWidget: [ null, 0, 4.26, 2.98, 0, 1.05, 1.31, 1.39 ]
          11:40:13.609	info	javascript.0 (6543) script.js.System_(Intern).EnergieBilanzWidget: [ null, 1.28, 1.93, 2.61, 0, 0.97, 1.9, 0.03 ]
          11:40:13.612	info	javascript.0 (6543) script.js.System_(Intern).EnergieBilanzWidget: [ null, 1.28, 1.93, 2.61, 0, 0.97, 1.9, 0.03 ]
          11:40:13.612	info	javascript.0 (6543) script.js.System_(Intern).EnergieBilanzWidget: [ null, 1.28, 1.93, 2.61, 0, 0.97, 1.9, 0.03 ]
          11:40:13.613	info	javascript.0 (6543) script.js.System_(Intern).EnergieBilanzWidget: [ null, 1.28, 1.93, 2.61, 0, 0.97, 1.9, 0.03 ]
          11:40:13.613	info	javascript.0 (6543) script.js.System_(Intern).EnergieBilanzWidget: [ null, 1.28, 1.93, 2.61, 0, 0.97, 1.9, 0.03 ]
          11:40:13.614	info	javascript.0 (6543) script.js.System_(Intern).EnergieBilanzWidget: [ null, 1.28, 1.93, 2.61, 0, 0.97, 1.9, 0.03 ]
          11:40:13.615	info	javascript.0 (6543) script.js.System_(Intern).EnergieBilanzWidget: [ null, 1.28, 1.93, 2.61, 0, 0.97, 1.9, 0.03 ]
          11:40:13.615	info	javascript.0 (6543) script.js.System_(Intern).EnergieBilanzWidget: [ null, 1.28, 1.93, 2.61, 0, 0.97, 1.9, 0.03 ]
          11:40:13.616	info	javascript.0 (6543) script.js.System_(Intern).EnergieBilanzWidget: [ null, 1.28, 1.93, 2.61, 0, 0.97, 1.9, 0.03 ]
          11:40:13.616	info	javascript.0 (6543) script.js.System_(Intern).EnergieBilanzWidget: [ null, 1.28, 1.93, 2.61, 0, 0.97, 1.9, 0.03 ]
          
          haus-automatisierung 1 Reply Last reply Reply Quote 0
          • haus-automatisierung
            haus-automatisierung Developer Most Active @jmeister79 last edited by

            @jmeister79 Das ist ja auch ziemlich verwirrend umgesetzt. Du hast eine globale Variable matrix, welche ständig wieder verwendet wird. Wenn jetzt ein Ergebnis länger ist als ein anderes dann bleiben die übrigen Zeilen ja enthalten.

            jmeister79 1 Reply Last reply Reply Quote 1
            • jmeister79
              jmeister79 @haus-automatisierung last edited by

              @haus-automatisierung jap das wars.
              das was noch vom rumprobieren übrig.

              Hab es jetzt in der function deklariert

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

                @jmeister79 Generell würde ich da einiges ändern. Das Array und dann jede zweite Position nehmen ist ja ziemlich umständlich.

                const arrayOfSensors = [
                    { id: 'Stromzaehler', unit: 'kWh' },
                    { id: 'Verbraucher', unit: 'kWh' },
                    { id: 'Einspeisung', unit: 'kWh' },
                    { id: 'Entfeuchter', unit: 'kWh' },
                    { id: 'Photovoltaik', unit: 'kWh' },
                    { id: 'Fronius', unit: 'Wh' },
                    { id: 'Wallbox', unit: 'kWh' },
                    { id: 'Trockner', unit: 'Wh' },
                    { id: 'Waschmaschine', unit: 'Wh' }
                ];
                
                
                createState("javascript.0.Helper.EnergyWidget", 'leer', { type: 'string', name: 'Energie Bilanz Widget' });
                
                async function getValues(end, id) {
                    return new Promise(resolve => {
                        sendTo('history.0', 'getHistory', {
                            id: `javascript.0.Verbrauch.${id}_daily`,
                            options: {
                                start: end - (3600 * 1000 * 24 * 7),   //7 Tage hier eine variable einbauen aus DAtenpunkt nach gewählter länge
                                end: end,
                                aggregate: 'onchange'
                            }
                        }, (result) => {
                            const matrix = [];
                
                            for (let j = 0; j < result.result.length; j++) {
                                // ???
                                const dateObj = new Date(result.result[j].ts);
                                const month = dateObj.getUTCMonth() + 1; // months from 1-12
                                const day = dateObj.getUTCDate();
                                const year = dateObj.getUTCFullYear();
                                const date = year + "-" + month + "-" + day;
                
                                matrix.push(result.result[j].val);
                            }
                
                            resolve(matrix);
                        });
                    });
                }
                
                async function run() {
                    const endmatrix = [];
                    const end = Date.now();
                
                    for (const row of arrayOfSensors) {
                        endmatrix.push(await getValues(end, row.id));
                    }
                
                    log(JSON.stringify(endmatrix));
                }
                
                run();
                
                jmeister79 1 Reply Last reply Reply Quote 1
                • jmeister79
                  jmeister79 @haus-automatisierung last edited by

                  @haus-automatisierung
                  ja das ist natürlich sehr viel eleganter. danke dir.

                  Ich hab dieses Prinzip damals aus unwissenheit begonnen und das nun überall so,
                  ist natürlich auch mal zeit das aufzuräumen.

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

                  Support us

                  ioBroker
                  Community Adapters
                  Donate

                  981
                  Online

                  31.8k
                  Users

                  80.0k
                  Topics

                  1.3m
                  Posts

                  2
                  7
                  177
                  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