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
//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);
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.
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.
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.
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.