[SOLVED] Value and timestamp of last and previous measurement

Hello all,
What is the code in a rule to get the following information from an influxdb database?

  • Value latest update
  • Timestamp latest update
  • Value previous latest update
  • Timestamp previous latest update
  • Value previous previous latest update
  • Timestamp previous previous latest update

I want these in a variable to do some calculations.
Extra related question : when the latest update was the same value, how is this related to the timestamp?

Kind regards, Hans

Easy that’s the current value

Item.lastUpdate returns timestamp of last update

Item.previousState.state

Item.previousState.getTimeStamp

Not possible AFAIK

Thanks for your quick answer, but I’m still getting problems reading out and do some calculation.
I tried with the code from your reply, but I get some errors. Searching the internet I found some working but the Item values are many times the same, and timing also. I want the timing in milliseconds.

logInfo(“Test”,“Item_last: {}”,Item.state)
logInfo(“Test”,“Item_-1: {}”,Item.previousState.state)
logInfo(“Test”,“Item Time_last: {}”,Item.lastUpdate.millis)
logInfo(“Test”,“Item Time_-1: {}”,Item.previousState.timestamp.time)
logInfo(“Test”,“Timediff: {}”,Item.previousState(false).timestamp.time-Item.lastUpdate.millis)

This code is in a rule when Itemvalue changed, so when there is an update with same value, this code is not executed. Regarding to the previousState(false) or previousState(true) question.
Regards, Hans

Item.lastUpdate will use whatever database you have defined as default.
You need Item.lastUpdate(“influxdb”) if the default is different.

The database is not the problem, I’m receiving data but wrong values…
2018-11-28 15:45:34.608 [vent.ItemStateChangedEvent] - Item changed from 369.905 to 371.877

==> /var/log/openhab2/openhab.log <==
2018-11-28 15:45:34.714 [INFO ] [.eclipse.smarthome.model.script.Test] - Item_last: 371.877
2018-11-28 15:45:34.765 [INFO ] [.eclipse.smarthome.model.script.Test] - Item_-1: 371.877
2018-11-28 15:45:34.811 [INFO ] [.eclipse.smarthome.model.script.Test] - Time_last: 1543416334593
2018-11-28 15:45:34.854 [INFO ] [.eclipse.smarthome.model.script.Test] - Time_-1: 1543416334593
2018-11-28 15:45:34.913 [INFO ] [.eclipse.smarthome.model.script.Test] - Time: 0

I think I found the correct syntax:

logInfo("Test","Item_last:   {}",Item.state)
logInfo("Test","Item_-1:     {}",Item.previousState(true).state)
logInfo("Test","Time_last:    {}",Item.lastUpdate.millis)
logInfo("Test","Time_-1:      {}",Item.previousState(true).timestamp.time)
logInfo("Test","Time_diff:     {}",Item.lastUpdate.millis-Item.previousState(true).timestamp.time)

I found out that when this request is at the beginning in a rule when a value changed and is logged with influxdb, sometimes the reading is wrong and last and previous value is the same. Probably a timing issue, reading too quickly while database is still getting updated. When I request it two times the second one is always correct and the first more not. Can I add a little wait or delay in the rule, or just request two times the same and use the second one?

anyone know how to read influxdb item info like daily average or 1 to 2 hours ago? as I would like the rule to perform as if current value higher than daily average like 5-10%, than don’t execute or execute command.