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.
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.
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.
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 …
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?