Persist strategy "everyChange" incl. the last known value before change

Then it’s exactly the same as above. You don’t even need to change the Rule code.

I get an error in the rule:

2020-03-29 22:50:42.101 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule ‘Persist SwitcherCurrent’: ‘update’ is not a member of ‘org.eclipse.smarthome.core.library.items.NumberItem’; line 67, column 5, length 78

Typos, they should be postUpdate

after change to postUpdate I now get this error:

2020-03-31 20:37:44.362 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule ‘Persist SwitcherCurrent’: cannot invoke method public abstract org.eclipse.smarthome.core.types.State org.eclipse.smarthome.core.persistence.HistoricItem.getState() on null

item persisted_SwitcherCurrent is not in .persist file, only SwitcherCurrent
rule:

rule "Persist SwitcherCurrent"
when
    Item SwitcherCurrent received update
then
    persisted_SwitcherCurrent.postUpdate(SwitcherCurrent.previousState.state.toString)
    persisted_SwitcherCurrent.persist
    persisted_SwitcherCurrent.postUpdate(SwitcherCurrent.state.toString)
    persisted_SwitcherCurrent.persist
end

items:

Number SwitcherCurrent
Number persisted_SwitcherCurrent

inside infuxdb.persist:

	SwitcherCurrent : strategy = everyChange

Have you successfully persisted any data yet?

I’m not sure what your rule is supposed to do, but note that postUpdate, and I think persist, are asynchronous. The rule will not stop and wait for that action to complete before moving on to following lines.

I’m also worried a bit about the granularity of whichever database you are using; some will regard two updates within a second as being the same moment in time I think.

The rule was a chart display workaround suggested by @rlkoshak to solve the issue described in the first post of this thread.

It won’t work unless you have. You can check with the REST API

Sorry I don’t follow.
I have persistence working ok.
As @rlkoshak noted above about the item:

Make sure to exclude persisted_mySwitch from your .persist file so it doesn’t get persisted automatically.

The first thing your rule does is attempt to get a persisted state. If you have never yet previously stored any data, that will return null. Your rule does not deal gracefully with that situation, and fails with an error. Because it fails, it never gets to the part where it persists data. The next time you run the rule, you still have no stored data …

Rich’s rule suggestion was a guide, it needs work to make it practical.

Understood but still, I have data persisted successfully for the item “SwitcherCurrent”. I have a chart for it.
How do you suggest I make this work? My understanding here is at it’s limit.

That’s why I asked in the first place.

So, there’s a mystery. Why are you getting

Rule ‘Persist SwitcherCurrent’: cannot invoke method public abstract org.eclipse.smarthome.core.types.State org.eclipse.smarthome.core.persistence.HistoricItem.getState() on null

on your rule line

persisted_SwitcherCurrent.postUpdate(SwitcherCurrent.previousState.state.toString)

which can null be due to lack of data, but you’ve now said you have data.

Next up, the method is using the system’s default persistence service.
Perhaps you have not set the default yet, perhaps it is not set to the service that your data is in.

Give it some guidance. Are you using influx, you haven’t saiid?
Example

persisted_SwitcherCurrent.postUpdate(SwitcherCurrent.previousState("influxdb").state.toString)

although I would open that up, get the previoustate first and see if it is null before trying to do toString etc. on it, so that your rule behaves better.

The rule below gives an the error:

Rule 'Persist Switcher Current': cannot invoke method public abstract org.eclipse.smarthome.core.types.State org.eclipse.smarthome.core.persistence.HistoricItem.getState() on null
rule "Persist Switcher Current"
when
    Item SwitcherCurrent received update
then
    persisted_SwitcherCurrent.postUpdate(SwitcherCurrent("influxdb").previousState.state)
end

Well, gee, I guess you’ll have to debug it a bit.

How about breaking it down into steps, and finding out which bit goes wrong?

Or you could look at my previous posting, and observe the difference.

I am lost here. Don’t really understand what should be the next step.
I tried other methods, searched additional threads which seem to hit the same point. Still no solution.
Here is my latest try:

rule "Persist Switcher Current"
when
    Item SwitcherCurrent changed
