Problematic persistence - item.previousState() returns null

Hi all

Trying to wrap my head around this for a quite a while, and thought of reaching out to the community for a clue.

Problem in a nutshell:
Persistence not returning proper previous state.

I’m working on a user configurable home automation solution with dynamic items, sitemaps and rules creations. One of the problems I have is when an unexpected power cycle happens (which is bit common in this part of the world), my end-points (ESP8266 driven relays) comes back on-line with all relays at on position. To resolve this issue, Im programming my endpoints to send a message (MQTT) every-time they boot-up and have an item in my items file to receive it.

String  mqtt_init               "MQTT Initiation message"       {mqtt="<[halmqtt:HAL_COM/MQTT/init:state:default]"}

Then I created a rule to reply back with previous state of the items so the end point can restore the original states. I understand restoreOnStartup does this, but there’s no openhab startup here, just the end-points, and hence the workaround. I have following logInfo line in the rule just to check if it works

rule "Persistance"
when
        Item mqtt_init received update
then
        logInfo("Persistance","Rules kicked")
        logInfo("Persistance",my_item.previousState())
end

This is where things go south. I get following lines in the openhab.log

2018-07-09 16:14:16.415 [INFO ] [e.smarthome.model.script.Persistance] - Rules kicked
2018-07-09 16:14:16.427 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Persistance': An error occurred during the script execution: Could not invoke method: org.eclipse.smarthome.model.script.actions.LogAction.logInfo(java.lang.String,java.lang.String,java.lang.Object[]) on instance: null

First I though my persistence setting were wrong, but I observed them properly recorded in the MariaDB table:

±------------------------±------+
| time | value |
±------------------------±------+
| 2018-07-09 14:55:51.683 | ON |
| 2018-07-09 15:05:11.226 | OFF |
| 2018-07-09 15:05:31.963 | ON |
| 2018-07-09 15:09:10.782 | OFF |
| 2018-07-09 15:09:11.937 | ON |
| 2018-07-09 15:11:53.551 | OFF |
| 2018-07-09 15:11:55.450 | ON |
| 2018-07-09 16:17:11.086 | OFF |
| 2018-07-09 16:17:12.154 | ON |
±------------------------±------+

Any idea why Im not getting proper previousState in this situation?

logInfo("Persistance",my_item.previousState())

do you mean mqtt_init, then not, what item-type is my_Item and what item is stored in the persistence-table you posted.

What about your openHAB server. Does that suffers from the power cut too?
Because it will take significantly longer to reboot and answer from your ESPs mqtt messages.

Are they running your own code or are running something like Tasmota?

I’m not looking for the persistence of mqtt_init here. My requirement is to get the previousState of other items, represented here by my_item

Openhab server also getting impacted by power outages. But this was tested without taking server out. Trying to solve one piece of the puzzle at a time :slight_smile:

BTW: End-point ESP’s are running my own code.

That will not work if your OH server is affected by power outages
Why don’t you save the last state in the ESP itself

Obviously you can code so save the state in memory and retrieve on boot

Why don’t you save the last state in the ESP itself

I like your idea, very much!!! And admit it never crossed my mind :thinking: Thank you for bringing that up!

However, I still like to keep things checked at the OH server. The whole idea is to have these nodes does basic tasks and do them very well. I don’t want to over complicate things at end-point, because these things go inside walls. Last thing I want to do is to go dig walls when something goes wrong :wink:

Also, I would like to learn how to read previousState of an item for future uses.

Adding to this, I once had to drop all the persistence tables and restart openhab to recreate them. I have a feeling that OH may still be looking in to those old tables for data, at least when it comes to persistance.

Something WILL go wrong. Don’t bury them in walls. Or leave an access point.

Im not burying them literally :slight_smile: But accessing them are not easy as sitting at my home and accessing OH over putty. In that sense, they are buried on the walls :wink:

Sometimes I’ve noticed the Rules DSL has problems recognizing when it is dealing with something where it needs to call toString to get the String version of that Object. Since previousState returns a HistoricItem, perhaps this is one of those cases. Try

logInfo("Persistance", my_item.previousState.toString)

If you are after the actual state, remember that you need to call

my_item.previousState.state

I tried this, and this is what I got in my openhab.log

2018-07-10 19:07:38.989 [ERROR] [org.knowm.yank.Yank                 ] - Error in SQL query!!!
java.sql.SQLException: Error preparing query: Table 'hal_db.item0085' doesn't exist Query: SELECT time, value FROM item0085 ORDER BY time DESC  LIMIT 0,1 Parameters: []

Then I looked in to MySQL and found NO Persistence tables exists!!! All of them were gone!
Not sure what going on here.