Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. Skripten / Logik
    4. JavaScript
    5. [Vorlage] Hilfreiche JavaScript-Funktionen

    NEWS

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

    • ioBroker goes Matter ... Matter Adapter in Stable

    • Monatsrückblick - April 2025

    [Vorlage] Hilfreiche JavaScript-Funktionen

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

      So, habe noch ein paar Funktionen ergänzt, außerdem alle in Spoiler gepackt, somit sieht das schon mal deutlich übersichtlicher aus.
      Ich werde weiter ergänzen.. Ich hoffe, die Forumsoftware macht mir keinen Strich durch die Rechnung wegen zu langem Text im Beitrag 🙂 , aber dann teile ich auf oder lagere auf Github aus.

      Ich ergänze gerne auch hilfreiche Funktionen von Euch, bitte einfach hier posten. Diese sollten im ioBroker getestet sein und laufen, und wiederkehrend verwendbar sein, also nicht zugeschnitten auf einen bestimmten Adapter oder zu speziell.

      Als nächstes könnte man dann Functions vereinfachen/zusammenfassen, gerade unter "Datum/Zeit" finden sich da einige Kandidaten, die sehr ähnlich sind. Aber das ist auch der Vorteil hier beim sammeln, dann sieht man das gleich.

      1 Reply Last reply Reply Quote 0
      • Mic
        Mic Developer last edited by Mic

        paar Sachen ergänzt...

        1 Reply Last reply Reply Quote 0
        • Mic
          Mic Developer last edited by

          ein paar Funktionen nachgetragen...

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

            Vielen Dank für deine Arbeit!

            1 Reply Last reply Reply Quote 1
            • M
              Marty56 last edited by

              Große Klasse. Danke dafür. Sofort einen Bookmark gemacht!!

              1 Reply Last reply Reply Quote 1
              • L
                leuchtturm last edited by leuchtturm

                Auch von mir ein herzliches Danke.
                War auf der Suche nach einer Funktion add Day to Date.
                Und schwupps schon gefunden 😎

                1 Reply Last reply Reply Quote 1
                • Mic
                  Mic Developer last edited by

                  ein paar Funktionen ergänzt...

                  1 Reply Last reply Reply Quote 0
                  • Mic
                    Mic Developer last edited by

                    stateLastUpdated() ergänzt.

                    1 Reply Last reply Reply Quote 0
                    • Mic
                      Mic Developer last edited by

                      isState() durch existsState() ersetzt:
                      Hinweis (07.01.2019): Bislang gab es hier eine Function isState(), diese ist nun hinfällig, weil es mit existsState() nun (mind. ab JS-Adapter-Version 4.3.4, gg. früher schon) eine neue ioBroker-Funktion gibt, um zu prüfen, ob ein State (Datenpunkt) existiert. Beispiel:

                      1 Reply Last reply Reply Quote 0
                      • Mic
                        Mic Developer last edited by

                        createUserStates() ergänzt:

                        Mit diesem Script bzw. dieser Funktion können States (Datenpunkte) unter 0_userdata.0 oder unter javascript.x angelegt werden. Dabei können mehrere States gleichzeitig angelegt werden. Sobald alle erfolgreich angelegt wurden, kann danach (per callback) ein beliebiger Code ausgeführt werden, also beispielsweise die Haupt-Funktion des Scripts.

                        1 Reply Last reply Reply Quote 0
                        • Mic
                          Mic Developer last edited by

                          isTimeInAstro() und fileExistsSync() ergänzt.

                          1 Reply Last reply Reply Quote 0
                          • Mic
                            Mic Developer last edited by

                            Notiz an mich selbst:
                            json2table() hinzufügen, also Umwandlung JSON in HTML-Tabelle.
                            https://forum.iobroker.net/topic/32540/json-zu-html-und-in-datei-schreiben-ablegen

                            1 Reply Last reply Reply Quote 0
                            • Matthias Stübner
                              Matthias Stübner last edited by

                              Ich möchte dateToString(), selbiges wird aber als unbekannt markiert. Muss ich, und wenn ja was, irgendein zusätzliches Modul/Adapter laden?

                              Mic htrecksler 2 Replies Last reply Reply Quote 0
                              • Mic
                                Mic Developer @Matthias Stübner last edited by Mic

                                @Matthias-Stübner
                                Was meinst du genau mit "als unbekannt markiert"? Am besten bitte Logausgabe hier posten.
                                Du brauchst keine weiteren Adapter ö.ä. hierfür.

                                1 Reply Last reply Reply Quote 0
                                • htrecksler
                                  htrecksler Forum Testing @Matthias Stübner last edited by

                                  @Matthias-Stübner das im Editor unterstrichen wird, kannst DU an der Stelle ignorieren.
                                  Es sollte dennoch wie erwartet funktionieren.

                                  moelski 1 Reply Last reply Reply Quote 0
                                  • moelski
                                    moelski @htrecksler last edited by

                                    Danke für die Funktionen !

                                    1 Reply Last reply Reply Quote 0
                                    • AlCalzone
                                      AlCalzone Developer last edited by

                                      clearStr stimmt nicht mit seiner Beschreibung überein:

                                      > String(null)
                                      'null'
                                      
                                      Mic 1 Reply Last reply Reply Quote 0
                                      • Mic
                                        Mic Developer @AlCalzone last edited by Mic

                                        @AlCalzone
                                        Danke für den Hinweis, hab die Funktion jetzt ersatzlos entfernt 😀
                                        Ist so "generisch" eher nicht zu gebrauchen, weil z.B. andere Datentypen (wie number) auch nicht behandelt werden und je nach Einsatzzweck was unterschiedliches zu prüfen ist.

                                        Christoph1337 1 Reply Last reply Reply Quote 0
                                        • Christoph1337
                                          Christoph1337 @Mic last edited by

                                          Moin zusammen,

                                          ich habe mal versucht ein paar der Funktionen bei mir einzufügen.

                                          Allerdings bekomme ich immer folgende Meldung:

                                          script.js.common.Array compile failed: at script.js.common.Array:256

                                          das komische. Das Script geht garnicht bis Zeile 256...

                                          Auch wenn ich dann alle Zeilen aus kommentiere habe ich dieses Problem.

                                          /**
                                           * Remove Duplicates from Array
                                           * Source - https://stackoverflow.com/questions/23237704/nodejs-how-to-remove-duplicates-from-array
                                           * @param {array} inputArray       Array to process
                                           * @return {array}  Array without duplicates.
                                           */
                                          
                                          function GlobalarrayRemoveDublicates(inputArray) {
                                              let uniqueArray;
                                              uniqueArray = inputArray.filter(function(elem, pos) {
                                                  return inputArray.indexOf(elem) == pos;
                                              });
                                              return uniqueArray;
                                          }
                                          
                                          /**
                                           * Clean Array: Removes all falsy values: undefined, null, 0, false, NaN and "" (empty string)
                                           * Source: https://stackoverflow.com/questions/281264/remove-empty-elements-from-an-array-in-javascript
                                           * @param {array} inputArray       Array to process
                                           * @return {array}  Cleaned array
                                           */
                                          /*
                                          function GlobalcleanArray(inputArray) {
                                            var newArray = [];
                                            for (let i = 0; i < inputArray.length; i++) {
                                              if (inputArray[i]) {
                                                newArray.push(inputArray[i]);
                                              }
                                            }
                                            return newArray;
                                          }
                                          */
                                          /**
                                           * Removing Array element(s) by input value. 
                                           * @param {array}   arr             the input array
                                           * @param {string}  valRemove       the value to be removed
                                           * @param {boolean} [exact=true]    OPTIONAL: default is true. if true, it must fully match. if false, it matches also if valRemove is part of element string
                                           * @return {array}  the array without the element(s)
                                           */
                                          /*
                                          function GlobalarrayRemoveElementsByValue(arr, valRemove, exact) {
                                           
                                              if (exact === undefined) exact = true;
                                           
                                              for ( let i = 0; i < arr.length; i++){ 
                                                  if (exact) {
                                                      if ( arr[i] === valRemove) {
                                                          arr.splice(i, 1);
                                                          i--; // required, see https://love2dev.com/blog/javascript-remove-from-array/
                                                      }
                                                  } else {
                                                      if (arr[i].indexOf(valRemove) != -1) {
                                                          arr.splice(i, 1);
                                                          i--; // see above
                                                      }
                                                  }
                                              }
                                              return arr;
                                          }
                                          */
                                          /**
                                           * Checks if Array or String is not undefined, null or empty.
                                           * 08-Sep-2019: added check for [ and ] to also catch arrays with empty strings.
                                           * @param inputVar - Input Array or String, Number, etc.
                                           * @return true if it is undefined/null/empty, false if it contains value(s)
                                           * Array or String containing just whitespaces or >'< or >"< or >[< or >]< is considered empty
                                           */
                                          /*
                                          function GlobalisLikeEmpty(inputVar) {
                                              if (typeof inputVar !== 'undefined' && inputVar !== null) {
                                                  let strTemp = JSON.stringify(inputVar);
                                                  strTemp = strTemp.replace(/\s+/g, ''); // remove all whitespaces
                                                  strTemp = strTemp.replace(/\"+/g, "");  // remove all >"<
                                                  strTemp = strTemp.replace(/\'+/g, "");  // remove all >'<
                                                  strTemp = strTemp.replace(/[+/g, "");  // remove all >[<
                                                  strTemp = strTemp.replace(/]+/g, "");  // remove all >]<
                                                  if (strTemp !== '') {
                                                      return false;
                                                  } else {
                                                      return true;
                                                  }
                                              } else {
                                                  return true;
                                              }
                                          }
                                          */
                                          
                                          Christoph1337 liv-in-sky AlCalzone 3 Replies Last reply Reply Quote 0
                                          • Christoph1337
                                            Christoph1337 @Christoph1337 last edited by

                                            Hab es selber gelöst:

                                            /** 
                                             * Checks if Array or String is not undefined, null or empty.
                                             * 08-Sep-2019: added check for [ and ] to also catch arrays with empty strings.
                                             * @param inputVar - Input Array or String, Number, etc.
                                             * @return true if it is undefined/null/empty, false if it contains value(s)
                                             * Array or String containing just whitespaces or >'< or >"< or >[< or >]< is considered empty
                                            */
                                            
                                            function GlobalIsLikeEmpty(inputVar) 
                                            {
                                                if (typeof inputVar !== 'undefined' && inputVar !== null) {
                                                    let strTemp = JSON.stringify(inputVar);
                                                    strTemp = strTemp.replace(/\s+/g, ''); // remove all whitespaces
                                                    strTemp = strTemp.replace(/\"+/g, "");  // remove all >"<
                                                    strTemp = strTemp.replace(/\'+/g, "");  // remove all >'<
                                                    strTemp = strTemp.replace(/[+/g, "");  // remove all >[<
                                                    strTemp = strTemp.replace(/]+/g, "");  // remove all >]<
                                            
                                                    if (strTemp !== '') {
                                                        return false;
                                                    } else {
                                                        return true;
                                                    }
                                                } else {
                                                    return true;
                                                }
                                            }
                                            
                                            1 Reply Last reply Reply Quote 0
                                            • First post
                                              Last post

                                            Support us

                                            ioBroker
                                            Community Adapters
                                            Donate

                                            528
                                            Online

                                            31.7k
                                            Users

                                            79.8k
                                            Topics

                                            1.3m
                                            Posts

                                            javascript
                                            10
                                            23
                                            11170
                                            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