Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. Skripten / Logik
    4. Gleitender Durchschnitt, Min., Max. über def. Zeitraum

    NEWS

    • ioBroker@Smart Living Forum Solingen, 14.06. - Agenda added

    • ioBroker goes Matter ... Matter Adapter in Stable

    • Monatsrückblick - April 2025

    Gleitender Durchschnitt, Min., Max. über def. Zeitraum

    This topic has been deleted. Only users with topic management privileges can see it.
    • sigi234
      sigi234 Forum Testing Most Active @paul53 last edited by

      @paul53 sagte in Gleitender Durchschnitt, Min., Max. über def. Zeitraum:

      @sigi234 sagte :

      javascript.0.Status.Tabelle

      Funktioniert es, so dass Du den Datenpunkt "javascript.0.Status.Tabelle" in Vis verwenden kannst ?

      Ja, Danke einfach perfekt..........👍

      Screenshot (3657).png

      1 Reply Last reply Reply Quote 0
      • K
        klassisch Most Active last edited by

        Das sieht ja super aus!
        Fehlt nur noch die Standardabweichung.
        Mein Geigerzähler hat seit Oktober 2017 schon 4 Fehlalarme produziert, weil ich wohl die Grenzen etwas zu stramm habe. Da hülfe wohl die Kenntnis der Standardabweichung.

        1 Reply Last reply Reply Quote 0
        • K
          kai_enet last edited by

          Hallo zusammen,

          sorry für Anfängerfragen - aber habe erst diese Woche mit iobroker angefangen:

          Hat schon jemand den export von history Daten benutzt (https://github.com/ioBroker/ioBroker.history/blob/master/docs/de/README.md - "Zugriff auf History Werte mit JavaScript") um damit für Sensoren eine Gleitmittelung bzw min max der letzten x Werte durchzuführen ?

          Wäre für ein Beispiel dankbar...

          paul53 1 Reply Last reply Reply Quote 0
          • paul53
            paul53 @kai_enet last edited by

            @kai_enet sagte:

            Wäre für ein Beispiel dankbar

            Mit 'history.0' anstelle 'sql.0'

            1 Reply Last reply Reply Quote 0
            • M
              martin last edited by martin

              Ich habe das Skript mit meinen Xiaomi Sensoren unter "const idData" gefüttert und ansonsten nichts verändert.

              `// ########################################################################################################
              // Berechnung von gleitendem Durchschnitt, Minimum und Maximum über einen Zeitraum
              // Version 1.4.1
              // ########################################################################################################
              "use strict";
              //
              //
              // Berechnet Min/Max, Durschschnitt und Median über die letzten 24h.
              // Berechnet Min/Max, Durschschnitt über die letzte 1h.
              //
              // IDs aller benötigten Datenpunkte
              //
              // Als Array im Array mit folgendem Format:
              //  1\.   original Datenpunktname
              //  2\.  neuer Datenpunktname
              //         Beispiel:   javascript.0.Status.Temperatur.Außen
              //                  javascript.0.Status.Luftfeuchtigkeit.Außen
              //  3\.   Beschreibung des Messwertes (zur Erzeugung neue Datenpunkte)
              //         Beispliele:   Temperatur
              //                  Luftfeuchtigkeit
              //   4\.  Einheit (zur Erzeugung neue Datenpunkte)
              //         Beispiele:   °C, %
              //
              // Ist beliebig erweiterbar und für beliebige Werte nutzbar.
              // Beispiel 1:
              // const idData = [['hm-rpc.3.CUX3200312.1.TEMPERATURE','javascript.0.Status.Temperatur.Außen','Temperatur','°C'],
              //                 ['hm-rpc.3.CUX9002580.1.HUMIDITY'   ,'javascript.0.Status.Luftfeuchtigkeit.Außen','Luftfeuchtigkeit','%']];
              //
              // Beispiel 2:
              // const idData = [['hm-rpc.3.CUX3200312.1.TEMPERATURE','javascript.0.Status.Außen.Temperatur','Temperatur','°C'],
              //                 ['hm-rpc.3.CUX9002580.1.HUMIDITY'   ,'javascript.0.Status.Außen.Luftfeuchtigkeit','Luftfeuchtigkeit','%'],
              //                 ['hm-rpc.3.CUX4007637.1.Data'   ,'javascript.0.Status.Außen.Lichtstärke','Lichtstärke','lux']];
              //
              const idData = [['mihome.0.devices.weather_v1_158d0002c8f3ab.temperature','javascript.0.Status.Temperatur.Balkon','Temperatur','°C'],
                              ['mihome.0.devices.weather_v1_158d0002c8f3ab.humidity','javascript.0.Status.Luftfeuchtigkeit.Balkon','Luftfeuchtigkeit','%'];
                             
               
              //Datenpunkt zur Speicherung aller internen Daten
              const dpData='javascript.0.Status.Statistic';
               
              // ########################################################################################################
              // Implementierung -- hier nichts mehr ändern
              // ########################################################################################################
               
              // globale Konstanten
              const tc = 10;         // Abtastrate in Minuten
               
              const statDataLength24 = Math.round((24 * 60) / tc); // Anzahl der Werte für 24h
              const statDataLength1 = Math.round(60 / tc); // Anzahl der Werte für stündlich
               
              // globale Variablen
              var listStatData;  //interne Speicherung aller Werte
               
              //Funktion zum einmaligem initialisieren aller Datenpunkte
              function initializeStatData() {
               
                 // Datenpunkt zur Speicherung der internen Werte erzeugen
                 createState(dpData, 0, false, {
                    name: "StatisticData",
                    read: true,
                    write: true,
                    desc: 'Statistische Daten',
                    type: 'string',
                    def: '',
                    role: 'json'        
                 });
               
                 //internes Array initialisieren
                  var needInit = false;
                 try {
                    listStatData = JSON.parse(getState(dpData).val);
                 } catch (ex) {
                    needInit = true;
                 }
                 if (needInit || !listStatData || (listStatData.length < idData.length)) {
                    listStatData = new Array(idData.length); 
                 }
                 //logDebug('initializeStatData for', dpData, listStatData);
               
                 for (var i = 0; i < idData.length; i++) {
                      if (!listStatData[i]) { listStatData[i] = {}; }
                      listStatData[i].value       = idData[i][1];
                      listStatData[i].max24h		= idData[i][1] + '.Max_24h';
                      listStatData[i].min24h		= idData[i][1] + '.Min_24h';
                      listStatData[i].mean24h		= idData[i][1] + '.Mean_24h';
                      listStatData[i].median24h	= idData[i][1] + '.Median_24h';
              	listStatData[i].max1h		= idData[i][1] + '.Max_1h';
              	listStatData[i].min1h		= idData[i][1] + '.Min_1h';
              	listStatData[i].mean1h		= idData[i][1] + '.Mean_1h';
               
              	createState(listStatData[i].value, 0, false, {
                          name: idData[i][2],
                          read: true,
              	        write: true,
                          desc: idData[i][2]+ ' Aktueller Wert',
                    	    type: 'number',
                          def: 0,
                          unit: idData[i][3],
                          role: 'value'
              	});
               
              	createState(listStatData[i].max24h, 0, false, {
                          name: 'Maximum_24h',
                          read: true,
                          write: true,
                          desc: idData[i][2] + ' Maximum',
                          type: 'number',
                          def: 0,
                          unit: idData[i][3],
                          role: 'value'
                     });
               
                     createState(listStatData[i].min24h, 0, false, {
                          name: 'Minimum_24h',
                          read: true,
                          write: true,
                          desc: idData[i][2] + ' Minimum',
                          type: 'number',
                          def: 0,
              	    unit: idData[i][3],
                          role: 'value'
                     });
               
                     createState(listStatData[i].mean24h, 0, false, {
                          name: 'Mittelwert_24h',
                          read: true,
                          write: true,
                          desc: idData[i][2] + ' Mittelwert',
                          type: 'number',
                          def: 0,
                          unit: idData[i][3],
                          role: 'value'
                    });
               
                    createState(listStatData[i].median24h, 0, false, {
                          name: 'Median_24h',
                          read: true,
                          write: true,
                          desc: idData[i][2] + ' Median',
                          type: 'number',
                          def: 0,
              	        unit: idData[i][3],
                          role: 'value'
                    });
               
                    createState(listStatData[i].max1h, 0, false, {
                          name: 'Maximum_1h',
                          read: true,
                          write: true,
                          desc: idData[i][2] + ' Maximum',
                          type: 'number',
                          def: 0,
                          unit: idData[i][3],
                          role: 'value'
                    });
               
                    createState(listStatData[i].min1h, 0, false, {
                          name: 'Minimum_1h',
                          read: true,
                          write: true,
                          desc: idData[i][2] + ' Minimum',
                          type: 'number',
                          def: 0,
                          unit: idData[i][3],
                          role: 'value'
                    });
               
                    createState(listStatData[i].mean1h, 0, false, {
                          name: 'Mittelwert_1h',
                          read: true,
                          write: true,
                     	    desc: idData[i][2] + ' Mittelwert',
                          type: 'number',
                          def: 0,
              	    unit: idData[i][3],
              	    role: 'value'
                    });
               
                    if (needInit || !listStatData[i].data || (listStatData[i].data.length != statDataLength24)) {
                          listStatData[i].data	= new Array(statDataLength24);
               
                          // 1\. Script start: Liste und String-Datenpunkt füllen
                          var x = getState(idData[i][0]).val;
                          for (var j = 0; j < statDataLength24; j++) {
                          listStatData[i].data[j] = x;
                       }
               
                        //logDebug(listStatData[i], i);
                          setStateDelayed(listStatData[i].value, x, false, 1000);
                          setStateDelayed(listStatData[i].min24h, x, false, 1000);
                          setStateDelayed(listStatData[i].max24h, x, false, 1000);
                          setStateDelayed(listStatData[i].mean24h, x, false, 1000);
                          setStateDelayed(listStatData[i].median24h, x, false, 1000);
                          setStateDelayed(listStatData[i].min1h, x, false, 1000);
                          setStateDelayed(listStatData[i].max1h, x, false, 1000);
                          setStateDelayed(listStatData[i].mean1h, x, false, 1000);
                    }      
                 }
                 setState(dpData, JSON.stringify(listStatData));         
              }
               
              //Berechnung der Werte
              function calcStatData() {
                 if (!listStatData || (idData.length != listStatData.length)) {
                      initializeStatData();
                 }
                  //logDebug('starting calcStatData');
               
                 for (var i = 0; i < idData.length; i++) {
               
                    listStatData[i].data.pop(); //Remove the last element of an array
                    var x = parseFloat(getState(idData[i][0]).val);
                    listStatData[i].data.unshift(x); //Add new items to the beginning of an array
                    setState(listStatData[i].value, x);
               
                    var min = x;
                    var max = x;
                    var sum = 0.0;
                    for (var j = 0; j < statDataLength24; j++) {
                       var s = parseFloat(listStatData[i].data[j]);
                       if (s < min) min = s;
                       if (s > max) max = s;
                       sum += s;
                       if (j == (statDataLength1-1)) {
                          setState(listStatData[i].min1h, min);
                          setState(listStatData[i].max1h, max);
                          setState(listStatData[i].mean1h, round(sum / statDataLength1, 2));     
                       }
                    }
                    setState(listStatData[i].min24h, min);
                    setState(listStatData[i].max24h, max);
                    setState(listStatData[i].mean24h, round(sum / statDataLength24, 2));   
                    setState(listStatData[i].median24h, getMedian(listStatData[i].data));
                 }
                 setState(dpData, JSON.stringify(listStatData));
              }    
               
              function getMedian(args) {
                if (!args.length) {return 0}
                var numbers = args.slice(0).sort((a,b) => a - b);
                var middle = Math.floor(numbers.length / 2);
                var isEven = numbers.length % 2 === 0;
                return isEven ? (numbers[middle] + numbers[middle - 1]) / 2 : numbers[middle];
              }
               
              /**
               * round a number
               * @param    value  to round
               * @param    exp    exponent to round
               * @returns         the round number
               */
              function round(value, exp) {
                if (typeof exp === 'undefined' || +exp === 0)
                  return Math.round(value);
               
                value = +value;
                exp = +exp;
               
                if (isNaN(value) || !(typeof exp === 'number' && exp % 1 === 0))
                  return NaN;
               
                // Shift
                var values = value.toString().split('e');
                value = Math.round(+(values[0] + 'e' + (values[1] ? (+values[1] + exp) : exp)));
               
                // Shift back
                var values = value.toString().split('e');
                return +(values[0] + 'e' + (values[1] ? (+values[1] - exp) : -exp));
              }
               
              initializeStatData();
               
              schedule('*/' + tc + ' * * * *', function () {
                 calcStatData();
              });`
              

              Es werden aber keine Datenpunkte erzeugt.

              Im Log finde ich nur:

              javascript.0	2020-09-30 18:14:21.682	info	(16357) script.js.Meine_Scripts.Temperatur_Min_Max: registered 0 subscriptions and 0 schedules
              javascript.0	2020-09-30 18:14:21.671	info	(16357) Start javascript script.js.Meine_Scripts.Temperatur_Min_Max
              
              paul53 1 Reply Last reply Reply Quote 0
              • paul53
                paul53 @martin last edited by

                @martin sagte:

                and 0 schedules

                Ganz zu Beginn und am Ende sind Zeichen `, die da nicht hin gehören.

                M 1 Reply Last reply Reply Quote 0
                • M
                  martin @paul53 last edited by martin

                  @paul53 said in Gleitender Durchschnitt, Min., Max. über def. Zeitraum:

                  d am Ende sind Zeich

                  Danke!

                  Jetzt kommt eine ganze Reihe an Warnungen mit denen ich nichts anfangen kann:

                  javascript.0	2020-09-30 18:55:11.308	info	(16357) script.js.Meine_Scripts.Temperatur_Min_Max: registered 0 subscriptions and 1 schedule
                  javascript.0	2020-09-30 18:55:11.300	warn	(16357) at script.js.Meine_Scripts.Temperatur_Min_Max:271:1
                  javascript.0	2020-09-30 18:55:11.299	warn	(16357) at initializeStatData (script.js.Meine_Scripts.Temperatur_Min_Max:199:4)
                  javascript.0	2020-09-30 18:55:11.299	warn	(16357) at setState (/opt/iobroker/node_modules/iobroker.javascript/lib/sandbox.js:1358:20)
                  javascript.0	2020-09-30 18:55:11.296	warn	(16357) State "javascript.0.Status.Statistic" not found
                  javascript.0	2020-09-30 18:55:11.290	warn	(16357) at script.js.Meine_Scripts.Temperatur_Min_Max:271:1
                  javascript.0	2020-09-30 18:55:11.289	warn	(16357) at initializeStatData (script.js.Meine_Scripts.Temperatur_Min_Max:71:33)
                  javascript.0	2020-09-30 18:55:11.285	warn	(16357) getState "javascript.0.Status.Statistic" not found (3)
                  javascript.0	2020-09-30 18:55:11.242	info	(16357) Start javascript script.js.Meine_Scripts.Temperatur_Min_Max
                  javascript.0	2020-09-30 18:55:11.078	info	(16357) Stop script script.js.Meine_Scripts.Temperatur_Min_Max
                  
                  paul53 2 Replies Last reply Reply Quote 0
                  • paul53
                    paul53 @martin last edited by

                    @martin
                    In Zeile 35 fehlt am Ende (vor dem Semikolon) eine schließende eckige Klammer.

                    M 1 Reply Last reply Reply Quote 0
                    • M
                      martin @paul53 last edited by

                      @paul53 said in Gleitender Durchschnitt, Min., Max. über def. Zeitraum:

                      @martin
                      In Zeile 35 fehlt am Ende (vor dem Semikolon) eine schließende eckige Klammer.

                      Das habe ich auch gerade gefunden. 🙂

                      Jetzt kommen die Warnungen (siehe oben - habe den Beitrag bearbeitet).

                      1 Reply Last reply Reply Quote 0
                      • paul53
                        paul53 @martin last edited by

                        @martin sagte:

                        State "javascript.0.Status.Statistic" not found

                        Der Datenpunkt existiert nicht.

                        M 1 Reply Last reply Reply Quote 0
                        • M
                          martin @paul53 last edited by

                          @paul53
                          Oha. Ich dachte den legt er selbst an.
                          Wie muss der denn aussehen? Zahl oder sonst was?

                          paul53 2 Replies Last reply Reply Quote 0
                          • paul53
                            paul53 @martin last edited by

                            @martin sagte:

                            Ich dachte den legt er selbst an.

                            Laut Script wird dieser DP zuerst erzeugt. Schau mal im Tab "Objekte" nach.

                            M 1 Reply Last reply Reply Quote 0
                            • M
                              martin @paul53 last edited by

                              @paul53 Nein, da ist er nicht vorhanden.

                              M 1 Reply Last reply Reply Quote 0
                              • paul53
                                paul53 @martin last edited by

                                @martin sagte:

                                Wie muss der denn aussehen?

                                Siehe Zeilen 58 bis 66.

                                1 Reply Last reply Reply Quote 0
                                • M
                                  martin @martin last edited by

                                  @martin Kommando zurück. Habe das Skript nochmal neu gestartet. Jetzt ist der Datenpunkt da.

                                  M 1 Reply Last reply Reply Quote 0
                                  • M
                                    martin @martin last edited by martin

                                    Mittlerweile sind alle DP da.
                                    Danke für deine Hilfe!

                                    paul53 1 Reply Last reply Reply Quote 0
                                    • paul53
                                      paul53 @martin last edited by

                                      @martin sagte:

                                      als Zahl darstellen?

                                      Das sind Zahlen. Mit Vis kenne ich mich nicht wirklich aus.

                                      1 Reply Last reply Reply Quote 0
                                      • S
                                        SiKo last edited by

                                        Hallo zusammen,

                                        ich habe jetzt hier meinen "Quelldatenpunk" vom Netatmo Crawler eingetragen und als Ziel für die neuen DP 0_Userdata.0.Wetter.
                                        Es wird jedoch folgendes erstellt: Hat Jemand einen Tipp für mich.

                                        808a1677-33f0-4cb1-897b-686459d58c81-image.png

                                        d6dfb2af-a614-4754-98b5-ff2bf49a4577-image.png

                                        // Berechnung von gleitendem Durchschnitt, Minimum und Maximum über einen Zeitraum
                                        // Version 1.4.1
                                        // ########################################################################################################
                                        "use strict";
                                        //
                                        //
                                        // Berechnet Min/Max, Durschschnitt und Median über die letzten 24h.
                                        // Berechnet Min/Max, Durschschnitt über die letzte 1h.
                                        //
                                        // IDs aller benötigten Datenpunkte
                                        //
                                        // Als Array im Array mit folgendem Format:
                                        //  1\.   original Datenpunktname
                                        //  2\.  neuer Datenpunktname
                                        //         Beispiel:   javascript.0.Status.Temperatur.Außen
                                        //                  javascript.0.Status.Luftfeuchtigkeit.Außen
                                        //  3\.   Beschreibung des Messwertes (zur Erzeugung neue Datenpunkte)
                                        //         Beispliele:   Temperatur
                                        //                  Luftfeuchtigkeit
                                        //   4\.  Einheit (zur Erzeugung neue Datenpunkte)
                                        //         Beispiele:   °C, %
                                        //
                                        // Ist beliebig erweiterbar und für beliebige Werte nutzbar.
                                        // Beispiel 1:
                                        // const idData = [['hm-rpc.3.CUX3200312.1.TEMPERATURE','javascript.0.Status.Temperatur.Außen','Temperatur','°C'],
                                        //                 ['hm-rpc.3.CUX9002580.1.HUMIDITY'   ,'javascript.0.Status.Luftfeuchtigkeit.Außen','Luftfeuchtigkeit','%']];
                                        //
                                        // Beispiel 2:
                                        // const idData = [['hm-rpc.3.CUX3200312.1.TEMPERATURE','javascript.0.Status.Außen.Temperatur','Temperatur','°C'],
                                        //                 ['hm-rpc.3.CUX9002580.1.HUMIDITY'   ,'javascript.0.Status.Außen.Luftfeuchtigkeit','Luftfeuchtigkeit','%'],
                                        //                 ['hm-rpc.3.CUX4007637.1.Data'   ,'javascript.0.Status.Außen.Lichtstärke','Lichtstärke','lux']];
                                        //
                                        const idData = ['netatmo-crawler.0.stationData.70:ee:50:15:e2:1e.temperature','0_userdata.0.Wetter.Temperatur.Netatmo','Temperatur','°C'];
                                                       
                                         
                                        //Datenpunkt zur Speicherung aller internen Daten
                                        const dpData='0_userdata.0.Wetter';
                                         
                                        // ########################################################################################################
                                        // Implementierung -- hier nichts mehr ändern
                                        // ########################################################################################################
                                         
                                        // globale Konstanten
                                        const tc = 10;         // Abtastrate in Minuten
                                         
                                        const statDataLength24 = Math.round((24 * 60) / tc); // Anzahl der Werte für 24h
                                        const statDataLength1 = Math.round(60 / tc); // Anzahl der Werte für stündlich
                                         
                                        // globale Variablen
                                        var listStatData;  //interne Speicherung aller Werte
                                         
                                        //Funktion zum einmaligem initialisieren aller Datenpunkte
                                        function initializeStatData() {
                                         
                                           // Datenpunkt zur Speicherung der internen Werte erzeugen
                                           createState(dpData, 0, false, {
                                              name: "StatisticData",
                                              read: true,
                                              write: true,
                                              desc: 'Statistische Daten',
                                              type: 'string',
                                              def: '',
                                              role: 'json'        
                                           });
                                         
                                           //internes Array initialisieren
                                            var needInit = false;
                                           try {
                                              listStatData = JSON.parse(getState(dpData).val);
                                           } catch (ex) {
                                              needInit = true;
                                           }
                                           if (needInit || !listStatData || (listStatData.length < idData.length)) {
                                              listStatData = new Array(idData.length); 
                                           }
                                           //logDebug('initializeStatData for', dpData, listStatData);
                                         
                                           for (var i = 0; i < idData.length; i++) {
                                                if (!listStatData[i]) { listStatData[i] = {}; }
                                                listStatData[i].value       = idData[i][1];
                                                listStatData[i].max24h		= idData[i][1] + '.Max_24h';
                                                listStatData[i].min24h		= idData[i][1] + '.Min_24h';
                                                listStatData[i].mean24h		= idData[i][1] + '.Mean_24h';
                                                listStatData[i].median24h	= idData[i][1] + '.Median_24h';
                                        	listStatData[i].max1h		= idData[i][1] + '.Max_1h';
                                        	listStatData[i].min1h		= idData[i][1] + '.Min_1h';
                                        	listStatData[i].mean1h		= idData[i][1] + '.Mean_1h';
                                         
                                        	createState(listStatData[i].value, 0, false, {
                                                    name: idData[i][2],
                                                    read: true,
                                        	        write: true,
                                                    desc: idData[i][2]+ ' Aktueller Wert',
                                              	    type: 'number',
                                                    def: 0,
                                                    unit: idData[i][3],
                                                    role: 'value'
                                        	});
                                         
                                        	createState(listStatData[i].max24h, 0, false, {
                                                    name: 'Maximum_24h',
                                                    read: true,
                                                    write: true,
                                                    desc: idData[i][2] + ' Maximum',
                                                    type: 'number',
                                                    def: 0,
                                                    unit: idData[i][3],
                                                    role: 'value'
                                               });
                                         
                                               createState(listStatData[i].min24h, 0, false, {
                                                    name: 'Minimum_24h',
                                                    read: true,
                                                    write: true,
                                                    desc: idData[i][2] + ' Minimum',
                                                    type: 'number',
                                                    def: 0,
                                        	    unit: idData[i][3],
                                                    role: 'value'
                                               });
                                         
                                               createState(listStatData[i].mean24h, 0, false, {
                                                    name: 'Mittelwert_24h',
                                                    read: true,
                                                    write: true,
                                                    desc: idData[i][2] + ' Mittelwert',
                                                    type: 'number',
                                                    def: 0,
                                                    unit: idData[i][3],
                                                    role: 'value'
                                              });
                                         
                                              createState(listStatData[i].median24h, 0, false, {
                                                    name: 'Median_24h',
                                                    read: true,
                                                    write: true,
                                                    desc: idData[i][2] + ' Median',
                                                    type: 'number',
                                                    def: 0,
                                        	        unit: idData[i][3],
                                                    role: 'value'
                                              });
                                         
                                              createState(listStatData[i].max1h, 0, false, {
                                                    name: 'Maximum_1h',
                                                    read: true,
                                                    write: true,
                                                    desc: idData[i][2] + ' Maximum',
                                                    type: 'number',
                                                    def: 0,
                                                    unit: idData[i][3],
                                                    role: 'value'
                                              });
                                         
                                              createState(listStatData[i].min1h, 0, false, {
                                                    name: 'Minimum_1h',
                                                    read: true,
                                                    write: true,
                                                    desc: idData[i][2] + ' Minimum',
                                                    type: 'number',
                                                    def: 0,
                                                    unit: idData[i][3],
                                                    role: 'value'
                                              });
                                         
                                              createState(listStatData[i].mean1h, 0, false, {
                                                    name: 'Mittelwert_1h',
                                                    read: true,
                                                    write: true,
                                               	    desc: idData[i][2] + ' Mittelwert',
                                                    type: 'number',
                                                    def: 0,
                                        	    unit: idData[i][3],
                                        	    role: 'value'
                                              });
                                         
                                              if (needInit || !listStatData[i].data || (listStatData[i].data.length != statDataLength24)) {
                                                    listStatData[i].data	= new Array(statDataLength24);
                                         
                                                    // 1\. Script start: Liste und String-Datenpunkt füllen
                                                    var x = getState(idData[i][0]).val;
                                                    for (var j = 0; j < statDataLength24; j++) {
                                                    listStatData[i].data[j] = x;
                                                 }
                                         
                                                  //logDebug(listStatData[i], i);
                                                    setStateDelayed(listStatData[i].value, x, false, 1000);
                                                    setStateDelayed(listStatData[i].min24h, x, false, 1000);
                                                    setStateDelayed(listStatData[i].max24h, x, false, 1000);
                                                    setStateDelayed(listStatData[i].mean24h, x, false, 1000);
                                                    setStateDelayed(listStatData[i].median24h, x, false, 1000);
                                                    setStateDelayed(listStatData[i].min1h, x, false, 1000);
                                                    setStateDelayed(listStatData[i].max1h, x, false, 1000);
                                                    setStateDelayed(listStatData[i].mean1h, x, false, 1000);
                                              }      
                                           }
                                           setState(dpData, JSON.stringify(listStatData));         
                                        }
                                         
                                        //Berechnung der Werte
                                        function calcStatData() {
                                           if (!listStatData || (idData.length != listStatData.length)) {
                                                initializeStatData();
                                           }
                                            //logDebug('starting calcStatData');
                                         
                                           for (var i = 0; i < idData.length; i++) {
                                         
                                              listStatData[i].data.pop(); //Remove the last element of an array
                                              var x = parseFloat(getState(idData[i][0]).val);
                                              listStatData[i].data.unshift(x); //Add new items to the beginning of an array
                                              setState(listStatData[i].value, x);
                                         
                                              var min = x;
                                              var max = x;
                                              var sum = 0.0;
                                              for (var j = 0; j < statDataLength24; j++) {
                                                 var s = parseFloat(listStatData[i].data[j]);
                                                 if (s < min) min = s;
                                                 if (s > max) max = s;
                                                 sum += s;
                                                 if (j == (statDataLength1-1)) {
                                                    setState(listStatData[i].min1h, min);
                                                    setState(listStatData[i].max1h, max);
                                                    setState(listStatData[i].mean1h, round(sum / statDataLength1, 2));     
                                                 }
                                              }
                                              setState(listStatData[i].min24h, min);
                                              setState(listStatData[i].max24h, max);
                                              setState(listStatData[i].mean24h, round(sum / statDataLength24, 2));   
                                              setState(listStatData[i].median24h, getMedian(listStatData[i].data));
                                           }
                                           setState(dpData, JSON.stringify(listStatData));
                                        }    
                                         
                                        function getMedian(args) {
                                          if (!args.length) {return 0}
                                          var numbers = args.slice(0).sort((a,b) => a - b);
                                          var middle = Math.floor(numbers.length / 2);
                                          var isEven = numbers.length % 2 === 0;
                                          return isEven ? (numbers[middle] + numbers[middle - 1]) / 2 : numbers[middle];
                                        }
                                         
                                        /**
                                         * round a number
                                         * @param    value  to round
                                         * @param    exp    exponent to round
                                         * @returns         the round number
                                         */
                                        function round(value, exp) {
                                          if (typeof exp === 'undefined' || +exp === 0)
                                            return Math.round(value);
                                         
                                          value = +value;
                                          exp = +exp;
                                         
                                          if (isNaN(value) || !(typeof exp === 'number' && exp % 1 === 0))
                                            return NaN;
                                         
                                          // Shift
                                          var values = value.toString().split('e');
                                          value = Math.round(+(values[0] + 'e' + (values[1] ? (+values[1] + exp) : exp)));
                                         
                                          // Shift back
                                          var values = value.toString().split('e');
                                          return +(values[0] + 'e' + (values[1] ? (+values[1] - exp) : -exp));
                                        }
                                         
                                        initializeStatData();
                                         
                                        schedule('*/' + tc + ' * * * *', function () {
                                           calcStatData();
                                        });
                                        
                                        1 Reply Last reply Reply Quote 0
                                        • S
                                          Shigoru @paul53 last edited by Shigoru

                                          @paul53

                                          Hallöchen, Danke für die Infos hab das Script integriert und die Darstellung im Flot umgesetzt. Jetzt hänge ich noch bei einem Thema.

                                          daten.jpg

                                          Grün ist die Außentemperatur und gelb der 24h Durschnitt. (Nicht auf die Werte schauen logging läuft erst ganz kurz.)

                                          Wie bekomme ich die Y Achse gleich für bei Daten ohne das ich einen MIN MAX Wert eingebe für die Eingangsdaten. Diese würde ich gerne bei Art auf minmax Auswahl lassen.

                                          Du hast das geschafft. Kannst du mir kurz sagen wo das im Flot ist?

                                          Bild.jpg

                                          Ich habs auch über die Setting mit Gemeinsame Y Achse versucht aber nicht hinbekommen.

                                          Ein Tip wäre hilfreich.

                                          Danke Ciao Shigoru

                                          paul53 1 Reply Last reply Reply Quote 0
                                          • paul53
                                            paul53 @Shigoru last edited by

                                            @shigoru sagte: Wie bekomme ich die Y Achse gleich für bei Daten ohne das ich einen MIN MAX Wert eingebe für die Eingangsdaten. Diese würde ich gerne bei Art auf minmax Auswahl lassen.

                                            Die Angabe von MIN und MAX ist für die Skalierung erforderlich und hat nichts mit der Art zu tun.

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

                                            Support us

                                            ioBroker
                                            Community Adapters
                                            Donate

                                            537
                                            Online

                                            31.7k
                                            Users

                                            79.8k
                                            Topics

                                            1.3m
                                            Posts

                                            javascript
                                            20
                                            113
                                            21819
                                            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