Item Previous State/Value

Had read some articles about an issue with using previous state/value of an item. Actually, WT.

See in events.log:
2020-09-12 12:22:43.349 [vent.ItemStateChangedEvent] - Boiler_Sensor_Input2Alarm changed from ON to OFF
2020-09-12 12:22:44.954 [vent.ItemStateChangedEvent] - Boiler_Sensor_SensorTemperature1 changed from 23.4 °C to 23.5 °C

So, it means, previous and new (current) states is clearly known right after new state is received, BOTH values passed to log writer. It means - both values MUST BE passed to rules with no problems, persistence and any other complication.

.previousState(true).state throw rrd4j errors.
.previousState().state - not working.

have code like
if(A>B){}
if(A<B){}
if(A=B){}
where A is Item.state, B Item.previousstate().state.

And… no one IF is matched.

openHAB 2.5.8 on Window 7.

Careful, there are two completely different things with similar names.

yourItemName.previousState()
is a persistence method. Nothing to do with logging or rule triggering.
It returns an historic state type object, which has both state and timestamp elements.


Don’t forget this cannot guess which persistence service to interrogate.

previousState
all on its own in a text rule is an implicit variable, conjured into existence by the rule triggering. But only if the trigger was for a change. This is a representation of the “from” in the events.log, and is a state object. No persistence service is involved here.

3 Likes

Confirm. Just checked - previousState variable contains previous state of Item.

Can I ask question ?

  • Can You point me some manual for this rules scripting language. Tried to check some references from Wiki - see no.
    Cause was surprised: state of temperature reading includes not only value, but also units and looks like “22.2 В°C” - then adding to log with .toString method. Have no idea ‘B’ means, actually. Item type is Number, dimension is Temperature.

Sorry, if asking dumb/stupid questions (how it maybe looks like) - just a newbie in dat area. Very appreciate any help and advice.

Temperature as Number solved in that way:

var TempBefore = (previousState as QuantityType).doubleValue
var TempBecome = (Boiler_Sensor_SensorTemperature1.state as QuantityType).doubleValue

logInfo(“test”,"changed from " + TempBefore + " to " + TempBecome)

Now getting in log just numbers as wanted to, as well as possible to compare etc.

Nope. Link to rules page already supplied. Mostly it is learn by example, many hundreds of examples in this forum.

However - the text based 'DSL" rules language is considered to be a legacy of openHAB-1. The “new rules engine” of openHAB-3 supports various other languages, jython seems to be popular. You might not want to learn DSL.

Yes, they call that a Quantity Type in openHAB. Again,many examples of how to deal with these e.g. comparisons often catch people out.

Me neither. Sounds like a character set issue with whatever you are viewing logs with. The actual state will be just 22.2 °C

1 Like