[SOLVED/Bug verified] OH3 Ecma/Javascript PersistenceExtension PreviousState with true: Is it a bug or my error?

Hi,

I am on OH 3.1 stable and use an ECMA script for a rule. Trigger is a switch and I want to have the previous state of a lamps brightness, but not simply the last, but the last different. For that there is in the Persistence Extension the method previousState(). First parameter is the item, second could be “true” to fulfill the requirement that the value is differnt to the current state. That throws an error in my testing script.

This is my code:

var Log = Java.type("org.openhab.core.model.script.actions.Log");
var ZonedDateTime = Java.type('java.time.ZonedDateTime');
var PersistenceExtensions = Java.type("org.openhab.core.persistence.extensions.PersistenceExtensions");

var lightItemPrevious = PersistenceExtensions.previousState(ir.getItem("FridasZimmerDeckenleuchteHinten_Helligkeit"), true);
Log.logError("AtticLights", "AtticLights eventItem PREVIOUS: " + lightItemPrevious);

results in

2021-08-29 19:01:44.148 [ERROR] [e.automation.internal.RuleEngineImpl] - Failed to execute rule '99a9b30cc0': Fail to execute action: 3

If I don’t use the “true”, I get an result.

var lightItemPrevious = PersistenceExtensions.previousState(ir.getItem("FridasZimmerDeckenleuchteHinten_Helligkeit"));
Log.logError("AtticLights", "AtticLights eventItem PREVIOUS: " + lightItemPrevious);

leads to

2021-08-29 19:07:13.307 [ERROR] [penhab.core.model.script.AtticLights] - AtticLights eventItem PREVIOUS: 29.08.21, 19:07: FridasZimmerDeckenleuchteHinten_Helligkeit -> 0

Has anybody a clue if I do something wrong or if it is a bug?

Thanks in advance!
HFM

Here is an example:

//below gets the historical sate if an item
var PersistenceExtensions = Java.type("org.openhab.core.persistence.extensions.PersistenceExtensions");
var ZonedDateTime = Java.type("java.time.ZonedDateTime");

var myPersistentItem = ir.getItem("Zigbeesengledbulb_Zigbeesengledbulb");
logger.info("The pyload is {}", myPersistentItem);

var sinceDate = ZonedDateTime.now().minusDays(7);
//var sinceDate = ZonedDateTime.now().minusHours(5);

var maximumValueSince = PersistenceExtensions.maximumSince(myPersistentItem, sinceDate);

logger.info("Historic item is {}", maximumValueSince);
logger.info("Item name is {}", maximumValueSince.name);
logger.info("Item state is {}", maximumValueSince.state);

More examples here:

Hi @ubeaut!

Thanks for response!

I need the last previous state which differs from current state. For that there is a parameter in the previousState() method of the Persistence Extension. It is the second parameter. I found examples here in the forum - they did work for the people. At my instance they do not.

Therefore my question, if it is a bug introduced in newer versions or if something is wrong in my notation.

Good start into the week!
HFM

If I leave the true parameter in then it crashes.

var Log = Java.type("org.openhab.core.model.script.actions.Log");
var ZonedDateTime = Java.type('java.time.ZonedDateTime');
var PersistenceExtensions = Java.type("org.openhab.core.persistence.extensions.PersistenceExtensions");

var lightItemPrevious = PersistenceExtensions.previousState(ir.getItem("Rawmqttdata_Outsidetemperature"));
Log.logInfo("AtticLights", "AtticLights eventItem PREVIOUS: " + lightItemPrevious);

Give me this:
2021-08-30 19:52:05.057 [INFO ] [penhab.core.model.script.AtticLights] - AtticLights eventItem PREVIOUS: 30/8/21, 7:51 pm: Rawmqttdata_Outsidetemperature → 7.7

Ok, then it is a bug. I will open a ticket.

Thanks @ubeaut for your help!
HFM

Not all of the persistence services support all of the possible features.
You should at least work out which one is involved here i.e. your default service.

Hi @rossko57,

I use rrd4j as default and it has historical data, because if I use another method, eg averageSince, with a datetime there is no error.

Here you can find the method:
https://www.openhab.org/javadoc/latest/org/openhab/core/persistence/extensions/persistenceextensions#previousState(org.openhab.core.items.Item,boolean)

Do you have some idea where I can get more info about the compatibility of rrd4j?

Thanks!
HFM

There is no “compatibility list”, because it’s not just about the service but also the data that you have configured to store. You need to know what you store where and when to predict the results.

rrd4j can act funny with some combinations, because of its archiving structure and older data is compressed.

For clarity, I too would expect to get something back from your previousState() query, as you have shown the basic data is present.

Note that you should get an object back, with .state and .getTimestamp properties.

So far as I know, this quirky rrrd4j behaviour still exists -
https://github.com/openhab/openhab1-addons/issues/5965
That’s about historicState(), but may well also affect previousState(true) as it includes an element of seeking backwards through records.

1 Like

Hi @rossko57 (and all others),

because of your advise I installed influxdb and changed the database. I can now confirm that the described behaviour is definitely a bug related to “rrd4j”.

So, to all the others having the same problem with previousState(item, true) wanting to get the last diferent state of an item => do not use “rrd4j” as persistence database. I use influxdb successfully.

Thanks
HFM

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.