JS Scripting: previousState

I’m trying out the new JS Scripting. I’m running OH 3.2.0, so just installed JSScripting inside the GUI - Automation(no other steps necessary?).

Trying to use previousState inside a script, according to the docs

var Power = items.getItem("Schakel_Server_Power");
console.log("Power Now", Power.state);
console.log("Previous Power", Power.history.previousState());

Bit this trow me an error:

2021-12-22 11:30:54.805 [INFO ] [org.openhab.automation.script       ] - Power Now 22
2021-12-22 11:31:04.818 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID '2426380ee0' failed: com.influxdb.exceptions.InfluxException: timeout

I’m using InfluxDB, and openhab is connected to influxdb (I see values getting added to the database). I also thought that you don’t need a persistence service for previousState. What am I doing wrong?

Which previousState? There are two completely different things by the same name, context and use is very important.

The rule triggering event may have a previousState element, if it is a state change event. ‘Old’ and ‘new’ states available in the event, unrelated to any persistence.

Persistence services add convenience methods to Items to retrieve their data, like averageSince(datetime). And of course one called previousState().
As described in the docs you reference.

Comment - the persistence previousState() can be tricky to use unless you have precise knowledge and control of when it gets persisted. Using default settings generally returns the ‘current’ value - as it has just been persisted …
But that’s about values and expectations, not the basic workings.

What you have done looks correct to me.
I would expect a different message if your Item was not being persisted at all, but you might check on that using API Explorer. Or try a historicState() retrieval.

Which previousState? There are two completely different things by the same name, context and use is very important.

Ok, what I try to achieve is do some calculation with the old and new value, so i started to get those 2 value’s.

The rule triggering event may have a previousState element, if it is a state change event. ‘Old’ and ‘new’ states available in the event, unrelated to any persistence.

Like this:

2021-12-22 12:26:17.851 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'Schakel_Server_Power' changed from 32 to 27

What you have done looks correct to me.
I would expect a different message if your Item was not being persisted at all, but you might check on that using API Explorer. Or try a historicState() retrieval.

If i change the rule to: console.log(“Previous Power”, Power.history.previousState(true, influxdb); the error is the same.

inside the API explorer(http://192.168.1.2:8080/rest/persistence/items/Schakel_Server_Power?serviceId=influxdb):

    {
      "time": 1640175850830,
      "state": "23.0"
    }

So the item is inside the influxdb with the last value’s.

The equivalent to Rules DSL’s previousState is event.oldItemState.

This has nothing to do with InfluxDB nor persistence. However, there is something wrong with your InfluxDB config. It’s timing out when attempting to make a query. I can’t help with that problem.

The equivalent to Rules DSL’s previousState is event.oldItemState .

When using event.oldItemState the log show: undefined
When using event.itemState the log show the correct last value

This has nothing to do with InfluxDB nor persistence. However, there is something wrong with your InfluxDB config. It’s timing out when attempting to make a query. I can’t help with that problem.

Don’t know the problem with InfluxDB. Like I already said, the connection from OH to influxdb is working nicely, al the states are stored inside influxdb.

As already said, availability depends on the rules triggering event, still an unknown at this point.

Have you tried some other persistence function, like historicState or latestState, to see if throws any light?

The Trigger is simple a state update

triggers:
  - id: "1"
    configuration:
      itemName: Schakel_Server_Power
    type: core.ItemStateUpdateTrigger

Have you tried some other persistence function, like historicState or latestState, to see if throws any light?

All gives a undefined, so something goes wrong getting information from influxdb. My influxdb(2.0) even crash when using this rule.

State update includes update-to-same-state i.e. no change. Not sure you get an old state with no change.

Sounds a bit drastic, might explain why it times out from the oipenHAB side.

Also with state change I don’t get the oldState.

Be a bit more forthcoming. The code you used, the result?

If you want to follow the persistence angle, JSS is all a bit new but it seems unlikely anything JSS can do should crash your influxdb.
You might try a similar retrieval from a simple DSL rule.

If i use a DSL Rule:

configuration: {}
triggers:
  - id: "1"
    configuration:
      itemName: Schakel_Server_Power
    type: core.ItemStateChangeTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      type: application/vnd.openhab.dsl.rule
      script: logInfo("Old state:", previousState.toString)
    type: script.ScriptAction

I get the previousState without a problem. So is the problem inside JS Scripting?
This is the rule i’m using(just to test previousState):

configuration: {}
triggers:
  - id: "1"
    configuration:
      itemName: Schakel_Server_Power
    type: core.ItemStateUpdateTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      type: application/javascript;version=ECMAScript-2021
      script: |-
        var Power = items.getItem("Schakel_Server_Power");
        console.log("Power Now", Power.state);
        console.log("Previous Power", Power.history.previousState());
    type: script.ScriptAction

This DSL is using the implicit variable previousState - this has nothing to do with persistence, and is derived from the event that triggers the rule.

This Javascript uses the Item’s persistence extension previousState, and will attempt to retrieve a value from your default persistence service.

So as these two things are nothing like each other, it tells us nothing new.

It would be interesting to find out if the persistence method worked from DSL, perhaps

logInfo("Test", "recorded: " + Schakel_Server_Power.previousState().toString)

when using this:

var A = Schakel_Server_Power.averageSince(now.minusMinutes(5), "influxdb")
logInfo("Old state:", A.toString)

I get the average value of the last 5 minutes correct

This is trowing a error!


2021-12-24 11:59:24.950 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID '85dede292d' failed: timeout
2021-12-24 11:59:25.758 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID '85dede292d' failed: interrupted

Also InfluxDB is crasing again.

Caused by: java.net.ConnectException: Failed to connect to /192.168.1.2:8086
........
Caused by: java.net.ConnectException: Connection refused (Connection refused)
........

But like I said, the connection to InfluxDB (2.0) is working nice, i see value’s getting added at the moment from OH.

The obvious difference is that the DSL averageSince retrieves from influxdb. The DSL previousState retrieves from whatever your system default is set to.
So let’s try spelling it out …

logInfo("Test", "recorded: " + Schakel_Server_Power.previousState("influxdb").toString)

System Default is InfluxDB…

Type mismatch: cannot convert from String to boolean; line 1, column 66, length 10

logInfo("Test", "recorded: " + Schakel_Server_Power.previousState(false,"influxdb").toString)  

This is crashing InfluxDB again

2021-12-24 12:08:47.176 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID '85dede292d' failed: timeout

Looks like you have found a bug in influxdb persistence layer.
Just try a different Item, in case it is something funny about that Items table.
Is there any special history here, like have you imported influxdb v1 data to v2?

Changed 2 months ago from 1.6 → 2.0 (now on 2.1.1) and imported data from v1 to v2