then
    persisted_SwitcherCurrent.postUpdate(SwitcherCurrent.previousState(now.minusSeconds(5)).state.toString)
    persisted_SwitcherCurrent.persist("influxdb")
    persisted_SwitcherCurrent.postUpdate(SwitcherCurrent.state.toString)
    persisted_SwitcherCurrent.persist("influxdb")
end

log:

[vent.ItemStateChangedEvent] - SwitcherStatus changed from ON to OFF
[vent.ItemStateChangedEvent] - SwitcherCurrent changed from 10.9 to 0.0
[ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Persist Switcher Current': An error occurred during the script execution: Could not invoke method: org.eclipse.smarthome.model.persistence.extensions.PersistenceExtensions.previousState(org.eclipse.smarthome.core.items.Item,boolean) on instance: null

When I change the first rule line to:

persisted_SwitcherCurrent.postUpdate(SwitcherCurrent.state.toString)

I see in the log that the item persisted_SwitcherCurrent receives the same value as SwitcherCurrent.

You seem to have ignored the stuff about default persistence service.
When you use a Item’s persistence method (like .previousState()) it obviously has to find which persistence service to use - you can have several.

If you don’t tell it which one explicitly, it will use the default. If you haven’t set a default either, it ain’t gonna work.

You haven’t told us what your system default is set to, if anything, so I cannot tell if that is any use for you.

You haven’t told us what persistence service you think has your data.

You did copy-paste “influxdb” (albeit to the wrong place) so maybe I guessed correctly? I don’t know. You’re not giving the info.

Okay, let’s look at your most recent rule.
persisted_SwitcherCurrent.postUpdate(SwitcherCurrent.previousState(now.minusSeconds(5)).state.toString)

No explicit persistence.
So it’ll use the default. What is that? What should it use? Do you need to be explicit here?
The docs, with how to be explicit -

Sorry I did not mention it before. It was already set from the beginning.
My system default is set to mapdb.
Still the rule below outputs the error.

rule "Persist Switcher Current"
when
    Item SwitcherCurrent changed
then
    persisted_SwitcherCurrent.postUpdate(SwitcherCurrent("influxdb").previousState(true).state.toString)
    persisted_SwitcherCurrent.persist("influxdb")
    persisted_SwitcherCurrent.postUpdate(SwitcherCurrent("influxdb").state.toString)
    persisted_SwitcherCurrent.persist("influxdb")
end
Rule 'Persist Switcher Current': cannot invoke method public abstract org.eclipse.smarthome.core.types.State org.eclipse.smarthome.core.persistence.HistoricItem.getState() on null

I’m not at all surprised. I’ve given an example and pointed to the docs, but you’re doing it wrong still

SwitcherCurrent is your Item.
Your Item has no argument, so SwitcherCurrent("influxdb") is meaningless.
Meantime, .previousState(true) has no explicit service so will go to your default.
It’s the previousState that needs the guidance.
.previousState(true, "influxdb")

1 Like

Oh, f**k, I just now noticed the difference and corrected it.
Note if I write anything else other than “true” I get the same null error.

rule "Persist Switcher Current"
when
    Item SwitcherCurrent changed
then
    persisted_SwitcherCurrent.postUpdate(SwitcherCurrent.previousState(true, "influxdb").state.toString)
    persisted_SwitcherCurrent.persist("influxdb")
    persisted_SwitcherCurrent.postUpdate(SwitcherCurrent.state.toString)
    persisted_SwitcherCurrent.persist("influxdb")
end

I don’t think, in the case of previousState() you can write anything else than true. The docs I pointed to suggest these would be valid -
....previousState(true, "influxdb")...
or
....previousState("influxdb")...

The first fetches the most record that is different from current state.
The second just fetches the most recent record.

What are you trying to put in there?

I wanted to play with it to observe the effect on the chart.
When I tried removing the “true” leaving only “influxdb” I get the null error.
The purpose is to insert a “0” value in time just before the latest updated value which is not “0” so that when ploting the chart is will show a nice step from 0 to some other value and not an inclined line (see first post in this thread).