Get the State before the last State

Hello All,

I have a question regarding the rules.

How can i find the state before the last state? or maybe three states in the past?

I don’t want to use time for it, but simple query to the persistence and get the values.

Is this possible?

Thanks all for the support.

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…

Many thanks for the reply…

I guess then i will need to control it via variables.

Hello,

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

and then at the end of each “if”

EZ_Esstisch_preprevious.sendCommand(EZ_Esstisch_previous)

EZ_Esstisch_preprevious is a string item:

String
    EZ_Esstisch_preprevious
    "EZ_Esstisch vorletzter Wert"

In my example the rule looks like:

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

I hope it helps.

Best regards
BrainPain

1 Like