Hi,
I read several posts regarding the previousState() issue but couldn’t find one which solves my issue.
In my case previousState() return everytime the current state and not the previous state.
rlkoshak wrote in one of his posts:
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
My understanding is that previousState() returns the second value in the DB table.
Let me give you an example:
Item: Heating_Bedroom_Mode
1. Step
Inital DB entries.
2020-05-18 14:14:14.000 1.0
2020-05-18 15:15:15.000 2.0
Return values:
Heating_Bedroom_Mode.state —> “2.0”
Heating_Bedroom_Mode.previousState().state —> returns “2.0” (expected “1.0”!)
2. Step
sendCommand(Heating_Bedroom_Mode, "3.0)
2020-05-18 14:14:14.000 1.0
2020-05-18 15:15:15.000 2.0
2020-05-18 16:16:15.000 3.0
Return values:
Heating_Bedroom_Mode.state —> “3.0”
Heating_Bedroom_Mode.previousState().state —> “3.0” (expected “2.0”)
Do I have another understanding of the function previousState() and is this the normal behavior or is it a bug?
Here is my setup where I’m facing the issue:
Items
Number Heating_Bedroom_Mode "Mode" <fire> (gHeatingMode, Bedroom) {channel="knx:device:bridge:actuatorAKH080002:channelDMode"}
Script:
rule "Heating: Deactivate during airing"
when
Member of gContactsWindowsAndDoorsWithoutEntrance changed
then
try {
if(Setting_Heating_DeactivateInRoomWhileAiring.state == ON) {
val room = transform("REGEX", "Homematic_Sensor_([^_]*).*", triggeringItem.name )
val item = gHeatingMode.members.filter[ i | i.name.contains(room) ].last
if(triggeringItem.state == OPEN) {
sendTelegram('bot1', "Window in room: "+room+" has been opened.");
sendCommand(item, "4.0")
} else if(triggeringItem.state == CLOSED) {
sendTelegram('bot1', "Window: "+room+" has beend closed. Current state: "+item.state.toString+". Restore previous state: "+item.previousState().state.toString);
sendCommand(item, item.previousState().state.toString)
}
}
}
catch(Throwable t) {
sendTelegram('bot1', "Error during execution rule: 'Heating: Deactivate during airing'. \n\r\Error:\n\r\n" + t.toString);
}
end
Platform information:
- Hardware: Synology Diskstation DS918+
- Java Runtime Environment: 1.8.0_202
- openHAB version: 2.5.1
- Persistence: MariaDB (JDBC)