Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. Skripten / Logik
    4. [gelöst] Prüfen, ob ein State existiert

    NEWS

    • Neuer Blog: Fotos und Eindrücke aus Solingen

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

    • ioBroker goes Matter ... Matter Adapter in Stable

    [gelöst] Prüfen, ob ein State existiert

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

      Hi zusammen,

      gibt es eine Möglichkeit zu prüfen, ob ein bestimmtes Object existiert bevor ich darauf zugreife?

      Klar ich könnte einfach ein getState("").val machen und das Ergebnis in eine Variable schreiben und diese dann prüfen, aber ich bekomme Warnungen in der Log angezeigt die eventuell irritieren.

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

        Vielleicht so ?

        if(getObject(id)) val = getState(id).val;
        
        1 Reply Last reply Reply Quote 0
        • D
          dering last edited by

          @paul53:

          Vielleicht so ?

          if(getObject(id)) val = getState(id).val;
          ```` `  
          

          Perfekt, genau so funktionierts :). Danke!

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

            Problem bei getObject(id) ist, dass hier immer noch eine Warnmeldung im Log erscheint, falls der State nicht existiert. Ich habe mir daher so geholfen:

            /**
             * Checks if a a given state or part of state is existing.
             * This is a workaround, as getObject() or getState() throw warnings in the log.
             * Also works for state 'folders'.
             * Please note: if the full state path is 'javascript.0.switches.Osram.Bedroom', and your input
             * string is 'javascript.0.switches.Osram.Bed' (so Bed and not Bedroom), it will result in true.
             * @param {string} strStatePath   Input string of state, like 'javascript.0.switches.Osram.Bedroom'
             * @return {boolean}       true if state exists, false if not
             */
            function isState(strStatePath) {
                let mSelector = $('state[id=' + strStatePath + ']');
                if (mSelector.length > 0) {
                    return true;
                } else {
                    return false;
                }
            }
            
            paul53 1 Reply Last reply Reply Quote 0
            • paul53
              paul53 @Mic last edited by

              @Mic sagte:

               * Please note: if the full state path is 'javascript.0.switches.Osram.Bedroom', and your input
               * string is 'javascript.0.switches.Osram.Bed' (so Bed and not Bedroom), it will result in true.
              

              Das kann man mit einem $ am Ende umgehen.

                  let mSelector = $('state[id=' + strStatePath + '$]');
              
              1 Reply Last reply Reply Quote 1
              • Mic
                Mic Developer last edited by

                Super, vielen Dank @paul53 !

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

                  Hier nun mit Pauls Hilfe verbessert.

                  /**
                   * Checks if a a given state or part of state is existing.
                   * This is a workaround, as getObject() or getState() throw warnings in the log.
                   * Set strict to true if the state shall match exactly. If it is false, it will add a wildcard * to the end.
                   * See: https://forum.iobroker.net/topic/11354/
                   * @param {string}    strStatePath     Input string of state, like 'javascript.0.switches.Osram.Bedroom'
                   * @param {boolean}   [strict=false]   Optional: if true, it will work strict, if false, it will add a wildcard * to the end of the string
                   * @return {boolean}                   true if state exists, false if not
                   */
                  function isState(strStatePath, strict) {
                      let mSelector;
                      if (strict) {
                          mSelector = $('state[id=' + strStatePath + '$]');
                      } else {
                          mSelector = $('state[id=' + strStatePath + ']');
                      }
                      if (mSelector.length > 0) {
                          return true;
                      } else {
                          return false;
                      }
                  }
                  
                  1 Reply Last reply Reply Quote 1
                  • O
                    Oli last edited by Oli

                    Hallo zusammen,

                    bin wahrscheinlich wieder mal zu doof dafür.

                    ich möchte nur prüfen ob dieser state "snmp.0.192_168_2_120.System_Uptime" vorhanden ist.

                    Wenn er vorhanden ist soll der Wert ausgelesen werden, wenn nicht soll etwas anderes gemacht werden.

                    Bei diesem Vorschlag von @paul53 "if(getObject(id)) val = getState(id).val;" bekomme ich trotzdem eine Warnmeldung im Log, wenn der state nicht vorhanden ist.

                    Bei der Vorlage von @Mic blicke ich gar nicht durch.

                    Wie setze ich mein Vorhaben am besten um?

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

                      @Oli sagte in [gelöst] Prüfen, ob ein State existiert:

                      Bei der Vorlage von @Mic blicke ich gar nicht durch.

                      Führe mal das hier aus, dann siehst Du, wie es geht.

                      if (isState('snmp.0.192_168_2_120.System_Uptime', true)) {
                          log('State existiert.')
                      } else {
                          log('State existiert NICHT.')
                      }
                      
                      
                      /**
                       * Checks if a a given state or part of state is existing.
                       * This is a workaround, as getObject() or getState() throw warnings in the log.
                       * Set strict to true if the state shall match exactly. If it is false, it will add a wildcard * to the end.
                       * See: https://forum.iobroker.net/topic/11354/
                       * @param {string}    strStatePath     Input string of state, like 'javas-cript.0.switches.Osram.Bedroom'
                       * @param {boolean}   [strict=false]   Optional: if true, it will work strict, if false, it will add a wildcard * to the end of the string
                       * @return {boolean}                   true if state exists, false if not
                       */
                      function isState(strStatePath, strict) {
                          let mSelector;
                          if (strict) {
                              mSelector = $('state[id=' + strStatePath + '$]');
                          } else {
                              mSelector = $('state[id=' + strStatePath + ']');
                          }
                          if (mSelector.length > 0) {
                              return true;
                          } else {
                              return false;
                          }
                      }
                      
                      O 1 Reply Last reply Reply Quote 1
                      • O
                        Oli @Mic last edited by

                        @Mic said in [gelöst] Prüfen, ob ein State existiert:

                        if (isState('snmp.0.192_168_2_120.System_Uptime', true)) { log('State existiert.') } else { log('State existiert NICHT.') } /** * Checks if a a given state or part of state is existing. * This is a workaround, as getObject() or getState() throw warnings in the log. * Set strict to true if the state shall match exactly. If it is false, it will add a wildcard * to the end. * See: https://forum.iobroker.net/topic/11354/ * @param {string} strStatePath Input string of state, like 'javas-cript.0.switches.Osram.Bedroom' * @param {boolean} [strict=false] Optional: if true, it will work strict, if false, it will add a wildcard * to the end of the string * @return {boolean} true if state exists, false if not */ function isState(strStatePath, strict) { let mSelector; if (strict) { mSelector = $('state[id=' + strStatePath + '$]'); } else { mSelector = $('state[id=' + strStatePath + ']'); } if (mSelector.length > 0) { return true; } else { return false; } }

                        Super, Dankeschön

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

                          Wenn der state existiert aber keinen Wert hat wird trotzdem eine warn ins log geschrieben.
                          Lösung ist existsState zu verwenden, da bekommt dann auch false zurück wenn kein Wert vorhanden ist, siehe:
                          https://forum.iobroker.net/topic/23548/getstate-warning-log-js-dokumentation/10

                          Musste ich hier noch dokumentieren 😉

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

                          Support us

                          ioBroker
                          Community Adapters
                          Donate

                          888
                          Online

                          31.8k
                          Users

                          80.0k
                          Topics

                          1.3m
                          Posts

                          5
                          11
                          2219
                          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