OpenHAB3 JavaScript Rules: unable to get triggeringItem or access PersistenceExtensions

I’been testing openHAB3 and trying to make some javascript rules. I want to compare the initial state of the triggering item inside the rule script. Do I need to use the PersistenceExtensions to access the previousState of the item? or is this information in some variable? Thanks in advance for the help.
This is the generated code I got right now:

  - id: "1"
      itemName: Lamparasalon_Color
    type: core.ItemStateChangeTrigger
conditions: []
  - inputs: {}
    id: "2"
      type: application/javascript
      script: >-
        var logger = Java.type("org.slf4j.LoggerFactory").getLogger("scripts.LuzSalonSwitchSync");

        var PersistenceExtensions = Java.type("org.openhab.core.persistence.extensions.PersistenceExtensions");

        var currentState = PersistenceExtensions.previousState(ir.getItem("Lamparasalon_Color"), true);
    type: script.ScriptAction

I getting a java.lang.RuntimeException: java.lang.ClassNotFoundException: org.openhab.core.persistence.extensions.PersistenceExtensions cannot be found by
Again, thanks for the help.

The oldState and newState are put in variables when the triggering module is core.ItemStateChangeTrigger.

(Technically, they are “outputs” to that module and all outputs from triggers are added to the script scopes).

So this should print the old and new states on the console (you can change that to use the logger instead):

var from = oldState.toString();
var to = newState.toString();

print(from + '>' + to);

In the future I’ll try to detect those available outputs and add them to the autocompletion list.

You can also inspect the context of the script, where you will also see these:


Thanks for the quick answer. That works perfect for me.
Just as a comment, I’m facing a strange behavior. After save the rule for first time, if I run it by clicking the play button in the interface I’m getting this error 16:42:17.100 [ERROR] [.internal.handler.ScriptActionHandler] - Script execution failed: ReferenceError: "oldState" is not defined in <eval> at line number 3, if later I make a change to the trigger item the rule runs as expected, and after this first successful execution the rule also runs fine from the Play button on the interface.
I don’t know if this is the expected behavior or it could be considered an issue.
Thanks again for the previous help. OpenHAB 3 looks awesome!

So there is no Item based event to extract old state from?

I did’t know button was intended to run the rule with the last execution variables. It is a bit weird to have this variables as not defined in the first run.Thanks for the clarification.

Discussed here…

This is related to this issue, which TMK was never recreated or resolved in the OHC repo…

1 Like

Hey ysc,

Where did you get this information from regarding the triggering module outputs? I don’t think this is documented anywhere just yet but as I am rewriting all my rules in javascript it would prove handy.

1 Like