Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. Skripten / Logik
    4. JavaScript
    5. Warten auf das Ergebnis der Funktion aus influxdb

    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

    Warten auf das Ergebnis der Funktion aus influxdb

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

      Hi,
      ich bringe von meinem Stromzähler den aktuellen Gesamtverbrauch (total_in) und den aktuellen Verbrauch (power_cur) in iobroker.

      Ich möchte jetzt eine Auswertung bauen und nehme dafür JS.

      Er soll in die Datenbank gehen und sich den aktuellen Verbrauch der letzten 2 Tage holen.

      Das klappt auch super, da habe ich jetzt ein paar Tausend Datensätze.
      Sehen so aus:

      {"result":[{"ack":true,"from":"system.adapter.sonoff.0","q":0,"val":225,"ts":1678795830890},{"ack":true,"from":"system.adapter.sonoff.0","q":0,"val":59,"ts":1678795860899}],"step":0,"error":null}
      

      Jetzt möchte ich pro Datensatz noch den jeweiligen Gesamtverbrauchstand zum Zeitpunkt des Timestamps ermitteln und hinzufügen dem Ergebnis hinzufügen.
      Das Ergebnis von oben soll dann so ausehen:

      {"ack":true,"from":"system.adapter.sonoff.0","q":0,"val":59,"ts":1678795860899, "total":23432.3}],"step":0,"error":null}
      

      Das Problem ist, dass ich die Funktion getHistoryWatt aufrufe und erst weitermachen möchte, bis ich das Ergebnis zurückhabe. Wie kann ich das konkret machen? Ich habe schon versuch irgendwas mit promises, await, async zu machen, alles gescheitert... kann mir jemand helfen? 🙂

      Mein Code:

      // returns device id of state, device or channel-id
      function getDeviceId(id) {
          return id.match(/(.+[a-zA-Z0-9]{24})/g);
      }
      
      
      function getHistoryWatt(index, start) {
                                  sendTo('influxdb.0', 'getHistory', {
                                                  id: 'sonoff.0.heimfelder7.SENSOR.MT681.Total_in',
                                                  options: {
                                                      start: start, 
                                                      count: 1,
                                                      aggregate: 'none' // or 'none' to get raw values
                                                  },
                                              }, function (watt_total_result) {
                                                  
                                                  if( watt_total_result.result.length > 0) {
                                                      var watt_total = watt_total_result.result[0].val;
      
                                                      return watt_total;
                                                  }
                                  });   
      }
      
      
      
      
          sendTo('influxdb.0', 'getHistory', {
                      id: 'sonoff.0.heimfelder7.SENSOR.MT681.Power_cur',
                      options: {
                          start:     new Date(new Date().setDate(new Date().getDate() - 2)).getTime(), // 2 tage zuvor127
                          end:       Date.now(),
                          count: 2,
                          aggregate: 'none' // or 'none' to get raw values
                      }
                  }, function (result) {
      
      
                      if( result.result.length > 0) {
      
                          for(var i=0; i < result.result.length; i++) {
      
                              var watt_total = getHistoryWatt(i, result.result[i].ts);
      
                              console.log(  i + " " + watt_total);
                              console.log(result.result[i]);
                                  
                          } // for
      
                      } // result > 0
                      
          }); // history call
      
      1 Reply Last reply Reply Quote 0
      • arteck
        arteck Developer Most Active last edited by arteck

        @cedric

        async function getHistoryWatt(index, start) {
        

        und bei aufruf

        await getHistoryWatt(i, result.result[i].ts);
        
        C 1 Reply Last reply Reply Quote 0
        • C
          cedric @arteck last edited by

          @arteck
          Vielen Dank für deine Antwort.
          Das hatte ich bereits probiert und bekomme immer beim await diese Fehlermeldung:

          'await' expressions are only allowed within async functions and at the top levels of modules.(1308)
          index0.js(62, 16): Did you mean to mark this function as 'async'?
          

          Hatte das dann versucht einzupacken in:

          async function main() {
          }
          

          Hat allerdings alles nichts gebracht... irgendeine Fehlermeldung kommt immer...

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

          Support us

          ioBroker
          Community Adapters
          Donate

          774
          Online

          31.9k
          Users

          80.1k
          Topics

          1.3m
          Posts

          2
          3
          167
          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