You can get the previousState or the previousState that is different from the current state.
MyItem.previousState // most recent value in the persisted database not counting the current state
MyItem.previousState(true) // most recent value in the persisted database that is different from the current state
Obviously, the above methods require persistence to be set up and configured.
If you have a changed trigger for your rule, there will be a previousState variable provided to your Rule that you can use.
Beyond that, you will have to keep track of the state changes yourself in Rules using Unbound/Virtual Items or global variables…
I found this post because I had the same problem and found a solution which I wanted to share. InfluxDB is used for persistence and I need the the actual state, the last state and the state before the last state.
variable with item.state and item.previousState at the beginning of the rule
var EZ_Esstisch_previous = EZ_Esstisch_dimmer.previousState(true,"influxdb").state as Number
var EZ_Esstisch_now = EZ_Esstisch_dimmer.state as Number
rule "EZ_Esstisch switch"
when
Item EZ_Esstisch_dimmer received command
then
val EZ_Esstisch_previous = EZ_Esstisch_dimmer.previousState(true,"influxdb").state as Number
var EZ_Esstisch_now = EZ_Esstisch_dimmer.state as Number
var EZ_Dimmer_time = 0
val Number EZ_max_dim = 7 //Zeit in Sekunden bis zur max. Helligkeit
if (receivedCommand == ON || receivedCommand == OFF) {
EZ_Esstisch_command.sendCommand(ON)
Thread::sleep(100)
EZ_Esstisch_command.sendCommand(OFF)
EZ_Esstisch_preprevious.sendCommand(EZ_Esstisch_previous)
end