Simple subtraction rule mostly (not always!) returning 0

Hi All,

I’ve noticed a strange arithmetic problem with decimals which started when I upgraded from 2.5M1 to 2.5M2. Hoping someone can shed some light!

I have a simple rule set to detect changes in humidity. If the humidity increases more than 4.5%, then turn on the bathroom fan. Prior to upgrading, this worked well, but ever since M2, the calculation no longer works.

I started logging the results and noticed the rule is returning a value of ‘0,’ even when the result should be a decimal. In the 2nd row of the log below, the previous value was 78.57 and the new value is 80.80, so it should have returned 2.23. However, the log shows ‘0’

Thanks!

rule "Shower Lights & Fan"
when
   Item HumiditySensorCurrentLevel changed 
then
	if (((HumiditySensorCurrentLevel.state as Number)-(HumiditySensorCurrentLevel.previousState.state as Number)) > 4.5)
	{
	   ShowerStatus.sendCommand(ON)
	   FanStatus.sendCommand(ON)
	}
	logInfo("Shower auto lights", "Humidity Difference - New: " + HumiditySensorCurrentLevel.state as Number + 
" -- Previous: " + HumiditySensorCurrentLevel.previousState.state as Number + " -------- " + ((HumiditySensorCurrentLevel.state as Number)-(HumiditySensorCurrentLevel.previousState.state as Number)) )  

end	
2019-11-07 20:17:48.408 [INFO ] [home.model.script.Shower auto lights] - Humidity Difference - New: 79.28 -- Previous: 57.57 -------- 21.71
2019-11-10 19:46:00.637 [INFO ] [home.model.script.Shower auto lights] - Humidity Difference - New: 78.57 -- Previous: 78.57 -------- 0.00
2019-11-10 19:51:26.892 [INFO ] [home.model.script.Shower auto lights] - Humidity Difference - New: 80.80 -- Previous: 78.57 -------- 0.00
2019-11-10 20:07:03.701 [INFO ] [home.model.script.Shower auto lights] - Humidity Difference - New: 79.20 -- Previous: 79.2 -------- 0.00
2019-11-10 20:13:04.997 [INFO ] [home.model.script.Shower auto lights] - Humidity Difference - New: 75.98 -- Previous: 79.2 -------- 0.00
2019-11-10 20:22:57.496 [INFO ] [home.model.script.Shower auto lights] - Humidity Difference - New: 72.84 -- Previous: 72.84 -------- 0.00
2019-11-10 20:43:24.333 [INFO ] [home.model.script.Shower auto lights] - Humidity Difference - New: 69.63 -- Previous: 72.84 -------- -3.21

First I’m going to show how to figure out how to figure out what is going on. Then I’ll show you the “correct” way to implement this Rule.

Just to clear up some potential issues that may come into play between the if statement and the log statement, make sure you are logging the result of the “real” calculation.

then
    val curr  = HumiditySensorCurrentLevel.state as Number
    val prev = HumiditySensorCurrentLevel.previousState.state as number
    val delta = curr - prev
    val threshold = 4.5

    if(delta > threshold){
        // blah blah blah
    }
    logInfo("Shower auto lights", "Humidity Difference - New: " + curr + " -- Previous: " + prev +" ---------" + delta)
end

With this you will find that all of those 0.00 deltas you are logging are in fact 0.00s. Why? Because previousState goes into persistence to pull the most recent value from the database. Storing a value to a database and retrieving it takes some time. And storing the value to the database is going on at the same time that your Rule is running. So it’s just a trick of the timing whether the previousState you are pulling out of the database is actually the previous state or it’s the current state. And it’s highly likely that you will get a different value the second time you call previousState for the log statement from the first time you call it in the if statement.

That’s why it’s important to perform your calculation once and then log out the result in order to really know what is going on.

Now for the “correct” way to implement this Rule. Just use the previousState implicit variable. You’re Rule is triggered with a changed trigger so the previous state the Item was in is available in a variable, no persistence required.

then
    val curr = HumiditySensorCurrentLevel.state as Number
    val delta = curr - (previousState as Number)
    ...
2 Likes

Thanks @rlkoshak! Makes sense!

I’ll give it a shot and report back if there are any problems. Appreciate your help!