[SOLVED] How to compare the actual value of an item with its previous value

I try to compare a Number item (=temperature of watertank) with its previous value. I have mapdb and rrdj configured for persistence and can draw a chart for that item. (should prove that persistence is working fine)

this is the rule that does not work:

rule "watertank is heating"
    when 
        Item Heizpumpe_t2 changed
    then
            logInfo("Watertank temp actual: " + Heizpumpe_t2.state +"°", "previous: " + Heizpumpe_t2.previousState().state + "°")
            if (Heizpumpe_t2.state > Heizpumpe_t2.previousState().state as DecimalType) {
                Boilerheating.sendCommand(ON)
            }
            else Boilerheating.sendCommand(OFF)           
    end
2019-01-05 12:40:34.653 [vent.ItemStateChangedEvent] - Heizpumpe_t2 changed from 49.1 to 48.9
2019-01-05 12:40:39.372 [INFO ] [.script.watertank temp actual: 48.9°] - previous: 48.9°

funny that this one works:

Heizpumpe_t2.historicState(now.minusMinutes(6)).state
2019-01-05 13:22:07.243 [INFO ] [.script.Watertank temp actual: 47.0°] - previous: 47.1°

can someone explain whats wrong with my previousState?

That’s because the state has already changed in the pressitence database when you do the query
As you are using the changed trigger you will be able to use the previousState implicit variable.

rule "watertank is heating"
when 
    Item Heizpumpe_t2 changed
then
    logInfo("Watertank temp actual: " + Heizpumpe_t2.state +"°", "previous: " + previousState + "°")
    if (Heizpumpe_t2.state > previousState) {
        Boilerheating.sendCommand(ON)
    } else {
        Boilerheating.sendCommand(OFF)
    }
end

thanks,

if (Heizpumpe_t2.state > previousState as DecimalType ) {

works!