lastUpdate and previusState is the same

I’m trying to calculate the time difference between latest and previous update of an item. Using jdbc as persistence.

I cant understand why this doesn’t work, Why is the timestamp for lastUpdate and previusState the same?

var timeLast = triggeringItem.lastUpdate("jdbc") 
var timePrevius = triggeringItem.previousState(true, "jdbc").timestamp
logInfo("Energy","timeLast          :   {}", timeLast)
logInfo("Energy","timePrevius       :   {}", timePrevius)


2022-12-02 21:58:36.236 [INFO ] [org.openhab.core.model.script.Energy] - timeLast          :   2022-12-02T21:58:26.152+01:00[Europe/Berlin]
2022-12-02 21:58:36.240 [INFO ] [org.openhab.core.model.script.Energy] - timePrevius       :   2022-12-02T21:58:26.152+01:00[Europe/Berlin]

2022-12-02 21:59:16.231 [INFO ] [org.openhab.core.model.script.Energy] - timeLast          :   2022-12-02T21:59:16.145+01:00[Europe/Berlin]
2022-12-02 21:59:16.233 [INFO ] [org.openhab.core.model.script.Energy] - timePrevius       :   2022-12-02T21:59:16.145+01:00[Europe/Berlin]

I think because this doesn’t do what you expect it to do:

triggeringItem.previousState(true, "jdbc").timestamp

I assume that you think you’re getting the timestamp that was applied at the time of the previous state, but it seems to me that you’re just getting the timestamp of the triggering item, which changed at that exact moment (since it fired the rule). However, I don’t use JDBC persistence, so I could be wrong about that.

I think you need to intentionally save the previous timestamp in another item so that you can make your comparison.


That’s my conclusion also.

I switched to jdbc from rrd4j just to rule out any issues with rrd4j as it is a bit special when storing the data.

Well, I have thought of that too, but I would rather try to not go that way. That would increase the number of items quite a lot. And the information I’m looking for is in the database, would rather access it somehow.