OH4 itemHistory lastChange

I was wondering, whether there was a possibility to retrieve the “lastChange” timestamp from an item History using (e.g. rrdj4 persistence) service.

I understand that

item["This_item"].history.lastUpdate()

will provide the timestamp of the previously persisted state, including those, where the state is unchanged (but updated). If you have the persistence policy set to something like :

Strategies {
        everyMinute  : "0 */1 * * * ?"
        default = everyChange
}
Items {
        * : strategy = everyMinute, everyChange
}

I understand I could scrap the everyMinute strategy, but for some reason I don’t dare doing that, since my persistence now works well for frequently and rarely changing items.

So is there a possibility to retrieve the timestamp from an item’s history, when it was last changed, as opposed to updated?

Many thanks

From the docs:

<item>.previousState(true) Gets the previous State of a persisted Item, skips Items with equal State values and searches the first Item with State not equal the current State (returns HistoricItem)

That returns a HistoricItem which includes a state and tiemstamp.

No, you can’t scrap that for rrd4j.

Rich, I have not been very lucky in using these persistence extensions. Say I have an item called Lights_Power, which is persisted with rrdj4 (everyChange, everyMinute).

Can you help me understanding, what <item> actually is, the name of the Item or the Item itself in a variable?

Reason I’m asking, both give me an error message:

console.log(Lights_Power.previousState(true));

tells me:
org.graalvm.polyglot.PolyglotException: ReferenceError: "Lights_Power" is not defined

while

console.log(items['Lights_Power'].previousState(true));

scolds me of the following:

org.graalvm.polyglot.PolyglotException: TypeError: (intermediate value)["Lights_Power"].previousState is not a function

I have progressed a little further with the syntax by using the opanhab-js itemHistory Class directly, which is not found in the “docs”, but vaguely referred to in the API documentation:

console.log(items.Lights_Power.history.previousState(true));

While that syntax is apparently admissible, here I’m informed of yet another problem:

2023-10-04 20:44:02.828 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'test' failed: java.lang.UnsupportedOperationException: rrd4j does not allow querys without a begin date, unless order is descending and a single value is requested

I’m a bit at a loss on what to do.

It depends on the language you are using. That’s why it’s left kind of vague.

Everything you find here are the docs too. Each add-on’s docs are official docs same as everything you find at Introduction | openHAB.

This should be the first place you look for how to do anything using JS Scripting.

This looks like a bug on the rrd4j add-on. About all I can recommend is to file an issue on openhab-addons.

I’m not sure there is a viable work around short of using some other persistence database which presumably does not have this problem.

On this, I really do not want to disgress from the subject. But it, frankly, doesn’t help to to imply with every other answer to a valid question of less experienced users that they aren’t reading the bible, while the bible may not be challenged. In this particular case. I have read the “documents” on the extensions, they provide a pseudo-syntax and no mention that they cannot be used in the most commonly used language JS Scripting., which - if I understand correctly - OH4 is now converging too.

Fine, it depends on the languages, if it’s Javascript ECMA v11,
What is then the correct syntax for using this extension?
as the two ones I tried out, do not work?

And it was, that’s how I came to discover that their is exactly zero information of what the 24 functions of the ItemHistory object actually do. This information, is - as mentioned - vaguely included in the API documentation. When using the syntax provided there with openHAB standard persistence service rrd4j, it gives you an error message that seems out of context.

I don’t want to get into a discussion on how to use this forum. But to me the problem doesn’t seem to be that users aren’t reading the documents, it’s that there is just too many non-updated pages in the documents and relevant information isn’t very logically distributed and more often than not leads to a dead end.

So i’m trying to rephrase my question again: Is there a (working) way to determine one of the many many JS Scripting functions or concepts and openHAB’s standard persistence service rrd4j, at what time(stamp) an Item was last changed ?

I can determine, when it was last updated – and the answer with rrd4j is - always - within the last minute, if the everyMinute strategy is active.

Ok, instead of trying to help you and more importantly future users if this forum better understand how to use the docs as they are more, I’ll just answer, again.

With rrd4j, right now. No.

Maybe it’s not clear from that, but you have the right syntax. There’s a bug preventing it from working. I doubt that previous state works right now in any language.

Cool.

@Dirk_Albrecht

The .history property of the Item class is documented in the JavaScript Scripting add-on README: JavaScript Scripting - Automation | openHAB

ItemHistory is documented in the same README, this paragraph is linked: JavaScript Scripting - Automation | openHAB

There are also two examples, however the first one could be updated with regards to the timestamp.

That code looks good and the code itself works, as the exception is coming from RRD4J. Unfortunately, getting the previous state doesn’t work but that’s an RRD4J issue and neither an issue of JavaScript Scripting nor your code.

1 Like

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