Access last value of an item in DSL rule

Splash screen

I have a setpoint for a thermostat that is changed throughout the day. I would like to have a different rule action based on whether the last change was up or down. I have rrd4j installed and am using DSL rules.

What I do not know is possible is to extract the prior value of an item in a rule. Something like;

when
    Item MainThermostat14_SensorTemperature changed
then
     var  tempnow = MainThermostat14_SensorTemperature.state as QuantityType<Temperature>).toUnit("°F")
     var tempold = MainThermostat14_SensorTemperature.previousstate as QuantityType<Temperature>).toUnit("°F")

Is something like that possible?

Bob

You might look at the implicit variable previousState, which does not need any persistence.

Thanks ! Let me see if I can make that work.

Bob

It took a few tries to see what previousState returned when attached to the Setpoint and it seems it is mostly a date and not temperature (and the temperature was not the previous 76 F). What I tried;

    val setpointnow = MainThermostat14_SetpointCooling.state.toString
    val setpointold = MainThermostat14_SetpointCooling.previousState.toString
//    val setpointdiff = (setpointnow - setpointold)
    logWarn("events", "setpointold {}, setpointnow {}",setpointold,setpointnow)

What I get is
2021-12-14 15:17:02.349 [WARN ] [org.openhab.core.model.script.events] - setpointold 14/12/2021, 15:17: MainThermostat14_SetpointCooling -> 75 °F, setpointnow 75 °F

Am I missing something basic?
Bob

This is not the implicit var!

    val setpointnow = newState
    val setpointold = previousState
//    val setpointdiff = (setpointnow - setpointold)
    logWarn("events", "setpointold {}, setpointnow {}",setpointold,setpointnow)

That’s all!

Or even more simple:

    logWarn("events", "old value: {}, new value: {}",previousState,newState)

Please be aware that both previousState and newState implicit var are only available for a rule which is triggered through a changed event.

1 Like

That goes and fetches the last entry from your persistence service. That’s likely to be the same as the current Item state, depending on exactly when you persisted it.

You don’t need to wrestle with that.

The trigger is

when

    Item MainThermostat14_SetpointCooling changed

so the simplier suggestion should work. I was never very clear on what implicit var means.

ok let me try some more.

Bob

Ok that works, so marked as solution (see first line below). Now I’m having trouble with the math (minus sign) as in the commented out line above. I’ve tried both val and var

2021-12-14 15:48:06.845 [WARN ] [org.openhab.core.model.script.events] - setpointold 75 °F, setpointnow 76 °F
2021-12-14 15:50:07.449 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'refresh.rules'
2021-12-14 15:50:26.180 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'refresh-3' failed: Unknown variable or command '-'; line 42, column 25, length 25 in refresh

Any idea why?

Bob

Edit: Never mind, let me try another way.
Edit2: Just jumped to what I said in post #1

with

    if( newState >= previousState ) {
        logWarn("events", "New State Hotter")   
    }
    else {
        logWarn("events", "New State Colder")
    }

and that works fine. Thanks again!

1 Like

You didn’t show us the math, so that makes it harder. At a guess, you tried to subtract states. Or perhaps ‘state as Number’ - that will mess up too, because your states are Quantities, with units.

1 Like