Help with variable declaration in rule

Can anyone please tell me what’s wrong with the following declaration?

var Number rainlatesthour = TellstickRainsensor_TotalRain.state - TellstickRainsensor_TotalRain.historicState(now.minusHours(1))

(TellstickRainsensor_TotalRain is an item of type number)

The log just tells me that The name '<XMemberFeatureCallImplCustom> - <XMemberFeatureCallImplCustom>' cannot be resolved to an item or type. I guess there is some variable type problem here that I don’t fully understand…

I think you’ll have to cast the state as decimal, like so:

var Number rainlatesthour = TellstickRainsensor_TotalRain.state as DecimalType -  TellstickRainsensor_TotalRain.historicState(now.minusHours(1)) as DecimalType


Tried that. Now I get this error instead (which says even less about what’s wrong):

Error during the execution of rule Calculate rain per hour: org.eclipse.smarthome.core.library.types.DecimalType

I tried splitting this up into multiple rows and now I’m down to this error:

The name '<XFeatureCallImplCustom>.minusHours(<XNumberLiteralImpl>)' cannot be resolved to an item or type.

It seems it doesn’t like minusHours()? I tried both with and without importing org.openhab.core.persistence.*. I have persistence set up and working and this device has values…

now.minusHours() ?

Note that .historicState will return a ‘historic Item’, not a state, when you get that far.

Oh, I forgot to mention. I’ve tried TellstickRainsensor_TotalRain.historicState(now.minusHours(1)).state as well, which gives me the exact same error.

Simplify further to see if your default persistence is working

logInfo(“test”, "Previous " + TellstickRainsensor_TotalRain.previousState.state.toString)

Yes, good thinking. I tend to forgot that possibility…

The result is that yes, persistence is working. But with a completely insane precision. I got “Previous 194.099999999999994315658113919198513031005859375”. The real reading said 194.1 (and also I checked in my mysql database, it also says 194.1) so some weird rounding seems to happen…

I also tried writing out the result of now.minusHours(1).toString to the log, it gives me “2017-02-17T21:25:00.579+01:00”, which seems quite correct to me. So it seems that now.minusHours(1) works perfectly fine, it just doesn’t give the type of value that the historicState() method expects…

Next try: TellstickRainsensor_TotalRain.historicState(parse(“2012-02-17”)).state. This one works. So why the heck doesn’t the combination historicState(now.minusHours(1)) work? I’m going to sleep now, maybe it works tomorrow…

Don’t get distracted by the numbers, you’d normally format before looking at them.

How often are you persisting, have you tried minusDays()?

Review your default persistence service setting, or specify

Mysql is my default persistence (at least according to the setting in paper ui, hope that’s the right place to check?). Also tried explicitly using mysql, made no difference.

I’m persisting on every change and once every midnight There hasn’t been any rain since I got those readings into the system, so the data for this item currently looks like this:

mysql> select * from Item28;
| Time                | Value |
| 2017-02-16 21:49:02 | 194.1 |
| 2017-02-17 00:00:00 | 194.1 |
| 2017-02-18 00:00:00 | 194.1 |
3 rows in set (0,00 sec)

I don’t know if this might be the problem? I thought that if I asked to get the historical state one hour back and the state hasn’t been persisted the last eleven hours it would automatically give me the last available state?

I’ve tried changing this item to persist every hour instead. Guess I’ll now soon if that makes any difference…

Two hours later: Darn. Nothing really goes my way. No matter what I do I can’t seem to make it persist more often. What is wrong with this?

Strategies {
    everyMinute : "0 * * * * ?"
    every5Minutes : "0 */5 * * * ?"
    everyHour   : "0 0 * * * ?"
    everyDay    : "0 0 0 * * ?"
    default = everyChange

Items {
    // persist all items once a day and on every change and restore them from the db at startup
    * : strategy = everyChange, everyDay, restoreOnStartup

    //Try persisting total rain every minute instead
    TellstickRainsensor_TotalRain : strategy = everyChange, everyMinute, restoreOnStartup

Gone beyond me now.
As .previousState does return an historic item, you could have a play and get the timestamp of that - make sure its not last year or something silly.

Ha! Think I’ve found one of the big culprits. I’m almost embarrased to admit it because it’s bloody stupid, but I realized I’ve used “now” as a variable name (for the variable holding the current value). So now I’m back to something that actually works! (although I’ve still got no idea why I didn’t succeed persisting the value more often, but that doesn’t really matter now)

The problem I’ve got now I think is related to rounding. I’m now down to this code:

var Number valuenow = TellstickRainsensor_TotalRain.state
var Number value1hago = TellstickRainsensor_TotalRain.historicState(now.minusHours(1)).state
var Number lasthour = valuenow - value1hago

logError("test", "Value one hour ago " + value1hago.toString)
logError("test", "Value now " + valuenow.toString)
logError("test", "Difference " + value1hago.toString)

…which gives the following in the log:

2017-02-18 13:48:00.483 [ERROR] [.eclipse.smarthome.model.script.test] - Value one hour ago 194.099999999999994315658113919198513031005859375
2017-02-18 13:48:00.488 [ERROR] [.eclipse.smarthome.model.script.test] - Value now 194.1
2017-02-18 13:48:00.493 [ERROR] [.eclipse.smarthome.model.script.test] - Difference 5.684341886080801486968994140625E-15

I know for sure the value hasn’t changed, it hasn’t rained for days (and the value in mysql is 194.1). Why does it do that? I guess I could go rounding the value back to 1 decimal, but I don’t understand why it isn’t correct in the first place…