Skip to content
  • Home
  • Aktuell
  • Tags
  • 0 Ungelesen 0
  • Kategorien
  • Unreplied
  • Beliebt
  • 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

  • Standard: (Kein Skin)
  • Kein Skin
Einklappen
ioBroker Logo

Community Forum

donate donate
  1. ioBroker Community Home
  2. Deutsch
  3. ioBroker Allgemein
  4. ehemalige Historydaten finden

NEWS

  • Monatsrückblick Januar/Februar 2026 ist online!
    BluefoxB
    Bluefox
    13
    1
    150

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

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

ehemalige Historydaten finden

Geplant Angeheftet Gesperrt Verschoben ioBroker Allgemein
148 Beiträge 7 Kommentatoren 9.3k Aufrufe 7 Watching
  • Älteste zuerst
  • Neuste zuerst
  • Meiste Stimmen
Antworten
  • In einem neuen Thema antworten
Anmelden zum Antworten
Dieses Thema wurde gelöscht. Nur Nutzer mit entsprechenden Rechten können es sehen.
  • M Offline
    M Offline
    Matt77CHE
    schrieb am zuletzt editiert von
    #141

    Im >Script zeigt es mit bei den Zeilen
    148
    214
    218
    238
    einen Fehler an

    // ********************************
    // findOldHistoryDPs v1.0.6
    // Copyright ©MCU
    // ********************************
    // v1.0.1 directDelete-> true  Möglichkeit die DatenDateien direkt löschen
    // v1.0.2 aliasId berücksichtigt bei enabledDPs
    // v1.0.3 zusätzliche Funktion listHistoryDPsWithaliasId('history.0');
    // v1.0.4 zusätzlich: fillAlias -> alle aktiven history-DPs mit alias in Array withAliasIdInEnabled
    //        fillEnabled_DPs -> nur die akitven History DPs in Array füllen -> enabled_DPs
    //        useAliasIdInEnabled -> true -> AliasId wird in enabled_DPs verwendet (was richtig ist, da die history-Werte mit alias geschrieben werden)
    // v1.0.5 zusätzliche Funktion: alias-Suche in allen ioBroker-DPs (unabhängig von history)
    // v1.0.6 error-Handling bei searchDir(),activeHistory, DP-Auswahl History
    
    
    let findOldDP                       = '0_userdata.0.history.findOldDPs';
    let findOldDPs_Old                  = findOldDP + '.old_DPs';
    let findOldDPs_Enabled              = findOldDP + '.enabled_DPs';
    let findOldDPs_InDB                 = findOldDP + '.inDB_DPS';
    let findOldDPs_BeginTS              = findOldDP + '.dirStart';
    let findOldDPs_EndTS                = findOldDP + '.dirEnd';
    let findOldDPs_Start                = findOldDP + '.start';
    let findOldDPs_DirectDel            = findOldDP + '.directDelete';
    let findOldDPs_fillEnabled          = findOldDP + '.fillEnabled_DPs';
    let findOldDPs_fillAlias            = findOldDP + '.fillWithAliasId';
    let findoldDPs_withAliasId          = findOldDP + '.withAliasId_DPs';
    let findOldDPs_useAliasInEnabled    = findOldDP + '.useAliasIdInEnabled';
    let activeHistoryDP                 = findOldDP + '.activeHistory';
    let findOldDPs_aliasDPs             = findOldDP + '.aliasDPs';
    let findOldDPs_aliasDPs_search      = findOldDP + '.aliasDPs_search'; 
    createStateAsync(findOldDPs_aliasDPs, {read: true, write: true, name: "ioBroker-DPs mit aliasId (unabhängig history)", type: "string", role: "text", def: JSON.stringify([]) });
    createStateAsync(findOldDPs_aliasDPs_search, {read: true, write: true, name: "Start Suche aliasIds in all ioBroker DPs", type: "boolean", role: "button", def: false });
    
    let dbNamespace = 'history.0';
    
    
    //createStateAsync(findOldDPs_Old, {read: true, write: true, name: "JSON für nicht genutzte DPs noch in DB", type: "string", role: "text", def: JSON.stringify([]) });
    createStateAsync(findOldDPs_Enabled, {read: true, write: true, name: "Aktive History-DPs in DB", type: "string", role: "text", def: JSON.stringify([]) });
    createStateAsync(findoldDPs_withAliasId, {read: true, write: true, name: "Aktive History-DPs mit aliasId", type: "string", role: "text", def: JSON.stringify([]) });
    //createStateAsync(findOldDPs_InDB, {read: true, write: true, name: "Genutzte DPs in DB", type: "string", role: "text", def: JSON.stringify([]) });
    createStateAsync(findOldDPs_BeginTS, {read: true, write: true, name: "Startverzeichnis", type: "number", role: "", def: 0 });
    createStateAsync(findOldDPs_EndTS, {read: true, write: true, name: "Endverzeichnis", type: "number", role: "", def: 10 }); //new Date().getTime()
    createStateAsync(findOldDPs_Start, {read: true, write: true, name: "Start Suche", type: "boolean", role: "button", def: false });
    createStateAsync(findOldDPs_fillEnabled, {read: true, write: true, name: "enabled_DPs suchen", type: "boolean", role: "button", def: false });
    createStateAsync(findOldDPs_fillAlias, {read: true, write: true, name: "withAliasId suchen", type: "boolean", role: "button", def: false });
    createStateAsync(findOldDPs_DirectDel, {read: true, write: true, name: "!!! Daten direkt löschen !!!", type: "boolean", role: "", def: false });
    createStateAsync(findOldDPs_useAliasInEnabled, {read: true, write: true, name: "AliasId in Enabled_DPs", type: "boolean", role: "", def: false });
    
    
    setTimeout(function(){
        findHistoryInstance()
    },3000)
    
    
    
    on({id: findOldDPs_Start, change: "any"}, function (obj) {
        let value = obj.state.val;
        if (value){
            //setState(findOldDPs_EndTS,parseInt(new Date().getTime()),false);
            let dbNamespace = getState(activeHistoryDP).val
            if(dbNamespace !=''){
                getEnabledHistoryDPs(dbNamespace);
                let fromDir = getState(findOldDPs_BeginTS).val;
                let toDir = getState(findOldDPs_EndTS).val;
                setTimeout(function(){
                    searchDir(fromDir,toDir);
                },5000);
            }else{
                console.warn('Es wurde keine History-Instanz ausgewählt!')
            }
            setStateDelayed(findOldDPs_Start,false,3000,false);
        }
    });
    
    on({id: findOldDPs_fillEnabled, change: "any"}, function (obj) {
        let value = obj.state.val;
        if (value){
            let dbNamespace = getState(activeHistoryDP).val
            if(dbNamespace !=''){
                getEnabledHistoryDPs(dbNamespace);
            }else{
                console.warn('Es wurde keine History-Instanz ausgewählt!')
            }
            setStateDelayed(findOldDPs_fillEnabled,false,3000,false);
        }
    });
    
    on({id: findOldDPs_fillAlias, change: "any"}, function (obj) {
        let value = obj.state.val;
        if (value){
            let dbNamespace = getState(activeHistoryDP).val
            if(dbNamespace !=''){
                listHistoryDPsWithaliasId(dbNamespace);
            }else{
                console.warn('Es wurde keine History-Instanz ausgewählt!')
            }
            setStateDelayed(findOldDPs_fillAlias,false,3000,false);
        }
    });
    
    on({id: findOldDPs_aliasDPs_search, change: "any"}, function (obj) {
        let value = obj.state.val;
        if (value){
            findDPsWithAliasId(); 
            setStateDelayed(findOldDPs_aliasDPs_search,false,3000,false);
        }
    });
    //listHistoryDPsWithaliasId('history.0');
    
    function listHistoryDPsWithaliasId(adp){
        let arr =[];
        sendTo(adp, 'getEnabledDPs', {}, function (result) {
            let count = 0;
            for ( let dp in result){
                let dpHistoryAlias = getObject(dp).common.custom[adp].aliasId;
                if (dpHistoryAlias != '' && dpHistoryAlias != undefined){
                    arr.push({'dpId':dp,'aliasId':dpHistoryAlias});
                    count++;
                }
            }
            log('Anzahl der aktiv geloggten DPs mit aliasId: '+count);        
            // setStateAsync(findOldDPs_Enabled,JSON.stringify(arr),false);
            //log(JSON.stringify(arr));
            setState(findoldDPs_withAliasId,JSON.stringify(arr),false);
        });
    }
    
    
    
    
    function findHistoryInstance(){
        let historyDPs = $('system.adapter.history.*.alive')
        let arr = []
        for(let i=0;i< historyDPs.length;i++){
            let instance = historyDPs[i].split('.')[3]
            arr.push(instance)
            //console.log(historyDPs[i])
        }
        console.log('Gefundene History Instanzen: '+arr)
        let obj = {'':'History auswählen'}
        for (let i=0;i<arr.length;i++){
            obj['history.'+arr[i]] = 'history.'+arr[i]
        }
        // console.log(obj)
        let firstHistory = 'history.'+arr[0]
        
        if(existsState(activeHistoryDP)){
            let histObj = getObject(activeHistoryDP)
            histObj.common.states = obj
            setObject(activeHistoryDP,histObj)
        }else{
            createStateAsync(activeHistoryDP, {read: true, write: true, name: "Auswahl aktive History", type: "string", role: "", def: firstHistory, states: JSON.stringify(obj) });
        }
    }
    
    //findDPsWithAliasId(); 
    
    function findDPsWithAliasId(){
        let allDPs = $('*');
        let arr = [];
        log('Anzahl der States: '+allDPs.length);
        for (let i=0;i<allDPs.length;i++){
            if (existsObject(allDPs[i])){
                if (getObject(allDPs[i]).common != undefined){
                    if (getObject(allDPs[i]).common.alias!= undefined){
                        let aliasId = getObject(allDPs[i]).common.alias.id;
                        if (aliasId != undefined){
                            //log(allDPs[i] +' mit aliasId: '+aliasId);
                            //break;
                            arr.push({'id':allDPs[i],'aliasId':aliasId});
                        }
                    }
                }
            }
        }
        //log(arr);
        setState(findOldDPs_aliasDPs,JSON.stringify(arr),false);
    }
    
    
    /*
    setTimeout(function(){
        getEnabledHistoryDPs(db);
        getHistoryDPinDB(db);
    },3000);
    */
    
    // getEnabledHistoryDPs('history.0');
    
    function getEnabledHistoryDPs(adp){
        let arr =[];
        let useAliasIdInenabled = getState(findOldDPs_useAliasInEnabled).val;
        sendTo(adp, 'getEnabledDPs', {}, function (result) {
            let count = 0;
            for ( let dp in result){
                let dpHistoryAlias = getObject(dp).common.custom[adp].aliasId;
                if (dpHistoryAlias != '' && dpHistoryAlias != undefined && useAliasIdInenabled){
                    arr.push(dpHistoryAlias);
                }else{
                    arr.push(dp);
                }
                count++;
            }
            log('Anzahl der aktiv geloggten DPs: '+count);        
            setStateAsync(findOldDPs_Enabled,JSON.stringify(arr),false);
        });
    }
    
    // getHistoryDPinDB('history.0');
    
    function getHistoryDPinDB(db){
        //Die 50 zuletzt gespeicherte Ereignisse für alle IDs holen:
        let beginTS = getState(findOldDPs_BeginTS).val;
        if (beginTS == 0){
            beginTS = parseInt(new Date().getTime()) - 30*24*60*60*1000;
        }
        let endTS = getState(findOldDPs_EndTS).val;
        if(endTS == 0){
            endTS = parseInt(new Date().getTime());
        }
        sendTo(db, 'getHistory', {
            id: '*',
            options: {
                begin: beginTS,
                end:   endTS,
                /* count:     50,*/
                addId:     true
            }
        }, function (result) {
            let dpArr =[];
            let count = 0;
            for (var i = 0; i < result.result.length; i++) {
                if (!isObjInArray(dpArr,result.result[i].id)){
                    dpArr.push(result.result[i].id);
                    count++;
                }
                //console.log(JSON.stringify(result.result[i]) + ' ' + new Date(result.result[i].ts).toISOString());
            }
            dpArr = dpArr.sort(function(a, b) {
                    return a > b;
                    });
            setStateAsync(findOldDPs_InDB,JSON.stringify(dpArr),false);
            //log('Anzahl der DPs in dem durchsuchten Bereich:'+count);
            log(count+' unterschiedliche DPs in "inDB" von '+ formatDate(beginTS,'DD.MM.YYYY hh:mm:ss')+' - '+formatDate(endTS,'DD.MM.YYYY hh:mm:ss'));
            //Vergleich der DPs
            let nonActiveArr = [];
            let activeDPs = JSON.parse(getState(findOldDPs_Enabled).val);
            for (let i=0; i< dpArr.length;i++){
                // dpArr[i]
                let vorhanden = false;
                for (let  j=0; j< activeDPs.length;j++ ){
                    if (dpArr[i] === activeDPs[j]){
                        vorhanden = true;
                        break; // vorhanden
                    }
                }
                if (!vorhanden){
                    // wird nicht mehr geloggt von active
                    nonActiveArr.push(dpArr[i]);
                }
            }
            setState(findOldDPs_Old,JSON.stringify(nonActiveArr),false);
        });
    }
    
    
    
    
    function isObjInArray(arr,itemObj){
        if (arr.length>0){
            for (let x=0;x<arr.length-1;x++){
                if (JSON.stringify(arr[x]) === JSON.stringify(itemObj)){
                    return true;
                }
            }
            return false;
        }else{
            return false;
        }
    }
    
    // searchDir(0,10);
    
    function searchDir(fromDir, toDir){
        let fs = require('fs');
        let activeDPs = JSON.parse(getState(findOldDPs_Enabled).val);
        let notActiveDPs = [];
        let dbNamespace = getState(activeHistoryDP).val
        if(dbNamespace !=''){
            let sysAdaptDir = getObject('system.adapter.'+dbNamespace).native.storeDir;
            console.warn('Ablage für History: '+dbNamespace+' im Verzeichnis: '+sysAdaptDir)
            fs.readdir(sysAdaptDir , function (erro, file) {
                if (erro) {
                    log('Fehler beim Lesen des Verzeichnisses: ' + erro, 'error');
                    return;
                }
                log(file.length+' Verzeichnisse wurde(n) gelesen!');
                log('fromDir und toDir dürfen nur im Bereich von 0 bis '+file.length +' sein! Bitte in 10er-Schritten verwenden!');
                log('Es wurde ein Bereich von '+fromDir +' bis '+ toDir+' gewählt-> es dauert '+(toDir-fromDir)*2 +' Sekunden');
                if (fromDir <0 || toDir >file.length){
    
                }else{
                    for (let j=fromDir; j<toDir; j++){
                        //log(file[j]);
                        if (file[j].indexOf('.')>=0 && file[j].length >8){
    
                        }else{
                            fs.readdir(sysAdaptDir+'/'+file[j] , function (error, files) {
                                log(files.length+' DPs im Verzeichnis: '+file[j]);
                                for (let i=0;i< files.length; i++){
                                    let dpFiles = files[i].substring(8,files[i].length-5);
                                    if (isObjInArray(activeDPs,dpFiles)){
                                    }else{
                                        notActiveDPs.push({'id':dpFiles,'dir':file[j]});
                                    }
                                }
                            });
                            //fs.unlinkSync('/opt/iobroker/iobroker-data/files/vis.0/'+jarvisDirName+'/'+files[i]+'/'+file[j]);
                        }
                    }
                    setTimeout(function(){
                        log('Nicht aktive DPs in den ausgewählten Verzeichnissen: '+notActiveDPs.length);
                        let olddir= '';
                        let directDelete = getState(findOldDPs_DirectDel).val;
                        for (let i=0;i<notActiveDPs.length;i++){
                            if (directDelete){
                                // Direkt löschen
                                fs.unlinkSync(sysAdaptDir+'/'+notActiveDPs[i].dir+'/history.'+notActiveDPs[i].id+'.json');
                            }
                            if (notActiveDPs[i].dir != olddir){
                                if (directDelete){
                                    log('Im Verzeichnis: '+notActiveDPs[i].dir +' folgende Dateien sind gelöscht!:','warn');
                                }else{
                                    log('Im Verzeichnis: '+notActiveDPs[i].dir +' können folgende Dateien gelöscht werden:','warn');
                                }
                                olddir = notActiveDPs[i].dir;
                            }
                            log(notActiveDPs[i].id);
                        }
                    },2000 * (toDir-fromDir)); // file.length
                }
            });
        }else{
            console.warn('Es wurde keine History-Instanz ausgewählt!')
        }
    }
    
    M HomoranH 3 Antworten Letzte Antwort
    0
    • M Matt77CHE

      Im >Script zeigt es mit bei den Zeilen
      148
      214
      218
      238
      einen Fehler an

      // ********************************
      // findOldHistoryDPs v1.0.6
      // Copyright ©MCU
      // ********************************
      // v1.0.1 directDelete-> true  Möglichkeit die DatenDateien direkt löschen
      // v1.0.2 aliasId berücksichtigt bei enabledDPs
      // v1.0.3 zusätzliche Funktion listHistoryDPsWithaliasId('history.0');
      // v1.0.4 zusätzlich: fillAlias -> alle aktiven history-DPs mit alias in Array withAliasIdInEnabled
      //        fillEnabled_DPs -> nur die akitven History DPs in Array füllen -> enabled_DPs
      //        useAliasIdInEnabled -> true -> AliasId wird in enabled_DPs verwendet (was richtig ist, da die history-Werte mit alias geschrieben werden)
      // v1.0.5 zusätzliche Funktion: alias-Suche in allen ioBroker-DPs (unabhängig von history)
      // v1.0.6 error-Handling bei searchDir(),activeHistory, DP-Auswahl History
      
      
      let findOldDP                       = '0_userdata.0.history.findOldDPs';
      let findOldDPs_Old                  = findOldDP + '.old_DPs';
      let findOldDPs_Enabled              = findOldDP + '.enabled_DPs';
      let findOldDPs_InDB                 = findOldDP + '.inDB_DPS';
      let findOldDPs_BeginTS              = findOldDP + '.dirStart';
      let findOldDPs_EndTS                = findOldDP + '.dirEnd';
      let findOldDPs_Start                = findOldDP + '.start';
      let findOldDPs_DirectDel            = findOldDP + '.directDelete';
      let findOldDPs_fillEnabled          = findOldDP + '.fillEnabled_DPs';
      let findOldDPs_fillAlias            = findOldDP + '.fillWithAliasId';
      let findoldDPs_withAliasId          = findOldDP + '.withAliasId_DPs';
      let findOldDPs_useAliasInEnabled    = findOldDP + '.useAliasIdInEnabled';
      let activeHistoryDP                 = findOldDP + '.activeHistory';
      let findOldDPs_aliasDPs             = findOldDP + '.aliasDPs';
      let findOldDPs_aliasDPs_search      = findOldDP + '.aliasDPs_search'; 
      createStateAsync(findOldDPs_aliasDPs, {read: true, write: true, name: "ioBroker-DPs mit aliasId (unabhängig history)", type: "string", role: "text", def: JSON.stringify([]) });
      createStateAsync(findOldDPs_aliasDPs_search, {read: true, write: true, name: "Start Suche aliasIds in all ioBroker DPs", type: "boolean", role: "button", def: false });
      
      let dbNamespace = 'history.0';
      
      
      //createStateAsync(findOldDPs_Old, {read: true, write: true, name: "JSON für nicht genutzte DPs noch in DB", type: "string", role: "text", def: JSON.stringify([]) });
      createStateAsync(findOldDPs_Enabled, {read: true, write: true, name: "Aktive History-DPs in DB", type: "string", role: "text", def: JSON.stringify([]) });
      createStateAsync(findoldDPs_withAliasId, {read: true, write: true, name: "Aktive History-DPs mit aliasId", type: "string", role: "text", def: JSON.stringify([]) });
      //createStateAsync(findOldDPs_InDB, {read: true, write: true, name: "Genutzte DPs in DB", type: "string", role: "text", def: JSON.stringify([]) });
      createStateAsync(findOldDPs_BeginTS, {read: true, write: true, name: "Startverzeichnis", type: "number", role: "", def: 0 });
      createStateAsync(findOldDPs_EndTS, {read: true, write: true, name: "Endverzeichnis", type: "number", role: "", def: 10 }); //new Date().getTime()
      createStateAsync(findOldDPs_Start, {read: true, write: true, name: "Start Suche", type: "boolean", role: "button", def: false });
      createStateAsync(findOldDPs_fillEnabled, {read: true, write: true, name: "enabled_DPs suchen", type: "boolean", role: "button", def: false });
      createStateAsync(findOldDPs_fillAlias, {read: true, write: true, name: "withAliasId suchen", type: "boolean", role: "button", def: false });
      createStateAsync(findOldDPs_DirectDel, {read: true, write: true, name: "!!! Daten direkt löschen !!!", type: "boolean", role: "", def: false });
      createStateAsync(findOldDPs_useAliasInEnabled, {read: true, write: true, name: "AliasId in Enabled_DPs", type: "boolean", role: "", def: false });
      
      
      setTimeout(function(){
          findHistoryInstance()
      },3000)
      
      
      
      on({id: findOldDPs_Start, change: "any"}, function (obj) {
          let value = obj.state.val;
          if (value){
              //setState(findOldDPs_EndTS,parseInt(new Date().getTime()),false);
              let dbNamespace = getState(activeHistoryDP).val
              if(dbNamespace !=''){
                  getEnabledHistoryDPs(dbNamespace);
                  let fromDir = getState(findOldDPs_BeginTS).val;
                  let toDir = getState(findOldDPs_EndTS).val;
                  setTimeout(function(){
                      searchDir(fromDir,toDir);
                  },5000);
              }else{
                  console.warn('Es wurde keine History-Instanz ausgewählt!')
              }
              setStateDelayed(findOldDPs_Start,false,3000,false);
          }
      });
      
      on({id: findOldDPs_fillEnabled, change: "any"}, function (obj) {
          let value = obj.state.val;
          if (value){
              let dbNamespace = getState(activeHistoryDP).val
              if(dbNamespace !=''){
                  getEnabledHistoryDPs(dbNamespace);
              }else{
                  console.warn('Es wurde keine History-Instanz ausgewählt!')
              }
              setStateDelayed(findOldDPs_fillEnabled,false,3000,false);
          }
      });
      
      on({id: findOldDPs_fillAlias, change: "any"}, function (obj) {
          let value = obj.state.val;
          if (value){
              let dbNamespace = getState(activeHistoryDP).val
              if(dbNamespace !=''){
                  listHistoryDPsWithaliasId(dbNamespace);
              }else{
                  console.warn('Es wurde keine History-Instanz ausgewählt!')
              }
              setStateDelayed(findOldDPs_fillAlias,false,3000,false);
          }
      });
      
      on({id: findOldDPs_aliasDPs_search, change: "any"}, function (obj) {
          let value = obj.state.val;
          if (value){
              findDPsWithAliasId(); 
              setStateDelayed(findOldDPs_aliasDPs_search,false,3000,false);
          }
      });
      //listHistoryDPsWithaliasId('history.0');
      
      function listHistoryDPsWithaliasId(adp){
          let arr =[];
          sendTo(adp, 'getEnabledDPs', {}, function (result) {
              let count = 0;
              for ( let dp in result){
                  let dpHistoryAlias = getObject(dp).common.custom[adp].aliasId;
                  if (dpHistoryAlias != '' && dpHistoryAlias != undefined){
                      arr.push({'dpId':dp,'aliasId':dpHistoryAlias});
                      count++;
                  }
              }
              log('Anzahl der aktiv geloggten DPs mit aliasId: '+count);        
              // setStateAsync(findOldDPs_Enabled,JSON.stringify(arr),false);
              //log(JSON.stringify(arr));
              setState(findoldDPs_withAliasId,JSON.stringify(arr),false);
          });
      }
      
      
      
      
      function findHistoryInstance(){
          let historyDPs = $('system.adapter.history.*.alive')
          let arr = []
          for(let i=0;i< historyDPs.length;i++){
              let instance = historyDPs[i].split('.')[3]
              arr.push(instance)
              //console.log(historyDPs[i])
          }
          console.log('Gefundene History Instanzen: '+arr)
          let obj = {'':'History auswählen'}
          for (let i=0;i<arr.length;i++){
              obj['history.'+arr[i]] = 'history.'+arr[i]
          }
          // console.log(obj)
          let firstHistory = 'history.'+arr[0]
          
          if(existsState(activeHistoryDP)){
              let histObj = getObject(activeHistoryDP)
              histObj.common.states = obj
              setObject(activeHistoryDP,histObj)
          }else{
              createStateAsync(activeHistoryDP, {read: true, write: true, name: "Auswahl aktive History", type: "string", role: "", def: firstHistory, states: JSON.stringify(obj) });
          }
      }
      
      //findDPsWithAliasId(); 
      
      function findDPsWithAliasId(){
          let allDPs = $('*');
          let arr = [];
          log('Anzahl der States: '+allDPs.length);
          for (let i=0;i<allDPs.length;i++){
              if (existsObject(allDPs[i])){
                  if (getObject(allDPs[i]).common != undefined){
                      if (getObject(allDPs[i]).common.alias!= undefined){
                          let aliasId = getObject(allDPs[i]).common.alias.id;
                          if (aliasId != undefined){
                              //log(allDPs[i] +' mit aliasId: '+aliasId);
                              //break;
                              arr.push({'id':allDPs[i],'aliasId':aliasId});
                          }
                      }
                  }
              }
          }
          //log(arr);
          setState(findOldDPs_aliasDPs,JSON.stringify(arr),false);
      }
      
      
      /*
      setTimeout(function(){
          getEnabledHistoryDPs(db);
          getHistoryDPinDB(db);
      },3000);
      */
      
      // getEnabledHistoryDPs('history.0');
      
      function getEnabledHistoryDPs(adp){
          let arr =[];
          let useAliasIdInenabled = getState(findOldDPs_useAliasInEnabled).val;
          sendTo(adp, 'getEnabledDPs', {}, function (result) {
              let count = 0;
              for ( let dp in result){
                  let dpHistoryAlias = getObject(dp).common.custom[adp].aliasId;
                  if (dpHistoryAlias != '' && dpHistoryAlias != undefined && useAliasIdInenabled){
                      arr.push(dpHistoryAlias);
                  }else{
                      arr.push(dp);
                  }
                  count++;
              }
              log('Anzahl der aktiv geloggten DPs: '+count);        
              setStateAsync(findOldDPs_Enabled,JSON.stringify(arr),false);
          });
      }
      
      // getHistoryDPinDB('history.0');
      
      function getHistoryDPinDB(db){
          //Die 50 zuletzt gespeicherte Ereignisse für alle IDs holen:
          let beginTS = getState(findOldDPs_BeginTS).val;
          if (beginTS == 0){
              beginTS = parseInt(new Date().getTime()) - 30*24*60*60*1000;
          }
          let endTS = getState(findOldDPs_EndTS).val;
          if(endTS == 0){
              endTS = parseInt(new Date().getTime());
          }
          sendTo(db, 'getHistory', {
              id: '*',
              options: {
                  begin: beginTS,
                  end:   endTS,
                  /* count:     50,*/
                  addId:     true
              }
          }, function (result) {
              let dpArr =[];
              let count = 0;
              for (var i = 0; i < result.result.length; i++) {
                  if (!isObjInArray(dpArr,result.result[i].id)){
                      dpArr.push(result.result[i].id);
                      count++;
                  }
                  //console.log(JSON.stringify(result.result[i]) + ' ' + new Date(result.result[i].ts).toISOString());
              }
              dpArr = dpArr.sort(function(a, b) {
                      return a > b;
                      });
              setStateAsync(findOldDPs_InDB,JSON.stringify(dpArr),false);
              //log('Anzahl der DPs in dem durchsuchten Bereich:'+count);
              log(count+' unterschiedliche DPs in "inDB" von '+ formatDate(beginTS,'DD.MM.YYYY hh:mm:ss')+' - '+formatDate(endTS,'DD.MM.YYYY hh:mm:ss'));
              //Vergleich der DPs
              let nonActiveArr = [];
              let activeDPs = JSON.parse(getState(findOldDPs_Enabled).val);
              for (let i=0; i< dpArr.length;i++){
                  // dpArr[i]
                  let vorhanden = false;
                  for (let  j=0; j< activeDPs.length;j++ ){
                      if (dpArr[i] === activeDPs[j]){
                          vorhanden = true;
                          break; // vorhanden
                      }
                  }
                  if (!vorhanden){
                      // wird nicht mehr geloggt von active
                      nonActiveArr.push(dpArr[i]);
                  }
              }
              setState(findOldDPs_Old,JSON.stringify(nonActiveArr),false);
          });
      }
      
      
      
      
      function isObjInArray(arr,itemObj){
          if (arr.length>0){
              for (let x=0;x<arr.length-1;x++){
                  if (JSON.stringify(arr[x]) === JSON.stringify(itemObj)){
                      return true;
                  }
              }
              return false;
          }else{
              return false;
          }
      }
      
      // searchDir(0,10);
      
      function searchDir(fromDir, toDir){
          let fs = require('fs');
          let activeDPs = JSON.parse(getState(findOldDPs_Enabled).val);
          let notActiveDPs = [];
          let dbNamespace = getState(activeHistoryDP).val
          if(dbNamespace !=''){
              let sysAdaptDir = getObject('system.adapter.'+dbNamespace).native.storeDir;
              console.warn('Ablage für History: '+dbNamespace+' im Verzeichnis: '+sysAdaptDir)
              fs.readdir(sysAdaptDir , function (erro, file) {
                  if (erro) {
                      log('Fehler beim Lesen des Verzeichnisses: ' + erro, 'error');
                      return;
                  }
                  log(file.length+' Verzeichnisse wurde(n) gelesen!');
                  log('fromDir und toDir dürfen nur im Bereich von 0 bis '+file.length +' sein! Bitte in 10er-Schritten verwenden!');
                  log('Es wurde ein Bereich von '+fromDir +' bis '+ toDir+' gewählt-> es dauert '+(toDir-fromDir)*2 +' Sekunden');
                  if (fromDir <0 || toDir >file.length){
      
                  }else{
                      for (let j=fromDir; j<toDir; j++){
                          //log(file[j]);
                          if (file[j].indexOf('.')>=0 && file[j].length >8){
      
                          }else{
                              fs.readdir(sysAdaptDir+'/'+file[j] , function (error, files) {
                                  log(files.length+' DPs im Verzeichnis: '+file[j]);
                                  for (let i=0;i< files.length; i++){
                                      let dpFiles = files[i].substring(8,files[i].length-5);
                                      if (isObjInArray(activeDPs,dpFiles)){
                                      }else{
                                          notActiveDPs.push({'id':dpFiles,'dir':file[j]});
                                      }
                                  }
                              });
                              //fs.unlinkSync('/opt/iobroker/iobroker-data/files/vis.0/'+jarvisDirName+'/'+files[i]+'/'+file[j]);
                          }
                      }
                      setTimeout(function(){
                          log('Nicht aktive DPs in den ausgewählten Verzeichnissen: '+notActiveDPs.length);
                          let olddir= '';
                          let directDelete = getState(findOldDPs_DirectDel).val;
                          for (let i=0;i<notActiveDPs.length;i++){
                              if (directDelete){
                                  // Direkt löschen
                                  fs.unlinkSync(sysAdaptDir+'/'+notActiveDPs[i].dir+'/history.'+notActiveDPs[i].id+'.json');
                              }
                              if (notActiveDPs[i].dir != olddir){
                                  if (directDelete){
                                      log('Im Verzeichnis: '+notActiveDPs[i].dir +' folgende Dateien sind gelöscht!:','warn');
                                  }else{
                                      log('Im Verzeichnis: '+notActiveDPs[i].dir +' können folgende Dateien gelöscht werden:','warn');
                                  }
                                  olddir = notActiveDPs[i].dir;
                              }
                              log(notActiveDPs[i].id);
                          }
                      },2000 * (toDir-fromDir)); // file.length
                  }
              });
          }else{
              console.warn('Es wurde keine History-Instanz ausgewählt!')
          }
      }
      
      M Online
      M Online
      MCU
      schrieb am zuletzt editiert von MCU
      #142

      @matt77che Die angezeigten Fehler sind nicht das Problem.
      Es geht um das Verzeichnis in dem die Daten liegen.
      Was steht im Log?
      -> Ablage für History
      Welche Instanz wurde ausgewählt

      Wie sind die Einstellungen in den History-Instanzen.

      NUC i7 64GB mit Proxmox ---- Jarvis Infos Aktualisierungen der Doku auf Instagram verfolgen -> mcuiobroker Instagram
      Wenn Euch mein Vorschlag geholfen hat, bitte rechts "^" klicken.

      1 Antwort Letzte Antwort
      0
      • M Matt77CHE

        Im >Script zeigt es mit bei den Zeilen
        148
        214
        218
        238
        einen Fehler an

        // ********************************
        // findOldHistoryDPs v1.0.6
        // Copyright ©MCU
        // ********************************
        // v1.0.1 directDelete-> true  Möglichkeit die DatenDateien direkt löschen
        // v1.0.2 aliasId berücksichtigt bei enabledDPs
        // v1.0.3 zusätzliche Funktion listHistoryDPsWithaliasId('history.0');
        // v1.0.4 zusätzlich: fillAlias -> alle aktiven history-DPs mit alias in Array withAliasIdInEnabled
        //        fillEnabled_DPs -> nur die akitven History DPs in Array füllen -> enabled_DPs
        //        useAliasIdInEnabled -> true -> AliasId wird in enabled_DPs verwendet (was richtig ist, da die history-Werte mit alias geschrieben werden)
        // v1.0.5 zusätzliche Funktion: alias-Suche in allen ioBroker-DPs (unabhängig von history)
        // v1.0.6 error-Handling bei searchDir(),activeHistory, DP-Auswahl History
        
        
        let findOldDP                       = '0_userdata.0.history.findOldDPs';
        let findOldDPs_Old                  = findOldDP + '.old_DPs';
        let findOldDPs_Enabled              = findOldDP + '.enabled_DPs';
        let findOldDPs_InDB                 = findOldDP + '.inDB_DPS';
        let findOldDPs_BeginTS              = findOldDP + '.dirStart';
        let findOldDPs_EndTS                = findOldDP + '.dirEnd';
        let findOldDPs_Start                = findOldDP + '.start';
        let findOldDPs_DirectDel            = findOldDP + '.directDelete';
        let findOldDPs_fillEnabled          = findOldDP + '.fillEnabled_DPs';
        let findOldDPs_fillAlias            = findOldDP + '.fillWithAliasId';
        let findoldDPs_withAliasId          = findOldDP + '.withAliasId_DPs';
        let findOldDPs_useAliasInEnabled    = findOldDP + '.useAliasIdInEnabled';
        let activeHistoryDP                 = findOldDP + '.activeHistory';
        let findOldDPs_aliasDPs             = findOldDP + '.aliasDPs';
        let findOldDPs_aliasDPs_search      = findOldDP + '.aliasDPs_search'; 
        createStateAsync(findOldDPs_aliasDPs, {read: true, write: true, name: "ioBroker-DPs mit aliasId (unabhängig history)", type: "string", role: "text", def: JSON.stringify([]) });
        createStateAsync(findOldDPs_aliasDPs_search, {read: true, write: true, name: "Start Suche aliasIds in all ioBroker DPs", type: "boolean", role: "button", def: false });
        
        let dbNamespace = 'history.0';
        
        
        //createStateAsync(findOldDPs_Old, {read: true, write: true, name: "JSON für nicht genutzte DPs noch in DB", type: "string", role: "text", def: JSON.stringify([]) });
        createStateAsync(findOldDPs_Enabled, {read: true, write: true, name: "Aktive History-DPs in DB", type: "string", role: "text", def: JSON.stringify([]) });
        createStateAsync(findoldDPs_withAliasId, {read: true, write: true, name: "Aktive History-DPs mit aliasId", type: "string", role: "text", def: JSON.stringify([]) });
        //createStateAsync(findOldDPs_InDB, {read: true, write: true, name: "Genutzte DPs in DB", type: "string", role: "text", def: JSON.stringify([]) });
        createStateAsync(findOldDPs_BeginTS, {read: true, write: true, name: "Startverzeichnis", type: "number", role: "", def: 0 });
        createStateAsync(findOldDPs_EndTS, {read: true, write: true, name: "Endverzeichnis", type: "number", role: "", def: 10 }); //new Date().getTime()
        createStateAsync(findOldDPs_Start, {read: true, write: true, name: "Start Suche", type: "boolean", role: "button", def: false });
        createStateAsync(findOldDPs_fillEnabled, {read: true, write: true, name: "enabled_DPs suchen", type: "boolean", role: "button", def: false });
        createStateAsync(findOldDPs_fillAlias, {read: true, write: true, name: "withAliasId suchen", type: "boolean", role: "button", def: false });
        createStateAsync(findOldDPs_DirectDel, {read: true, write: true, name: "!!! Daten direkt löschen !!!", type: "boolean", role: "", def: false });
        createStateAsync(findOldDPs_useAliasInEnabled, {read: true, write: true, name: "AliasId in Enabled_DPs", type: "boolean", role: "", def: false });
        
        
        setTimeout(function(){
            findHistoryInstance()
        },3000)
        
        
        
        on({id: findOldDPs_Start, change: "any"}, function (obj) {
            let value = obj.state.val;
            if (value){
                //setState(findOldDPs_EndTS,parseInt(new Date().getTime()),false);
                let dbNamespace = getState(activeHistoryDP).val
                if(dbNamespace !=''){
                    getEnabledHistoryDPs(dbNamespace);
                    let fromDir = getState(findOldDPs_BeginTS).val;
                    let toDir = getState(findOldDPs_EndTS).val;
                    setTimeout(function(){
                        searchDir(fromDir,toDir);
                    },5000);
                }else{
                    console.warn('Es wurde keine History-Instanz ausgewählt!')
                }
                setStateDelayed(findOldDPs_Start,false,3000,false);
            }
        });
        
        on({id: findOldDPs_fillEnabled, change: "any"}, function (obj) {
            let value = obj.state.val;
            if (value){
                let dbNamespace = getState(activeHistoryDP).val
                if(dbNamespace !=''){
                    getEnabledHistoryDPs(dbNamespace);
                }else{
                    console.warn('Es wurde keine History-Instanz ausgewählt!')
                }
                setStateDelayed(findOldDPs_fillEnabled,false,3000,false);
            }
        });
        
        on({id: findOldDPs_fillAlias, change: "any"}, function (obj) {
            let value = obj.state.val;
            if (value){
                let dbNamespace = getState(activeHistoryDP).val
                if(dbNamespace !=''){
                    listHistoryDPsWithaliasId(dbNamespace);
                }else{
                    console.warn('Es wurde keine History-Instanz ausgewählt!')
                }
                setStateDelayed(findOldDPs_fillAlias,false,3000,false);
            }
        });
        
        on({id: findOldDPs_aliasDPs_search, change: "any"}, function (obj) {
            let value = obj.state.val;
            if (value){
                findDPsWithAliasId(); 
                setStateDelayed(findOldDPs_aliasDPs_search,false,3000,false);
            }
        });
        //listHistoryDPsWithaliasId('history.0');
        
        function listHistoryDPsWithaliasId(adp){
            let arr =[];
            sendTo(adp, 'getEnabledDPs', {}, function (result) {
                let count = 0;
                for ( let dp in result){
                    let dpHistoryAlias = getObject(dp).common.custom[adp].aliasId;
                    if (dpHistoryAlias != '' && dpHistoryAlias != undefined){
                        arr.push({'dpId':dp,'aliasId':dpHistoryAlias});
                        count++;
                    }
                }
                log('Anzahl der aktiv geloggten DPs mit aliasId: '+count);        
                // setStateAsync(findOldDPs_Enabled,JSON.stringify(arr),false);
                //log(JSON.stringify(arr));
                setState(findoldDPs_withAliasId,JSON.stringify(arr),false);
            });
        }
        
        
        
        
        function findHistoryInstance(){
            let historyDPs = $('system.adapter.history.*.alive')
            let arr = []
            for(let i=0;i< historyDPs.length;i++){
                let instance = historyDPs[i].split('.')[3]
                arr.push(instance)
                //console.log(historyDPs[i])
            }
            console.log('Gefundene History Instanzen: '+arr)
            let obj = {'':'History auswählen'}
            for (let i=0;i<arr.length;i++){
                obj['history.'+arr[i]] = 'history.'+arr[i]
            }
            // console.log(obj)
            let firstHistory = 'history.'+arr[0]
            
            if(existsState(activeHistoryDP)){
                let histObj = getObject(activeHistoryDP)
                histObj.common.states = obj
                setObject(activeHistoryDP,histObj)
            }else{
                createStateAsync(activeHistoryDP, {read: true, write: true, name: "Auswahl aktive History", type: "string", role: "", def: firstHistory, states: JSON.stringify(obj) });
            }
        }
        
        //findDPsWithAliasId(); 
        
        function findDPsWithAliasId(){
            let allDPs = $('*');
            let arr = [];
            log('Anzahl der States: '+allDPs.length);
            for (let i=0;i<allDPs.length;i++){
                if (existsObject(allDPs[i])){
                    if (getObject(allDPs[i]).common != undefined){
                        if (getObject(allDPs[i]).common.alias!= undefined){
                            let aliasId = getObject(allDPs[i]).common.alias.id;
                            if (aliasId != undefined){
                                //log(allDPs[i] +' mit aliasId: '+aliasId);
                                //break;
                                arr.push({'id':allDPs[i],'aliasId':aliasId});
                            }
                        }
                    }
                }
            }
            //log(arr);
            setState(findOldDPs_aliasDPs,JSON.stringify(arr),false);
        }
        
        
        /*
        setTimeout(function(){
            getEnabledHistoryDPs(db);
            getHistoryDPinDB(db);
        },3000);
        */
        
        // getEnabledHistoryDPs('history.0');
        
        function getEnabledHistoryDPs(adp){
            let arr =[];
            let useAliasIdInenabled = getState(findOldDPs_useAliasInEnabled).val;
            sendTo(adp, 'getEnabledDPs', {}, function (result) {
                let count = 0;
                for ( let dp in result){
                    let dpHistoryAlias = getObject(dp).common.custom[adp].aliasId;
                    if (dpHistoryAlias != '' && dpHistoryAlias != undefined && useAliasIdInenabled){
                        arr.push(dpHistoryAlias);
                    }else{
                        arr.push(dp);
                    }
                    count++;
                }
                log('Anzahl der aktiv geloggten DPs: '+count);        
                setStateAsync(findOldDPs_Enabled,JSON.stringify(arr),false);
            });
        }
        
        // getHistoryDPinDB('history.0');
        
        function getHistoryDPinDB(db){
            //Die 50 zuletzt gespeicherte Ereignisse für alle IDs holen:
            let beginTS = getState(findOldDPs_BeginTS).val;
            if (beginTS == 0){
                beginTS = parseInt(new Date().getTime()) - 30*24*60*60*1000;
            }
            let endTS = getState(findOldDPs_EndTS).val;
            if(endTS == 0){
                endTS = parseInt(new Date().getTime());
            }
            sendTo(db, 'getHistory', {
                id: '*',
                options: {
                    begin: beginTS,
                    end:   endTS,
                    /* count:     50,*/
                    addId:     true
                }
            }, function (result) {
                let dpArr =[];
                let count = 0;
                for (var i = 0; i < result.result.length; i++) {
                    if (!isObjInArray(dpArr,result.result[i].id)){
                        dpArr.push(result.result[i].id);
                        count++;
                    }
                    //console.log(JSON.stringify(result.result[i]) + ' ' + new Date(result.result[i].ts).toISOString());
                }
                dpArr = dpArr.sort(function(a, b) {
                        return a > b;
                        });
                setStateAsync(findOldDPs_InDB,JSON.stringify(dpArr),false);
                //log('Anzahl der DPs in dem durchsuchten Bereich:'+count);
                log(count+' unterschiedliche DPs in "inDB" von '+ formatDate(beginTS,'DD.MM.YYYY hh:mm:ss')+' - '+formatDate(endTS,'DD.MM.YYYY hh:mm:ss'));
                //Vergleich der DPs
                let nonActiveArr = [];
                let activeDPs = JSON.parse(getState(findOldDPs_Enabled).val);
                for (let i=0; i< dpArr.length;i++){
                    // dpArr[i]
                    let vorhanden = false;
                    for (let  j=0; j< activeDPs.length;j++ ){
                        if (dpArr[i] === activeDPs[j]){
                            vorhanden = true;
                            break; // vorhanden
                        }
                    }
                    if (!vorhanden){
                        // wird nicht mehr geloggt von active
                        nonActiveArr.push(dpArr[i]);
                    }
                }
                setState(findOldDPs_Old,JSON.stringify(nonActiveArr),false);
            });
        }
        
        
        
        
        function isObjInArray(arr,itemObj){
            if (arr.length>0){
                for (let x=0;x<arr.length-1;x++){
                    if (JSON.stringify(arr[x]) === JSON.stringify(itemObj)){
                        return true;
                    }
                }
                return false;
            }else{
                return false;
            }
        }
        
        // searchDir(0,10);
        
        function searchDir(fromDir, toDir){
            let fs = require('fs');
            let activeDPs = JSON.parse(getState(findOldDPs_Enabled).val);
            let notActiveDPs = [];
            let dbNamespace = getState(activeHistoryDP).val
            if(dbNamespace !=''){
                let sysAdaptDir = getObject('system.adapter.'+dbNamespace).native.storeDir;
                console.warn('Ablage für History: '+dbNamespace+' im Verzeichnis: '+sysAdaptDir)
                fs.readdir(sysAdaptDir , function (erro, file) {
                    if (erro) {
                        log('Fehler beim Lesen des Verzeichnisses: ' + erro, 'error');
                        return;
                    }
                    log(file.length+' Verzeichnisse wurde(n) gelesen!');
                    log('fromDir und toDir dürfen nur im Bereich von 0 bis '+file.length +' sein! Bitte in 10er-Schritten verwenden!');
                    log('Es wurde ein Bereich von '+fromDir +' bis '+ toDir+' gewählt-> es dauert '+(toDir-fromDir)*2 +' Sekunden');
                    if (fromDir <0 || toDir >file.length){
        
                    }else{
                        for (let j=fromDir; j<toDir; j++){
                            //log(file[j]);
                            if (file[j].indexOf('.')>=0 && file[j].length >8){
        
                            }else{
                                fs.readdir(sysAdaptDir+'/'+file[j] , function (error, files) {
                                    log(files.length+' DPs im Verzeichnis: '+file[j]);
                                    for (let i=0;i< files.length; i++){
                                        let dpFiles = files[i].substring(8,files[i].length-5);
                                        if (isObjInArray(activeDPs,dpFiles)){
                                        }else{
                                            notActiveDPs.push({'id':dpFiles,'dir':file[j]});
                                        }
                                    }
                                });
                                //fs.unlinkSync('/opt/iobroker/iobroker-data/files/vis.0/'+jarvisDirName+'/'+files[i]+'/'+file[j]);
                            }
                        }
                        setTimeout(function(){
                            log('Nicht aktive DPs in den ausgewählten Verzeichnissen: '+notActiveDPs.length);
                            let olddir= '';
                            let directDelete = getState(findOldDPs_DirectDel).val;
                            for (let i=0;i<notActiveDPs.length;i++){
                                if (directDelete){
                                    // Direkt löschen
                                    fs.unlinkSync(sysAdaptDir+'/'+notActiveDPs[i].dir+'/history.'+notActiveDPs[i].id+'.json');
                                }
                                if (notActiveDPs[i].dir != olddir){
                                    if (directDelete){
                                        log('Im Verzeichnis: '+notActiveDPs[i].dir +' folgende Dateien sind gelöscht!:','warn');
                                    }else{
                                        log('Im Verzeichnis: '+notActiveDPs[i].dir +' können folgende Dateien gelöscht werden:','warn');
                                    }
                                    olddir = notActiveDPs[i].dir;
                                }
                                log(notActiveDPs[i].id);
                            }
                        },2000 * (toDir-fromDir)); // file.length
                    }
                });
            }else{
                console.warn('Es wurde keine History-Instanz ausgewählt!')
            }
        }
        
        HomoranH Nicht stören
        HomoranH Nicht stören
        Homoran
        Global Moderator Administrators
        schrieb am zuletzt editiert von
        #143

        @matt77che sagte in ehemalige Historydaten finden:

        einen Fehler an

        und der ist geheim?

        kein Support per PN! - Fragen im Forum stellen -

        Benutzt das Voting rechts unten im Beitrag wenn er euch geholfen hat.

        ioBroker freut sich auch über eine Spende für das Forum. Benutzt dazu den Spendenbutton im Header. Danke!

        der Installationsfixer: curl -fsL https://iobroker.net/fix.sh | bash -

        M 1 Antwort Letzte Antwort
        0
        • HomoranH Homoran

          @matt77che sagte in ehemalige Historydaten finden:

          einen Fehler an

          und der ist geheim?

          M Online
          M Online
          MCU
          schrieb am zuletzt editiert von
          #144

          @homoran Nein, wird nur rot unterschtrichen.

          NUC i7 64GB mit Proxmox ---- Jarvis Infos Aktualisierungen der Doku auf Instagram verfolgen -> mcuiobroker Instagram
          Wenn Euch mein Vorschlag geholfen hat, bitte rechts "^" klicken.

          1 Antwort Letzte Antwort
          1
          • M Matt77CHE

            Im >Script zeigt es mit bei den Zeilen
            148
            214
            218
            238
            einen Fehler an

            // ********************************
            // findOldHistoryDPs v1.0.6
            // Copyright ©MCU
            // ********************************
            // v1.0.1 directDelete-> true  Möglichkeit die DatenDateien direkt löschen
            // v1.0.2 aliasId berücksichtigt bei enabledDPs
            // v1.0.3 zusätzliche Funktion listHistoryDPsWithaliasId('history.0');
            // v1.0.4 zusätzlich: fillAlias -> alle aktiven history-DPs mit alias in Array withAliasIdInEnabled
            //        fillEnabled_DPs -> nur die akitven History DPs in Array füllen -> enabled_DPs
            //        useAliasIdInEnabled -> true -> AliasId wird in enabled_DPs verwendet (was richtig ist, da die history-Werte mit alias geschrieben werden)
            // v1.0.5 zusätzliche Funktion: alias-Suche in allen ioBroker-DPs (unabhängig von history)
            // v1.0.6 error-Handling bei searchDir(),activeHistory, DP-Auswahl History
            
            
            let findOldDP                       = '0_userdata.0.history.findOldDPs';
            let findOldDPs_Old                  = findOldDP + '.old_DPs';
            let findOldDPs_Enabled              = findOldDP + '.enabled_DPs';
            let findOldDPs_InDB                 = findOldDP + '.inDB_DPS';
            let findOldDPs_BeginTS              = findOldDP + '.dirStart';
            let findOldDPs_EndTS                = findOldDP + '.dirEnd';
            let findOldDPs_Start                = findOldDP + '.start';
            let findOldDPs_DirectDel            = findOldDP + '.directDelete';
            let findOldDPs_fillEnabled          = findOldDP + '.fillEnabled_DPs';
            let findOldDPs_fillAlias            = findOldDP + '.fillWithAliasId';
            let findoldDPs_withAliasId          = findOldDP + '.withAliasId_DPs';
            let findOldDPs_useAliasInEnabled    = findOldDP + '.useAliasIdInEnabled';
            let activeHistoryDP                 = findOldDP + '.activeHistory';
            let findOldDPs_aliasDPs             = findOldDP + '.aliasDPs';
            let findOldDPs_aliasDPs_search      = findOldDP + '.aliasDPs_search'; 
            createStateAsync(findOldDPs_aliasDPs, {read: true, write: true, name: "ioBroker-DPs mit aliasId (unabhängig history)", type: "string", role: "text", def: JSON.stringify([]) });
            createStateAsync(findOldDPs_aliasDPs_search, {read: true, write: true, name: "Start Suche aliasIds in all ioBroker DPs", type: "boolean", role: "button", def: false });
            
            let dbNamespace = 'history.0';
            
            
            //createStateAsync(findOldDPs_Old, {read: true, write: true, name: "JSON für nicht genutzte DPs noch in DB", type: "string", role: "text", def: JSON.stringify([]) });
            createStateAsync(findOldDPs_Enabled, {read: true, write: true, name: "Aktive History-DPs in DB", type: "string", role: "text", def: JSON.stringify([]) });
            createStateAsync(findoldDPs_withAliasId, {read: true, write: true, name: "Aktive History-DPs mit aliasId", type: "string", role: "text", def: JSON.stringify([]) });
            //createStateAsync(findOldDPs_InDB, {read: true, write: true, name: "Genutzte DPs in DB", type: "string", role: "text", def: JSON.stringify([]) });
            createStateAsync(findOldDPs_BeginTS, {read: true, write: true, name: "Startverzeichnis", type: "number", role: "", def: 0 });
            createStateAsync(findOldDPs_EndTS, {read: true, write: true, name: "Endverzeichnis", type: "number", role: "", def: 10 }); //new Date().getTime()
            createStateAsync(findOldDPs_Start, {read: true, write: true, name: "Start Suche", type: "boolean", role: "button", def: false });
            createStateAsync(findOldDPs_fillEnabled, {read: true, write: true, name: "enabled_DPs suchen", type: "boolean", role: "button", def: false });
            createStateAsync(findOldDPs_fillAlias, {read: true, write: true, name: "withAliasId suchen", type: "boolean", role: "button", def: false });
            createStateAsync(findOldDPs_DirectDel, {read: true, write: true, name: "!!! Daten direkt löschen !!!", type: "boolean", role: "", def: false });
            createStateAsync(findOldDPs_useAliasInEnabled, {read: true, write: true, name: "AliasId in Enabled_DPs", type: "boolean", role: "", def: false });
            
            
            setTimeout(function(){
                findHistoryInstance()
            },3000)
            
            
            
            on({id: findOldDPs_Start, change: "any"}, function (obj) {
                let value = obj.state.val;
                if (value){
                    //setState(findOldDPs_EndTS,parseInt(new Date().getTime()),false);
                    let dbNamespace = getState(activeHistoryDP).val
                    if(dbNamespace !=''){
                        getEnabledHistoryDPs(dbNamespace);
                        let fromDir = getState(findOldDPs_BeginTS).val;
                        let toDir = getState(findOldDPs_EndTS).val;
                        setTimeout(function(){
                            searchDir(fromDir,toDir);
                        },5000);
                    }else{
                        console.warn('Es wurde keine History-Instanz ausgewählt!')
                    }
                    setStateDelayed(findOldDPs_Start,false,3000,false);
                }
            });
            
            on({id: findOldDPs_fillEnabled, change: "any"}, function (obj) {
                let value = obj.state.val;
                if (value){
                    let dbNamespace = getState(activeHistoryDP).val
                    if(dbNamespace !=''){
                        getEnabledHistoryDPs(dbNamespace);
                    }else{
                        console.warn('Es wurde keine History-Instanz ausgewählt!')
                    }
                    setStateDelayed(findOldDPs_fillEnabled,false,3000,false);
                }
            });
            
            on({id: findOldDPs_fillAlias, change: "any"}, function (obj) {
                let value = obj.state.val;
                if (value){
                    let dbNamespace = getState(activeHistoryDP).val
                    if(dbNamespace !=''){
                        listHistoryDPsWithaliasId(dbNamespace);
                    }else{
                        console.warn('Es wurde keine History-Instanz ausgewählt!')
                    }
                    setStateDelayed(findOldDPs_fillAlias,false,3000,false);
                }
            });
            
            on({id: findOldDPs_aliasDPs_search, change: "any"}, function (obj) {
                let value = obj.state.val;
                if (value){
                    findDPsWithAliasId(); 
                    setStateDelayed(findOldDPs_aliasDPs_search,false,3000,false);
                }
            });
            //listHistoryDPsWithaliasId('history.0');
            
            function listHistoryDPsWithaliasId(adp){
                let arr =[];
                sendTo(adp, 'getEnabledDPs', {}, function (result) {
                    let count = 0;
                    for ( let dp in result){
                        let dpHistoryAlias = getObject(dp).common.custom[adp].aliasId;
                        if (dpHistoryAlias != '' && dpHistoryAlias != undefined){
                            arr.push({'dpId':dp,'aliasId':dpHistoryAlias});
                            count++;
                        }
                    }
                    log('Anzahl der aktiv geloggten DPs mit aliasId: '+count);        
                    // setStateAsync(findOldDPs_Enabled,JSON.stringify(arr),false);
                    //log(JSON.stringify(arr));
                    setState(findoldDPs_withAliasId,JSON.stringify(arr),false);
                });
            }
            
            
            
            
            function findHistoryInstance(){
                let historyDPs = $('system.adapter.history.*.alive')
                let arr = []
                for(let i=0;i< historyDPs.length;i++){
                    let instance = historyDPs[i].split('.')[3]
                    arr.push(instance)
                    //console.log(historyDPs[i])
                }
                console.log('Gefundene History Instanzen: '+arr)
                let obj = {'':'History auswählen'}
                for (let i=0;i<arr.length;i++){
                    obj['history.'+arr[i]] = 'history.'+arr[i]
                }
                // console.log(obj)
                let firstHistory = 'history.'+arr[0]
                
                if(existsState(activeHistoryDP)){
                    let histObj = getObject(activeHistoryDP)
                    histObj.common.states = obj
                    setObject(activeHistoryDP,histObj)
                }else{
                    createStateAsync(activeHistoryDP, {read: true, write: true, name: "Auswahl aktive History", type: "string", role: "", def: firstHistory, states: JSON.stringify(obj) });
                }
            }
            
            //findDPsWithAliasId(); 
            
            function findDPsWithAliasId(){
                let allDPs = $('*');
                let arr = [];
                log('Anzahl der States: '+allDPs.length);
                for (let i=0;i<allDPs.length;i++){
                    if (existsObject(allDPs[i])){
                        if (getObject(allDPs[i]).common != undefined){
                            if (getObject(allDPs[i]).common.alias!= undefined){
                                let aliasId = getObject(allDPs[i]).common.alias.id;
                                if (aliasId != undefined){
                                    //log(allDPs[i] +' mit aliasId: '+aliasId);
                                    //break;
                                    arr.push({'id':allDPs[i],'aliasId':aliasId});
                                }
                            }
                        }
                    }
                }
                //log(arr);
                setState(findOldDPs_aliasDPs,JSON.stringify(arr),false);
            }
            
            
            /*
            setTimeout(function(){
                getEnabledHistoryDPs(db);
                getHistoryDPinDB(db);
            },3000);
            */
            
            // getEnabledHistoryDPs('history.0');
            
            function getEnabledHistoryDPs(adp){
                let arr =[];
                let useAliasIdInenabled = getState(findOldDPs_useAliasInEnabled).val;
                sendTo(adp, 'getEnabledDPs', {}, function (result) {
                    let count = 0;
                    for ( let dp in result){
                        let dpHistoryAlias = getObject(dp).common.custom[adp].aliasId;
                        if (dpHistoryAlias != '' && dpHistoryAlias != undefined && useAliasIdInenabled){
                            arr.push(dpHistoryAlias);
                        }else{
                            arr.push(dp);
                        }
                        count++;
                    }
                    log('Anzahl der aktiv geloggten DPs: '+count);        
                    setStateAsync(findOldDPs_Enabled,JSON.stringify(arr),false);
                });
            }
            
            // getHistoryDPinDB('history.0');
            
            function getHistoryDPinDB(db){
                //Die 50 zuletzt gespeicherte Ereignisse für alle IDs holen:
                let beginTS = getState(findOldDPs_BeginTS).val;
                if (beginTS == 0){
                    beginTS = parseInt(new Date().getTime()) - 30*24*60*60*1000;
                }
                let endTS = getState(findOldDPs_EndTS).val;
                if(endTS == 0){
                    endTS = parseInt(new Date().getTime());
                }
                sendTo(db, 'getHistory', {
                    id: '*',
                    options: {
                        begin: beginTS,
                        end:   endTS,
                        /* count:     50,*/
                        addId:     true
                    }
                }, function (result) {
                    let dpArr =[];
                    let count = 0;
                    for (var i = 0; i < result.result.length; i++) {
                        if (!isObjInArray(dpArr,result.result[i].id)){
                            dpArr.push(result.result[i].id);
                            count++;
                        }
                        //console.log(JSON.stringify(result.result[i]) + ' ' + new Date(result.result[i].ts).toISOString());
                    }
                    dpArr = dpArr.sort(function(a, b) {
                            return a > b;
                            });
                    setStateAsync(findOldDPs_InDB,JSON.stringify(dpArr),false);
                    //log('Anzahl der DPs in dem durchsuchten Bereich:'+count);
                    log(count+' unterschiedliche DPs in "inDB" von '+ formatDate(beginTS,'DD.MM.YYYY hh:mm:ss')+' - '+formatDate(endTS,'DD.MM.YYYY hh:mm:ss'));
                    //Vergleich der DPs
                    let nonActiveArr = [];
                    let activeDPs = JSON.parse(getState(findOldDPs_Enabled).val);
                    for (let i=0; i< dpArr.length;i++){
                        // dpArr[i]
                        let vorhanden = false;
                        for (let  j=0; j< activeDPs.length;j++ ){
                            if (dpArr[i] === activeDPs[j]){
                                vorhanden = true;
                                break; // vorhanden
                            }
                        }
                        if (!vorhanden){
                            // wird nicht mehr geloggt von active
                            nonActiveArr.push(dpArr[i]);
                        }
                    }
                    setState(findOldDPs_Old,JSON.stringify(nonActiveArr),false);
                });
            }
            
            
            
            
            function isObjInArray(arr,itemObj){
                if (arr.length>0){
                    for (let x=0;x<arr.length-1;x++){
                        if (JSON.stringify(arr[x]) === JSON.stringify(itemObj)){
                            return true;
                        }
                    }
                    return false;
                }else{
                    return false;
                }
            }
            
            // searchDir(0,10);
            
            function searchDir(fromDir, toDir){
                let fs = require('fs');
                let activeDPs = JSON.parse(getState(findOldDPs_Enabled).val);
                let notActiveDPs = [];
                let dbNamespace = getState(activeHistoryDP).val
                if(dbNamespace !=''){
                    let sysAdaptDir = getObject('system.adapter.'+dbNamespace).native.storeDir;
                    console.warn('Ablage für History: '+dbNamespace+' im Verzeichnis: '+sysAdaptDir)
                    fs.readdir(sysAdaptDir , function (erro, file) {
                        if (erro) {
                            log('Fehler beim Lesen des Verzeichnisses: ' + erro, 'error');
                            return;
                        }
                        log(file.length+' Verzeichnisse wurde(n) gelesen!');
                        log('fromDir und toDir dürfen nur im Bereich von 0 bis '+file.length +' sein! Bitte in 10er-Schritten verwenden!');
                        log('Es wurde ein Bereich von '+fromDir +' bis '+ toDir+' gewählt-> es dauert '+(toDir-fromDir)*2 +' Sekunden');
                        if (fromDir <0 || toDir >file.length){
            
                        }else{
                            for (let j=fromDir; j<toDir; j++){
                                //log(file[j]);
                                if (file[j].indexOf('.')>=0 && file[j].length >8){
            
                                }else{
                                    fs.readdir(sysAdaptDir+'/'+file[j] , function (error, files) {
                                        log(files.length+' DPs im Verzeichnis: '+file[j]);
                                        for (let i=0;i< files.length; i++){
                                            let dpFiles = files[i].substring(8,files[i].length-5);
                                            if (isObjInArray(activeDPs,dpFiles)){
                                            }else{
                                                notActiveDPs.push({'id':dpFiles,'dir':file[j]});
                                            }
                                        }
                                    });
                                    //fs.unlinkSync('/opt/iobroker/iobroker-data/files/vis.0/'+jarvisDirName+'/'+files[i]+'/'+file[j]);
                                }
                            }
                            setTimeout(function(){
                                log('Nicht aktive DPs in den ausgewählten Verzeichnissen: '+notActiveDPs.length);
                                let olddir= '';
                                let directDelete = getState(findOldDPs_DirectDel).val;
                                for (let i=0;i<notActiveDPs.length;i++){
                                    if (directDelete){
                                        // Direkt löschen
                                        fs.unlinkSync(sysAdaptDir+'/'+notActiveDPs[i].dir+'/history.'+notActiveDPs[i].id+'.json');
                                    }
                                    if (notActiveDPs[i].dir != olddir){
                                        if (directDelete){
                                            log('Im Verzeichnis: '+notActiveDPs[i].dir +' folgende Dateien sind gelöscht!:','warn');
                                        }else{
                                            log('Im Verzeichnis: '+notActiveDPs[i].dir +' können folgende Dateien gelöscht werden:','warn');
                                        }
                                        olddir = notActiveDPs[i].dir;
                                    }
                                    log(notActiveDPs[i].id);
                                }
                            },2000 * (toDir-fromDir)); // file.length
                        }
                    });
                }else{
                    console.warn('Es wurde keine History-Instanz ausgewählt!')
                }
            }
            
            M Online
            M Online
            MCU
            schrieb am zuletzt editiert von
            #145

            @matt77che Versuch mal v1.0.7.
            Anscheinend hast du kein Speicherverzeichnis definiert und somit wurde bislang kein Standardverzeichnis gesetzt.
            Ich hatte bei mir immer ein Speicherverzeichnis gesetzt.
            Wurde mit v1.0.7 behoben.

            NUC i7 64GB mit Proxmox ---- Jarvis Infos Aktualisierungen der Doku auf Instagram verfolgen -> mcuiobroker Instagram
            Wenn Euch mein Vorschlag geholfen hat, bitte rechts "^" klicken.

            1 Antwort Letzte Antwort
            1
            • M Offline
              M Offline
              Matt77CHE
              schrieb am zuletzt editiert von
              #146

              Im Log steht nur

              2024-10-16 18:10:59.887	error	script.js.common.Alte_history_dp_löschen: Fehler beim Lesen des Verzeichnisses: Error: ENOENT: no such file or directory, scandir ''
              

              von history steht nichts

              Screenshot 2024-10-16 180606.jpg

              Screenshot 2024-10-16 180728.jpg

              M 1 Antwort Letzte Antwort
              0
              • M Matt77CHE

                Im Log steht nur

                2024-10-16 18:10:59.887	error	script.js.common.Alte_history_dp_löschen: Fehler beim Lesen des Verzeichnisses: Error: ENOENT: no such file or directory, scandir ''
                

                von history steht nichts

                Screenshot 2024-10-16 180606.jpg

                Screenshot 2024-10-16 180728.jpg

                M Online
                M Online
                MCU
                schrieb am zuletzt editiert von
                #147

                @matt77che Ja wie vermutet, kein Speicherverzeichnis gesetzt. Das sollte jetzt mit v1.0.7 funktionieren.

                NUC i7 64GB mit Proxmox ---- Jarvis Infos Aktualisierungen der Doku auf Instagram verfolgen -> mcuiobroker Instagram
                Wenn Euch mein Vorschlag geholfen hat, bitte rechts "^" klicken.

                M 1 Antwort Letzte Antwort
                0
                • M MCU

                  @matt77che Ja wie vermutet, kein Speicherverzeichnis gesetzt. Das sollte jetzt mit v1.0.7 funktionieren.

                  M Offline
                  M Offline
                  Matt77CHE
                  schrieb am zuletzt editiert von
                  #148

                  @mcu
                  Ja, jetzt läuft es ohne Fehler durch!

                  Vielen Dank! - auch an @Homoran !

                  1 Antwort Letzte Antwort
                  0
                  Antworten
                  • In einem neuen Thema antworten
                  Anmelden zum Antworten
                  • Älteste zuerst
                  • Neuste zuerst
                  • Meiste Stimmen


                  Support us

                  ioBroker
                  Community Adapters
                  Donate
                  FAQ Cloud / IOT
                  HowTo: Node.js-Update
                  HowTo: Backup/Restore
                  Downloads
                  BLOG

                  265

                  Online

                  32.7k

                  Benutzer

                  82.4k

                  Themen

                  1.3m

                  Beiträge
                  Community
                  Impressum | Datenschutz-Bestimmungen | Nutzungsbedingungen | Einwilligungseinstellungen
                  ioBroker Community 2014-2025
                  logo
                  • Anmelden

                  • Du hast noch kein Konto? Registrieren

                  • Anmelden oder registrieren, um zu suchen
                  • Erster Beitrag
                    Letzter Beitrag
                  0
                  • Home
                  • Aktuell
                  • Tags
                  • Ungelesen 0
                  • Kategorien
                  • Unreplied
                  • Beliebt
                  • GitHub
                  • Docu
                  • Hilfe