Read historicstate(n), where n is a integer

Good morning everyone.
I’m working on OH3.4 with DSL rules and I need your help.
Is it possible to read the timestamp of a state recorded n historicstate before? I’ll try to explain myself better: I have a shelly that detects power consumption. Everything is fine and everything works, but often the power goes out. Since it is a country house, the power may not be reconnected for days.
The moment the power comes back on and openhab wakes up, I would like to read the timestamp of one of the status recordings made before the power went out. Is it possible? I use MariaDB as a persistence

I am doing something similar

You can use an item that stores system startup time, and use that to historicState

Hi @ssalonen , it could be a solution, except that the elements containing the data to be checked are in a large number.
However, it is a good workaround that I will use if there is no other way to get the data I need.
It would be interesting if this method existed to check whether there have been “jumps” due to external causes between one update and another of an item.

I would use a uninterruptible power supplie (USP).
Check the state of the ups and shut down the openhab server when needed :wink:

That’s not what I’m trying to do.
I’ll try to explain myself better.
I have this shelly that sends consumption readings.
The binding only provides an overall reading of the consumption and it is I who, using a DSL rule, divides the consumption into daily, monthly and annual, storing the data in another item and resetting the counter. What I noticed is that if the power outage were to occur around midnight, the reset is not performed.
I therefore thought that if I could intercept the timestamp of one of the immediately previous updates for a comparison with the timestamp of the current update I could overcome this by “forcing” the reset at that moment.
I saw that there is a “lastUpdate” method but I don’t think it can be used because I imagine that OH, at startup, immediately carries out an update from all the items and this would make me lose the timestamp of the last update I had before the power cut.

I am pretty sure that restoreOnStartup doesn’t cause a new entry in the database. However where possible I am sure that as Things initialize they query the devices for updates.

But this new value is almost certainly going to be different from the last saved value. When you pass true to the lastUpdate method it returns the least update different from the current Item state. if this is run in a system started rule at an early run level you should get the timestamp you need.

In fact, if you run this at run level 40 you can probably just use lastUpdate as the Things didn’t start to come online until run level 60 IIRC. If the timing works out right the rule would run before OH connects back to the Shelly.

However a lot needs to go right timing wise for this so you’d have to test to see if it works out not and reliably.

Thanks @rlkoshak ,
I’ll work on it a bit. My openhab server runs on an old laptop and a power outage doesn’t necessarily cause it to shut down; the battery still has some autonomy. I was hoping there was a way to read a state by taking n steps back, regardless of a specific date and time, but I’ll work on it.
I will have to find a balance between running a rule during openhab startup, the solution proposed by @ssalonen , and maybe checking the battery charge status and network status. However, if I can suggest an implementation to the system, a search/scroll by step (go back n steps) in the Item states could be useful.
Thanks again