NEWS
[gelöst] Prüfen, ob ein State existiert
-
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.
-
Vielleicht so ?
if(getObject(id)) val = getState(id).val;
-
Vielleicht so ?
if(getObject(id)) val = getState(id).val; ```` `
Perfekt, genau so funktionierts :). Danke!
-
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; } }
-
@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 + '$]');
-
Super, vielen Dank @paul53 !
-
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; } }
-
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?
-
@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; } }
-
@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
-
Wenn der state existiert aber keinen Wert hat wird trotzdem eine warn ins log geschrieben.
Lösung istexistsState
zu verwenden, da bekommt dann auchfalse
zurück wenn kein Wert vorhanden ist, siehe:
https://forum.iobroker.net/topic/23548/getstate-warning-log-js-dokumentation/10Musste ich hier noch dokumentieren