Skip to content
  • Home
  • 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
ioBroker Logo

Community Forum

donate donate
  1. ioBroker Community Home
  2. Deutsch
  3. Skripten / Logik
  4. JavaScript
  5. Wert aus callback an Programm übergeben

NEWS

  • Jahresrückblick 2025 – unser neuer Blogbeitrag ist online! ✨
    BluefoxB
    Bluefox
    17
    1
    3.5k

  • Neuer Blogbeitrag: Monatsrückblick - Dezember 2025 🎄
    BluefoxB
    Bluefox
    13
    1
    1.1k

  • Weihnachtsangebot 2025! 🎄
    BluefoxB
    Bluefox
    25
    1
    2.5k

Wert aus callback an Programm übergeben

Scheduled Pinned Locked Moved JavaScript
javascript
4 Posts 3 Posters 380 Views 2 Watching
  • 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.
  • stephan1827S Offline
    stephan1827S Offline
    stephan1827
    wrote on last edited by
    #1

    Hallo,
    ich möchte gerne historische Daten auslesen und weiter verarbeiten. Das Auslesen klappt auch soweit aber die Funktion sendTo erwartet eine Callback Funktion mit festen Parametern. Wie bekomme ich die Daten von der Callback Funktion an das Programm übergeben? Aktuell verwende ich den Umweg über einen zusätzlichen Datenpunkt aber das führt zu diversen Problemen.

        // --------------------------------------------------------------------
        // Give 7 latest values of variables
        sendTo('history.0', 'getHistory', {id: 'javascript.0.Solar.DailyTotal', options: {end: new Date().getTime(), count: 20, aggregate: 'onchange'}}, cbSendTo1);
        function cbSendTo1(result) 
        {
            let tmpDay = '';
            let tmpSTR = '';
            let x = 0;
            for (var i = result.result.length-1; i >= 0; i--) {
                if (tmpDay != new Date(result.result[i].ts).getDate().toString())
                {
                    tmpDay = new Date(result.result[i].ts).getDate().toString();
                    tmpSTR += "data.setCell(" + x.toString() + ", 0, '" + new Date(result.result[i].ts).getDate() + "." + new Date(result.result[i].ts).getMonth() + "." + "');\n";
                    if(result.result[i].val == null) {tmpSTR += "data.setCell(" + x.toString() + ", 1, '0');\n";}
                    else {tmpSTR += "data.setCell(" + x.toString() + ", 1, '" + result.result[i].val + "');\n";}
                    x++;
                    if (x > 6) break;
                    console.log('Total: ' + x.toString() + ' -> ' + new Date(result.result[i].ts).getDate() + "." + new Date(result.result[i].ts).getMonth() + ' -> ' + result.result[i].val);
                }
            }
            setState('Solar.html1', tmpSTR);
        }
            
        tmpData += getState('Solar.html1').val;
        console.log(getState('Solar.html1').val);
    
    paul53P 1 Reply Last reply
    0
    • stephan1827S stephan1827

      Hallo,
      ich möchte gerne historische Daten auslesen und weiter verarbeiten. Das Auslesen klappt auch soweit aber die Funktion sendTo erwartet eine Callback Funktion mit festen Parametern. Wie bekomme ich die Daten von der Callback Funktion an das Programm übergeben? Aktuell verwende ich den Umweg über einen zusätzlichen Datenpunkt aber das führt zu diversen Problemen.

          // --------------------------------------------------------------------
          // Give 7 latest values of variables
          sendTo('history.0', 'getHistory', {id: 'javascript.0.Solar.DailyTotal', options: {end: new Date().getTime(), count: 20, aggregate: 'onchange'}}, cbSendTo1);
          function cbSendTo1(result) 
          {
              let tmpDay = '';
              let tmpSTR = '';
              let x = 0;
              for (var i = result.result.length-1; i >= 0; i--) {
                  if (tmpDay != new Date(result.result[i].ts).getDate().toString())
                  {
                      tmpDay = new Date(result.result[i].ts).getDate().toString();
                      tmpSTR += "data.setCell(" + x.toString() + ", 0, '" + new Date(result.result[i].ts).getDate() + "." + new Date(result.result[i].ts).getMonth() + "." + "');\n";
                      if(result.result[i].val == null) {tmpSTR += "data.setCell(" + x.toString() + ", 1, '0');\n";}
                      else {tmpSTR += "data.setCell(" + x.toString() + ", 1, '" + result.result[i].val + "');\n";}
                      x++;
                      if (x > 6) break;
                      console.log('Total: ' + x.toString() + ' -> ' + new Date(result.result[i].ts).getDate() + "." + new Date(result.result[i].ts).getMonth() + ' -> ' + result.result[i].val);
                  }
              }
              setState('Solar.html1', tmpSTR);
          }
              
          tmpData += getState('Solar.html1').val;
          console.log(getState('Solar.html1').val);
      
      paul53P Offline
      paul53P Offline
      paul53
      wrote on last edited by
      #2

      @stephan1827 sagte:

      Wie bekomme ich die Daten von der Callback Funktion an das Programm übergeben?

      Weshalb willst Du die Daten an das Hauptprogramm übergeben und nicht innerhalb der Callback-Funktion weiter verarbeiten ?
      Eine Möglichkeit, den Wert eines Datenpunktes zu verarbeiten, ist ein Trigger.

      var tmpData = '';
      on({id: 'Solar.html1'}, function(dp) {
         tmpData += dp.state.val;
         log(dp.state.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
      • J.A.R.V.I.S.J Offline
        J.A.R.V.I.S.J Offline
        J.A.R.V.I.S.
        Developer
        wrote on last edited by
        #3

        @stephan1827 was genau meinst du mit übergeben?

        Du könntest daraus auch ein Promise machen und dann in einer asynchronen Funktion auf den Wert warten, dann musst du das nicht in einen State schreiben.

            // --------------------------------------------------------------------
        
            // Give 7 latest values of variables
        
        	function getHistory() {
        		return new Promise(resolve, reject) => {
        			
        			sendTo('history.0', 'getHistory', {id: 'javascript.0.Solar.DailyTotal', options: {end: new Date().getTime(), count: 20, aggregate: 'onchange'}}, result => {
        
        				let tmpDay = '';
        
        				let tmpSTR = '';
        
        				let x = 0;
        
        				for (let i = result.result.length - 1; i >= 0; i--) {
        
        					if (tmpDay != new Date(result.result[i].ts).getDate().toString()) {
        
        						tmpDay = new Date(result.result[i].ts).getDate().toString();
        
        						tmpSTR += "data.setCell(" + x.toString() + ", 0, '" + new Date(result.result[i].ts).getDate() + "." + new Date(result.result[i].ts).getMonth() + "." + "');\n";
        
        						if(result.result[i].val == null) {tmpSTR += "data.setCell(" + x.toString() + ", 1, '0');\n";}
        
        						else {tmpSTR += "data.setCell(" + x.toString() + ", 1, '" + result.result[i].val + "');\n";}
        
        						x++;
        
        						if (x > 6) break;
        
        						console.log('Total: ' + x.toString() + ' -> ' + new Date(result.result[i].ts).getDate() + "." + new Date(result.result[i].ts).getMonth() + ' -> ' + result.result[i].val);
        
        					}
        				}
        
        				resolve(tmpSTR);
        			});
        		}
        	}
        
        
        	async function tuWasMitHistory() {
        		
        		const history = await getHistory();
        		
        		tmpData += history;
        		
        		console.log(history);
        	}
        	
        	tuWasMitHistory();
        

        Oder du verarbeitest es direkt in deinem Callback.

        1 Reply Last reply
        0
        • stephan1827S Offline
          stephan1827S Offline
          stephan1827
          wrote on last edited by
          #4

          Ich muss diese Funktion mehrfach aufrufen und die Daten dann weiter verarbeiten. Ich habe es jetzt so gelöst.

          Funktionsaufruf

          const history = await GetData('javascript.0.Solar.DailyTotal');
          

          Funktion

          async function GetData(strID) {
              return new Promise(function (resolve, reject) {
                      sendTo('history.0', 'getHistory', {
                              id: strID,
                              options: {
                                  end: new Date().getTime(),
                                  count: 20,
                                  aggregate: 'onchange'
                              }
                          }, (result) => {
                              if (result.error) {
                                  reject('Error: ' + result.error);
                              } else {
                                  let retData = '';
                                  let tmpDay = '';
                                  let x = 0;
                                  for (var i = result.result.length - 1; i >= 0; i--) {
                                      if (tmpDay != new Date(result.result[i].ts).getDate().toString()) {
                                          tmpDay = new Date(result.result[i].ts).getDate().toString();
                                          retData += "data.setCell(" + x.toString() + ", 0, '" + new Date(result.result[i].ts).getDate() + "." + new Date(result.result[i].ts).getMonth() + "." + "');\n";
                                          if (result.result[i].val == null) {
                                              retData += "data.setCell(" + x.toString() + ", 1, '0');\n";
                                          } else {
                                              retData += "data.setCell(" + x.toString() + ", 1, '" + result.result[i].val + "');\n";
                                          }
                                          x++;
                                          if (x > 6) break;
                                          console.log('Total: ' + x.toString() + ' -> ' + new Date(result.result[i].ts).getDate() + "." + new Date(result.result[i].ts).getMonth() + ' -> ' + result.result[i].val);
                                      }
                                  //console.log('1---->' + retData + '<----');
                              };
                              resolve(retData);
                          }
                      });
              });
          }
          
          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

          654

          Online

          32.7k

          Users

          82.4k

          Topics

          1.3m

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

          • Don't have an account? Register

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