historicState doesn't work

Hi !

I have a problem with historicState :frowning:

I have a function which calculate gas usage for last 10 minutes:

rule "Gaz 10min"
when    
    Time cron "0 0/10 * * * ?" 
then    
    {
    postUpdate(Gaz_10min,  (Gaz.deltaSince(now.minusMinutes(10))/1000) )
    logInfo("Gaz","Now: "+ Gaz)
    logInfo("Gaz", "20 min: "+ Gaz.historicState(now.minusMinutes(20)).state)
    logInfo("Gaz","180 min: "+ Gaz.historicState(now.minusMinutes(180)).state)
    logInfo("Gaz","1100min: "+ Gaz.historicState(now.minusMinutes(1100)).state)
    logInfo("Gaz","Gaz 10min: "+ Gaz_10min)
    }    
end 

Gaz is:

Number Gaz “Gaz [%d m\u00B3]” {onewire=“deviceId=1D.243D0A000000;propertyName=counters.B;refreshinterval=300”}

I save Gaz on rrdj4 persistence with everyminute strategy. I can see diff values under habmin, but when rule execute I got following result:

2016-07-05 10:53:07.866 [INFO ] [org.openhab.model.script.Gaz  ] - Now: Gaz (Type=NumberItem, State=138596)
2016-07-05 10:53:08.277 [INFO ] [org.openhab.model.script.Gaz  ] - 20 min: 138596
2016-07-05 10:53:08.689 [INFO ] [org.openhab.model.script.Gaz  ] - 180 min: 138596
2016-07-05 10:53:09.085 [INFO ] [org.openhab.model.script.Gaz  ] - 1100min: 138596
2016-07-05 10:53:09.296 [INFO ] [org.openhab.model.script.Gaz  ] - Gaz 10min: Gaz_10min (Type=NumberItem, State=0.00000000)

so whatever I put in Gaz.historicState(now.minusMinutes(20)) I always got same latest value. I have also checked with maximumSince and minimumSince and again same value.

any ideas why ?

Did you set up persistence for this item? Is it the default persistence Service?

Yes, as I wrote I set rrd4j persistence with everyminute strategy. I am also able to say chart and table with data under habmin.

Ok I found a reason why it doesn’t work, according wiki this persistence has to be default (but why ?).
Thanks Udo for suggestion.

I have changed default from mapdb to rrd4j, but now I lost possibility to “restoreonStartup” :frowning:
What I have configured is that rrd4j I use for data which I need charts or history, rest I assign to mapdb with everyChange and restoreOnStartup strategy. It was working ok but when I change default persistence service it looks it stopped.
Any ideas how 2 persistence can co-exist ?

I have also second problem with rule which is refreshed every 10 minutes and register gas usage:
rule “Gaz 10min”

when
Time cron "0 0/10 * * * ?"
then
if (Gaz.updatedSince(now.minusMinutes(10)))
postUpdate(Gaz_10min, (Gaz.deltaSince(now.minusMinutes(10))/1000) )
end

Gaz is config with rrd4j with everyminute strategy. Habmin shows proper values:

06:16:00 28 Jul 2016 141409
06:20:00 28 Jul 2016 141409
06:24:00 28 Jul 2016 141409
06:28:00 28 Jul 2016 141409
06:32:00 28 Jul 2016 141409
06:26:00 28 Jul 2016 141409

and at 6:30 I got an error:

2016-07-28 06:30:00.076 [ERROR] [.o.m.r.i.engine.ExecuteRuleJob] - Error during the execution of rule Gaz 10min
java.lang.NullPointerException: null
    at org.openhab.model.script.lib.NumberExtensions.operator_divide(NumberExtensions.java:46) ~[na:na]
    at sun.reflect.GeneratedMethodAccessor172.invoke(Unknown Source) ~[na:na]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0]
    at java.lang.reflect.Method.invoke(Method.java:483) ~[na:1.8.0]

What is funny, I got this error in very random way, sometimes after 2h sometimes after 20h

Any idea what is wrong ?

