Get Item Previous State

Hello all, I need to get the previews state of a item, but ir seams that is not working as it should.

I have a MySQL persistent, and I have this rule:

rule "Switch"  
    when
        Item FF_LivingRoom_Switch1 received command
    then 
        
        logInfo("Prev", FF_LivingRoom_Switch1.previousState.state.toString);

end

When I toggle the switch

I get random values on the LOG, it dos not match the prev state, somtimes I change from OFF to ON, for exemple, I should get OFF, and I get ON, and then I switch back to OFF and get ON again, and and then again to ON and ON again… Then I get a sequence on a row that works, only to go back again to errors… Not reliable at all… This really neads to be on the “received command”, because I have some bindings that only change the state (mqtt)

Thank You

1 Like

Ok I made some progress does get exactly the previews state…

rule "Switch"  
    when
        Item FF_LivingRoom_Switch1 received command
    then 
            logInfo("Prev", FF_LivingRoom_Switch1.previousState(true, "mysql").state.toString() + " <> " + FF_LivingRoom_Switch1.state.toString());
end

The problem is when the value comes from MQTT…

The Item:

Switch                    FF_LivingRoom_Switch1          "Interruptor 1"            <light>           (FF_gSwitch, gSwitch)                                              {mqtt="<[mymqtt:mygateway1-out/1/10/1/0/2:command:MAP(1on0off.map)]"} 

The switch is ON… and I submit 3 times the message with “0” I get the log:

02:55:21.328 [INFO ] [smarthome.event.ItemCommandEvent     ] - Item 'FF_LivingRoom_Switch1' received command OFF
02:55:21.330 [INFO ] [smarthome.event.ItemStateChangedEvent] - FF_LivingRoom_Switch1 changed from ON to OFF
02:55:21.332 [INFO ] [home.event.GroupItemStateChangedEvent] - gSwitch changed from ON to OFF through FF_LivingRoom_Switch1
02:55:21.334 [INFO ] [g.eclipse.smarthome.model.script.Prev] - ON <> OFF
02:55:21.855 [INFO ] [smarthome.event.ItemCommandEvent     ] - Item 'FF_LivingRoom_Switch1' received command OFF
02:55:21.859 [INFO ] [g.eclipse.smarthome.model.script.Prev] - ON <> OFF
02:55:22.399 [INFO ] [smarthome.event.ItemCommandEvent     ] - Item 'FF_LivingRoom_Switch1' received command OFF
02:55:22.403 [INFO ] [g.eclipse.smarthome.model.script.Prev] - ON <> OFF

As you can see… The item switches from ON to OFF… as it should… but next I keep getting ON <> OFF when I should get OFF <> OFF on the 2nd and 3rd

So now works on the switch… but does not work on the MQTT

Thank you

Ok I found the Solution :slight_smile:

Updating to help any one else:

The persistence (the secret is everyUpdate)

Items {
	   gSwitch*      : strategy = everyUpdate
}

the rule:

rule "Switch"  
    when
        Item FF_LivingRoom_Switch1 received command
    then 
        
            logInfo("Prev", FF_LivingRoom_Switch1.previousState(false, "mysql").state.toString() + " <> " + FF_LivingRoom_Switch1.state.toString());
        
 
end

Hope it helps someone else :slight_smile:

2 Likes