Unable to restore state for item

I have trouble restoring the previous state for an item and send it back to my pulse energy meter on request.
I have tried to code below, but it gives me the following error:

Error during the execution of rule 'Arduino sends to Openhab': Could not invoke method: org.openhab.core.persistence.extensions.PersistenceExtensions.previousState(org.openhab.core.items.Item,boolean,java.lang.String) on instance: null

I’m using the persistence service provided by “my openhab”.

        // Message Request
        if(msgType == 2){
        	if (subType == V_VAR1){
        		sendCommand(Arduino,"105;3;1;0;24;" + pulseCountItem.previousState(now)state + "\n")
        	}
        }

I’ve always wondered if my.openhab lets you query for historical data or whether it only publishes the data to enable the my.openhab service to work. Have you tried any of the other persistence such as rrd4j or db4o?

It is not clear from the documentation, but I have seen comments in other posts (not able to find them right now) that are pretty clearly stating that the my.openhab persistence service does not support restore/retrieve/query [take your pick].

1 Like

So I added the rrd4j bidning tried changing my line to

sendCommand(Arduino,"105;3;1;0;24;" + pulseCountItem.previousState(now, "rrd4j").state + "\n")

2015-10-09 21:39:33.845 [ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during the execution of rule ‘Arduino sends to Openhab’: Could not invoke method: org.openhab.core.persistence.extensions.PersistenceExtensions.previousState(org.openhab.core.items.Item,boolean,java.lang.String) on instance: null

However when starting after a restart I see the following in my log:

2015-10-09 21:34:38.843 [DEBUG] [.p.internal.PersistenceManager] - Restored item state from 'Oct 9, 2015 9:28:00 PM' for item 'cellarWatt01' -> '2937'
2015-10-09 21:34:38.854 [DEBUG] [.p.internal.PersistenceManager] - Restored item state from 'Oct 9, 2015 9:28:00 PM' for item 'cellarKwh01' -> '35.5379999999999967030817060731351375579833984375'
2015-10-09 21:34:38.865 [DEBUG] [.p.internal.PersistenceManager] - Restored item state from 'Oct 9, 2015 9:28:00 PM' for item 'pulseCountItem' -> '35538'

So not sure how my pulseCountItem can be null here

You are not calling previousState with the correct arguments. You are passing now when you need to pass a boolean.

pulseCountItem.previousState(true, "rrd4j").state  // gets most recent state that is different from the current state
pulseCountItem.previousState(false, "rrd4j").state // get the most recent update, even if it is the same as the current state

The underlying rules engine cannot convert the Joda time object “now” to a boolean so it passes a null instead is what I’m guessing is happening and why you got a null error. Maybe not.

I find it a little confusing that the documentation on the Persistence wiki page do not match what I’m seeing in Designer with <ctrl><space>. The wiki may be out of date.

Thank you SO much! It’s finally working :smile:
And yes, it is strange indeed that the documentation states that the argument should be an AbstractInstant. Cost me a couple of hours of debugging.

It probably worked that way in a previous version and the wiki wasn’t updated with the new behavior. When in doubt, I always go to Designer. :slight_smile:

Glad its working!

Thanks for the explanation.

But with this line

logInfo("test.rules", "Rollo_test zuvor " + Rollo_test.previousState(true, "rrd4j").state)

i’ve got still following error

[ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during the execution of rule ‘Sonne Test’: rrd4j does not allow querys without a begin date, unless order is descending and a single value is requested

What does this mean ? I want to work with the previous State.

Do you have more than one persistence binding configured and is that one the default one? If the answer is “no” to either question, try just previousState(true)

Is Rollo_test a Number, Switch, Contact, DateTime, or any other kind of Item that is represented by a Number? rrd4j will only store numerical data.

Are you saving the value for Rollo_test using an everyMinute strategy? Charting and the persistence methods tend not to work very well unless you save items once per minute, even if it only changes once per hour.

i have only one persistence binding.

when
        Item Rollo_test changed
then

but what is when i’ve got more then one. How can i define the default one ?

Yes, Rollo_test is an Number -> Rollershutter

And yes i save everyMinute and everyChange.

With

logInfo("test.rules", "Rollo_test zuvor " + Rollo_test.previousState(true))

i got the error message

Error during the execution of rule 'Rollo Test': rrd4j does not allow querys without a begin date, unless order is descending and a single value is requested

What does this means ?

Well I’m stumped. I’ve used rrd4j since I started using OH and I’ve never seen this error. I wonder if the DB became corrupt or something of the sort.

Try to clear out and recreate your persistence store to see if the error goes away or changes.

Stop openHAB, move /var/lib/openhab/persistence/rrd4j to a temporary folder, and restart openHAB. It should recreate the DBs from scratch from that point forward. If everything starts working then we found the problem. If not, I’m out of ideas.

Hello, same problem here. I try to store and recover the value of a number with the following rule:

rule test_numero
when
Item MyNumb changed
then
MyNumb.persist
logInfo(“MyNumb.previousState =”,MyNumb.previousState(true).toString)
logInfo("MyNumb.CurrentState = ", MyNumb.state.toString )
end

This leads to the following error:

2015-11-16 19:06:08.959 [ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during the execution of rule ‘test_numero’: rrd4j does not allow querys without a begin date, unless order is descending and a single value is requested

I know that something is working beacuas when I stop and restart OH tha value of the number is restored.

In my ubuntu installation I do not find the file /var/lib/openhab/persistence/rrd4j.

Any help would be very appreciated.

Did you install openhab using apt-get, the preferred method for installation on Linux? If not, your persistence file is somewhere under your openHAB folder.

Hello Rich, thank you for your reply.

The db files are in OPENHAB/etc/rrd4j and have the name of the items defined in the persistence file .rrd.

When deleting the old files , the new ones are created, as you said (it happens restarting or not OH indipendently) but unfortunately the problem with persistence is still there.

BTW, thank you for your post

Running into the same problem. Tried blowing away the old rrd files, still getting:

2015-12-16 07:14:46.085 [ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during the execution of rule 'Today Water Used': rrd4j does not allow querys without a begin date, unless order is descending and a single value is requested

import java.lang.Math

rule "Today Water Used"
when
	Item Water_Meter changed
then
	val double current_meter = Water_Meter.state
	val double previous_meter = Water_Meter.previousState(true).state
	val double today_water_used = if(Today_Water_Used.state == Uninitialized) 0 else Today_Water_Used.state
	postUpdate(Today_Water_Used, today_water_used + (current_meter - previous_meter))
end


rule "Today Water Used Reset"
when
	Time cron "59 59 23 * * ?"
then
	Today_Water_Used.postUpdate(0)
end

Same here. How to solve it?
@Kai @rlkoshak Any ideas how to use it?
Sorry for disturb maybe anyone can make advice which persistence service is best to use for store and query switches, nubmber states.

Please see:

The best to use depends on what you want to do with it. rrd4j doesn’t support Switches and Contacts anymore so that one is not a good choice. Mapdb only saves the most recent state and is an excellent choice for restoreOnStartup or if all you care about is the time of the most recent update to the Item.

Beyond that I can’t tell you what is the “best” because it depends on what you want to with the data and your level of experience.

Thanks for Information and patterns. I have Plex and when I start movie my lights go to wanted scene. When I press stop I want to get back states before I press start. As I understand rrd4j can’t help me as mapdb too or i wrong?