Get difference between current value and previous value

I have a gas meter which reports the cumulative meter value every minute. I now would like to create a new item which logs the difference between the current and previous value.

Something like this:

rule "Gas consumption"
when
    Item GasmeterLastValue changed
then
    var Number currentStateValue = GasmeterLastValue.state
    var Number previousStateValue = GasmeterLastValue.previousState.state
    var Number diff = currentStateValue - previousStateValue
    GasmeterDiscreteValue.postUpdate(diff)
end

But this is apparently not the correct syntax. Could I get some help with this?

Hi,

I think you’re looking for

rule "Gas consumption"
when
    Item GasmeterLastValue changed
then
    var Number diff = newState - previousState
    GasmeterDiscreteValue.postUpdate(diff)
end
2 Likes