JS Scripting: how to determine, that "previousstate" was UNDEF

If I use the following code:

console.info("previousState is: ", items.getItem("MyItem").persistence.previousState(true, "jdbc").state);

I only get “real” states, and not “UNDEF” or “NULL”. let’s say “MyItem” was

  1. ON
  2. UNDEF
  3. OFF

If the rule runs on state #3, the above code will say, the previousState was “ON”. I’d like to see, if it was “UNDEF”.
Is there a way to determine, that the state of an item was UNDEF before? For a trigger of a rule I can use “UNDEF” as “previous state”, but not in a rule itself?

UNDEF and NULL do not get saved to persistence. You’ll never see UNDEF or NULL from any persistence call.

But the event Object has event.oldState for file based rules or event.oldItemStarte for UI rules.

These only exist for received update and changed triggers. If the rule is triggered by something else these variables will be undefined. Note that the UI event Object is the raw event Object meaning everything in it is the raw Java Object. In file based rules it’s a JS wrapper and it provides JS data types.

ok, makes sense. I don’t like the idea to switch to some “irregular” values like “-99999” just to “disable” an item. Because “0” is a valid state…