Help with errors while trying to restore gpio state

I’m getting errors while trying to sendCommand after restart
items

Group                    Control               "Control"                    <switch> 
Switch Relay01          "Relay01"           <switch>          (Control)               ["Relay"]       {gpio="pin:2"}
Switch Relay02          "Relay02"           <switch>          (Control)               ["Relay"]       {gpio="pin:3"}
Switch Relay03          "Relay03"           <switch>          (Control)               ["Relay"]       {gpio="pin:17"}
Switch Relay04          "Relay04"           <switch>          (Control)               ["Relay"]       {gpio="pin:4"}
Switch Relay05          "Relay05"           <switch>          (Control)               ["Relay"]       {gpio="pin:27"}

rules

rule "Controls Initializing"
when
    System started
then
    Thread::sleep(30000) 
    Control.members.forEach[ i | i.sendCommand(i.previousState().state as OnOffType) ]
end

error:

2020-09-20 14:29:05.845 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Error during the execution of startup rule 'Controls Initializing': cannot invoke method public abstract org.eclipse.smarthome.core.types.State org.eclipse.smarthome.core.persistence.HistoricItem.getState() on null

That suggests that one or more of your Items does not have a persisted state.
Have you a persistence service installed, are your Items configured to persist, is that part working, is the system default service the one you expect?

1 Like

Thanks @rossko57 for respond
I’ve a mapdb and rrd4j running

mapdb.persist

Strategies {
    // for rrd charts, we need a cron strategy
    everyMinute : "0 * * * * ?"
    everyHour   : "0 0 * ? * *"
    everyDay    : "0 0 0 * * ?"     // Every day at midnight - 12am
    default = everyChange
}

Items {
    // persist all items on every change
    * : strategy = everyUpdate, restoreOnStartup
}

rrd4j.persist

Strategies {
    // for rrd charts, we need a cron strategy
    everyMinute : "0 * * * * ?"
    everyHour   : "0 0 * ? * *"
    everyDay    : "0 0 0 * * ?"     // Every day at midnight - 12am
    default = everyChange
}

Items {
    // persist all items on every change
    * : strategy = everyUpdate, restoreOnStartup
}

but I did not set a default service
runtime.cfg

################ PERSISTENCE ####################

#  The persistence service to use if no other is specified.
#
#org.eclipse.smarthome.persistence:default=

Then it won’t work.
previousState() pretty obviously asks the default service for data.
You can set the default, or tell previousState() which service to use.

off topic - why would you want both db to store everything and restore everything?

Thanks again

By set a default you mean set it in the runtime.cfg file, right ?
and how can I “tell previousState() which service to use.” ?

Both answers there.

I don’t know what I’m doing :))
I’m using charts for temperature and humidity and weather
which one do you suggest to use for charts, switches, settings ?
Thanks a lot

mapdb is good for restore-on-startup (including your gpio work)

rrd4j is good for numbers and charting. It doesn’t even work with half the Item types, so you probably don’t want to use it everything or for restore.

Persisting stuff you don’t need to persist creates i/o traffic, uses storage media space, reduces storage media life. Be choosy.

1 Like

@rossko57 Thanks for that help, you are helping me a lot
Here is my updates: , Note that I’m still getting the same errors

items

Group                    Control               "Control"                    <switch> 
Switch Relay01          "Relay01"           <switch>          (Control)               ["Relay"]       {gpio="pin:2"}
Switch Relay02          "Relay02"           <switch>          (Control)               ["Relay"]       {gpio="pin:3"}
Switch Relay03          "Relay03"           <switch>          (Control)               ["Relay"]       {gpio="pin:17"}
Switch Relay04          "Relay04"           <switch>          (Control)               ["Relay"]       {gpio="pin:4"}
Switch Relay05          "Relay05"           <switch>          (Control)               ["Relay"]       {gpio="pin:27"}

rrd4j.persist

Strategies {
    // for rrd charts, we need a cron strategy
    everyMinute : "0 * * * * ?"
    everyHour   : "0 0 * ? * *"
    everyDay    : "0 0 0 * * ?"     // Every day at midnight - 12am
    default = everyChange
}

Items {
    // persist all items on every change
    Sensors* : strategy = everyChange, everyUpdate, restoreOnStartup
}

mapdb.persist

Strategies {
    default = everyChange
}

Items {
    // persist Control items on every change
    Control*,  ControlPeriod : strategy = everyUpdate, restoreOnStartup
}

rules

rule "Controls Initializing 2"
when
    Member of Control changed
then
    logInfo("sps.ControlsInitializing2", "triggeringItem:" + triggeringItem.name + ".previousState(): " + triggeringItem.previousState().state.toString + " state: " + triggeringItem.state.toString)
   
end

I’m still getting errors

2020-09-20 15:37:30.752 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Controls Initializing 2': cannot invoke method public abstract org.eclipse.smarthome.core.types.State org.eclipse.smarthome.core.persistence.HistoricItem.getState() on null


Changes that you make to persistence settings do not take effect until the packages involved have been restarted.

I’ve never understood the habit of stuffing many operations into one line and then struggling to find out which bit is broken. Break it open, go step by small step.

from

logInfo("sps.ControlsInitializing2", "triggeringItem:" + triggeringItem.name + ".previousState(): " + triggeringItem.previousState().state.toString + " state: " + triggeringItem.state.toString)

to perhaps

logInfo("sps.ControlsInitializing2", "triggeringItem:" + triggeringItem.name)
logInfo("sps.ControlsInitializing2", "current state " + triggeringItem.state.toString)
var results = triggeringItem.previousState()
if (results == null {
   logInfo("sps.ControlsInitializing2", "Item has no default stored data")
} else {
   logInfo("sps.ControlsInitializing2", "historic state " + results.state.toString)
}

As you are relying on your chosen persistence service having somestored data to return, it’d be smart to find out if there actually is any stored data for that Item and that service. The REST API is a useful tool for this.

1 Like

I’ve restarted openhab but the same errors

The REST API is showing that no data are stored for some items , But why some items have no data ?

I don’t know. Which Items? Which persistence service?

sorry for being annoying
Relay02, Relay03, Relay04, Relay05 have no data
only Relay01, Relay04 have data

According to the strategy you have specified (via Control*) nothing will get persisted until those Items get a state update. Have they had one?

That’s in the rrd4j service. Is that your default? Is that the one you read with REST?

You should also read the rrd4j docs, especially as you mentioned charting. You almost certainly want to use an everyMinute strategy with rrd4j.

1 Like

It works fine now, Thanks a lot , :slight_smile:
sorry for keeping you here