:slight_smile: Of course it needn’t to be default persistence, but if you want to query from a non-default persistence, you have to explicitly call this persistence, so use

when
    Time cron "0 0/10 * * * ?"
then
    if (Gaz.updatedSince(now.minusMinutes(10),"rrd4j"))
        Gaz_10min.postUpdate(Gaz.deltaSince(now.minusMinutes(10))/1000,"rrd4j")
end

instead.

1 Like

Udo, thanks !

That’s solved problem with restore onstartup.

Anyway still I got random errors like this:

2016-07-28 06:30:00.076 [ERROR] [.o.m.r.i.engine.ExecuteRuleJob] - Error during the execution of rule Gaz 10min
java.lang.NullPointerException: null
    at org.openhab.model.script.lib.NumberExtensions.operator_divide(NumberExtensions.java:46) ~[na:na]
    at sun.reflect.GeneratedMethodAccessor172.invoke(Unknown Source) ~[na:na]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0]
    at java.lang.reflect.Method.invoke(Method.java:483) ~[na:1.8.0]

Still can’t get rid of this problem :frowning:

> 2016-12-28 04:10:00.049 [ERROR] [.o.m.r.i.engine.ExecuteRuleJob] - Error during the execution of rule Gaz 10min
> 2016-12-28 05:00:00.117 [ERROR] [.o.m.r.i.engine.ExecuteRuleJob] - Error during the execution of rule Gaz Godz
> 2016-12-28 07:50:00.077 [ERROR] [.o.m.r.i.engine.ExecuteRuleJob] - Error during the execution of rule Gaz 10min
> 2016-12-28 09:20:00.052 [ERROR] [.o.m.r.i.engine.ExecuteRuleJob] - Error during the execution of rule Gaz 10min 

according habmin:
Gaz_10min:

03:56:00 28 Dec 2016 0
04:00:00 28 Dec 2016 0
04:04:00 28 Dec 2016 0
04:08:00 28 Dec 2016 0
04:12:00 28 Dec 2016 0

04:48:00 28 Dec 2016 0.016
04:48:00 28 Dec 2016 0.016
04:52:00 28 Dec 2016 0.0166666666666
04:56:00 28 Dec 2016 0.017
05:00:00 28 Dec 2016 0.0165

07:40:00 28 Dec 2016 0.014
07:44:00 28 Dec 2016 0.014
07:48:00 28 Dec 2016 0.014
07:52:00 28 Dec 2016 0.014

09:08:00 28 Dec 2016 0.009
09:12:00 28 Dec 2016 0.009
09:16:00 28 Dec 2016 0.009
09:20:00 28 Dec 2016 0.009
09:24:00 28 Dec 2016 0.009

and Gaz:

03:46:00 28 Dec 2016 212276
04:00:00 28 Dec 2016 212276
04:14:00 28 Dec 2016 212276.5714285

04:42:00 28 Dec 2016 212318
04:56:00 28 Dec 2016 212340.6428571
05:10:00 28 Dec 2016 212362.5714285

07:30:00 28 Dec 2016 212562.1428571
07:44:00 28 Dec 2016 212582.1538461
07:58:00 28 Dec 2016 212999.6428571

09:08:00 28 Dec 2016 212672.0714285
09:22:00 28 Dec 2016 212684.166666

Rules are:
> rule “Gaz 10min”
> when
> Time cron “0 0/10 * * * ?”
> then
> if (Gaz.updatedSince(now.minusMinutes(10),“rrd4j”))
> Gaz_10min.postUpdate(Gaz.deltaSince(now.minusMinutes(10),“rrd4j”)/1000)
> end

rule "Gaz Godz"
when	
	Time cron "0 0 * * * ?" 
then	
	if (Gaz.updatedSince(now.minusMinutes(60),"rrd4j"))
		Gaz_Godz.postUpdate(Gaz.deltaSince(now.minusMinutes(60),"rrd4j")/1000)		
end

How is this rrd4j works ?
Gaz is updated every 300s from onewire counter, then every 10minutes and every hour rule is executed. Randomly I got above errors.
Any ideas why ?