Skip to content
  • Home
  • Recent
  • Tags
  • 0 Unread 0
  • Categories
  • Unreplied
  • Popular
  • GitHub
  • Docu
  • Hilfe
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
ioBroker Logo

Community Forum

donate donate
  1. ioBroker Community Home
  2. Deutsch
  3. Skripten / Logik
  4. [gelöst] Prüfen, ob ein State existiert

NEWS

  • Monatsrückblick Januar/Februar 2026 ist online!
    BluefoxB
    Bluefox
    18
    1
    668

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

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

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

Scheduled Pinned Locked Moved Skripten / Logik
11 Posts 5 Posters 2.7k Views 4 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dering
    wrote on last edited by
    #1

    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
    0
    • paul53P Offline
      paul53P Offline
      paul53
      wrote on last edited by
      #2

      Vielleicht so ?

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

      Bitte verzichtet auf Chat-Nachrichten, denn die Handhabung ist grauenhaft !
      Produktiv: RPi 2 mit S.USV, HM-MOD-RPI und SLC-USB-Stick mit root fs

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dering
        wrote on last edited by
        #3

        @paul53:

        Vielleicht so ?

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

        Perfekt, genau so funktionierts :). Danke!

        1 Reply Last reply
        0
        • MicM Offline
          MicM Offline
          Mic
          Developer
          wrote on last edited by
          #4

          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;
              }
          }
          
          paul53P 1 Reply Last reply
          0
          • MicM Mic

            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;
                }
            }
            
            paul53P Offline
            paul53P Offline
            paul53
            wrote on last edited by
            #5

            @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 + '$]');
            

            Bitte verzichtet auf Chat-Nachrichten, denn die Handhabung ist grauenhaft !
            Produktiv: RPi 2 mit S.USV, HM-MOD-RPI und SLC-USB-Stick mit root fs

            1 Reply Last reply
            1
            • MicM Offline
              MicM Offline
              Mic
              Developer
              wrote on last edited by
              #6

              Super, vielen Dank @paul53 !

              1 Reply Last reply
              0
              • MicM Offline
                MicM Offline
                Mic
                Developer
                wrote on last edited by
                #7

                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
                1
                • O Offline
                  O Offline
                  Oli
                  wrote on last edited by Oli
                  #8

                  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?

                  Gruß
                  Oliver

                  MicM 1 Reply Last reply
                  0
                  • O 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?

                    MicM Offline
                    MicM Offline
                    Mic
                    Developer
                    wrote on last edited by
                    #9

                    @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
                    1
                    • MicM Mic

                      @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 Offline
                      O Offline
                      Oli
                      wrote on last edited by
                      #10

                      @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

                      Gruß
                      Oliver

                      1 Reply Last reply
                      0
                      • ScroungerS Offline
                        ScroungerS Offline
                        Scrounger
                        Developer
                        wrote on last edited by Scrounger
                        #11

                        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
                        0

                        Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                        Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                        With your input, this post could be even better 💗

                        Register Login
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        Support us

                        ioBroker
                        Community Adapters
                        Donate

                        591

                        Online

                        32.7k

                        Users

                        82.6k

                        Topics

                        1.3m

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

                        • Don't have an account? Register

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