Accessing previous state produces an error

  • Platform information:
    • Hardware: amd64/8Gb/12
    • OS: Ubuntu 20.04
    • Java Runtime Environment: zulu-8 8.99.0.0-1
    • openHAB version:2.5.7-1
  • Issue of the topic:
    Whenever I try to access the previous state of a variable in the rule I get the following error in the logs

2020-08-25 18:01:30.448 [WARN ] [nce.extensions.PersistenceExtensions] - There is no default persistence service configured!
2020-08-25 18:01:30.449 [WARN ] [nce.extensions.PersistenceExtensions] - There is no queryable persistence service registered with the id ‘null’

I have two persistence services configured as following

cat persistence/mapdb.persist

Strategies {
default = everyChange
}

Items {
* : strategy = everyChange, restoreOnStartup
}

cat persistence/mysql.persist

Strategies {
// if no strategy is specified for an item entry below, the default list will be used
everyMinute : “0 * * * * ?”
every5Minutes : "0 /5 * * * ?"
everyHour : “0 0 * * * ?”
everyDay : “0 0 0 * * ?”
everyWeek : "0 0 0 ? * MON "
everyMonth : "0 0 0 1 * ? "
default = everyChange
}
Items {
// persist all items once a day and on every change and restore them from the db at startup
gPers_Change_Month
: strategy = everyChange, everyMonth, restoreOnStartup
gPers_Change_Week
: strategy = everyChange, everyWeek, restoreOnStartup
gPers_Change_Day
: strategy = everyChange, everyDay, restoreOnStartup
gPers_Change_Hour* : strategy = everyChange, everyHour, restoreOnStartup
gPers_5Min* : strategy = every5Minutes, everyHour
gPers_1Min* : strategy = everyMinute, everyHour
//* : strategy = everyChange, everyDay, restoreOnStartup
}

Item description

Number sens_vibr02_strength “Vibr02 Strength [%d]” (gPers_Change_Day) {channel=“mqtt:topic:mymosquitto:sens_vibr02:strength”}

request for the previousstate in a rule

logInfo("ALARM: ", sens_vibr02_strength.previousState().state)

I can see that mysql has the records of every change in the table
| 2020-08-25 17:26:10 | 24 |
| 2020-08-25 17:31:15 | 45 |
| 2020-08-25 17:39:07 | 86 |
| 2020-08-25 17:45:35 | 60 |
| 2020-08-25 17:55:20 | 34 |
| 2020-08-25 18:00:30 | 64 |
| 2020-08-25 18:01:29 | 92 |
±--------------------±------+
61 rows in set (0.00 sec)

But for some reason the OH throws an error that “There is no default persistence service configured!”

What could be the reason?

There are several topics in the forum that discuss default persistence including how to configure it. For example:

You need to make sure you either specify which persistence service to use in your attempt to access the previous state or have OH configured to use one of the services in the absence of a more specific call.

1 Like

Thanks Justin.

Apparently if I set .previousState(“mapdb”).state or .previousState(“mysql”).state it does not work and produces the same error,
However if I define globally the
org.eclipse.smarthome.persistence:default=mysql

then .previousState().state works. Really weird


Somehow the historic state works when I specify the mysql
.historicState(now.minusMinutes(60), “mysql”).state

Apparently you MUST specify true or false when retrieving previousState. I don’t think it is mentioned in the manual :frowning:
so the correct statement is
.previousState(true, “mysql”).state
.previousState(false, “mysql”).state

What the documentation says is that
myItem.previousState()
myItem.previousState("mysql")
myItem.previousState(true, "mysql")
should all work.

(with the caveat - )

It is important to select a default persistence service.

and the description of how to do that.

If it doesn’t work as described, please tell us!

I am testing using the following statement
logInfo("ALARM2: ", "previous " + sens_vibr02_angle_x.previousState().state)

persistence default is set to mapdb. I have both mysql and mapdb persistence enabled.

WORKS
.previousState().state
.previousState(true,“mysql”).state
.previousState(false,“mysql”).state

DOESNT WORK
.previousState(“mysql”).state
2020-08-28 13:04:53.110 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule ‘vibration strength notification’: An error occurred during the script execution: Could not invoke method: org.eclipse.smarthome.model.persistence.extensions.PersistenceExtensions.pr
eviousState(org.eclipse.smarthome.core.items.Item,boolean) on instance: null

.previousState(true).state
2020-08-28 13:07:41.265 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule ‘vibration strength notification’: cannot invoke method public abstract org.eclipse.smarthome.core.types.State org.eclipse.smarthome.core.persistence.HistoricItem.getState() on null

Have you followed the recommended process to set a default persistence? You seem to have done something else instead.

As an aside, not everything works with mapdb because it stores only one record.

hmm, I think my configs are pretty straightforward, unless I miss something obvious.

mapdb and mysql persistence are loaded. mapdb is set as default.

/etc/openhab2 # grep -Ri mapdb *                                                                                                                                                                                                         
services/addons.cfg:persistence = mapdb,mysql
services/runtime.cfg:org.eclipse.smarthome.persistence:default=mapdb

mapdb is configured to store all items’ values and restore from it when OH starts.

 /etc/openhab2/persistence # cat mapdb.persist
    Strategies {
            default = everyChange 
    }
     
    Items {
            * : strategy = everyChange, restoreOnStartup
    }

mysql has groups defined with the frequency of how often to record values.

Strategies {
        everyMinute     : "0 * * * * ?"
        every5Minutes   : "0 */5 * * * ?"
        everyHour       : "0 0 * * * ?"
        everyDay        : "0 0 0 * * ?"
        everyWeek       : "0 0 0 ? * MON *"
        everyMonth      : "0 0 0 1 * ? *"
        default = everyChange
}

Items {
    gPers_Change_Month* : strategy = everyChange,   everyMonth, restoreOnStartup
    gPers_Change_Week*  : strategy = everyChange,   everyWeek, restoreOnStartup
    gPers_Change_Day*   : strategy = everyChange,   everyDay, restoreOnStartup
    gPers_Change_Hour*  : strategy = everyChange,   everyHour, restoreOnStartup
    gPers_5Min*         : strategy = every5Minutes, everyHour
    gPers_1Min*         : strategy = everyMinute,   everyHour
}

Can you spot any misconfiguration in my configs?

Okay, if mapdb is your default then
myItem.previousState(true)
just cannot work. Mapdb has no other state than the one it currently holds, so you cannot retrieve the state before.
Not all the standard persistence functions make sense for any particular database.

Of course, any given previousState might return null if there is no record e.g. when persisting once a month.

I do not know if the way that you have chosen to set the default produces any different result to the recommended way.

It’s a bit nuts having mySQL doing restore as well as mapdb, but that’s not related here.

What problems are left now?

Thanks Rossko57.

I would have thought that even with the mapdb it should be possible to retrieve the last value. Providing that the new value is not yet written to the mapdb, it will contain the previous state.

re mysql and restoreonstartup, i remember i had a problem a while back when I just started playing with persistence, but I guess it might have been due to the fact that I didn’t set the default persistence and set mapd in .perstist file only. But you are right, restoring from both sources doesn’t make any sense.

no problems left with persistence, and thanks for your help.

The thing about mapdb is that previousState is meaningless. It holds the “now” state. You certainly cannot get the “previous state that was different to now”.