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")
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
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.
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")
}
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.