# Variable Math Not Working?

It appears the set variable to itself/2 action is not working correctly:

n-2FreezerTemp = 4.14
n-1FreezerTemp = 3.17
n-1FreezerTemp + currentFreezerTemp = 7.31
avgFreezerTemp = (n-1FreezerTemp + currentFreezerTemp) / 2 = 3.65 NOT 7.31

Please keep in mind that the screenshot with avgFreezerTemp = 7.31 is taken after the calculation. Therefore, n-1FreezerTemp should be substituted for currentFreezerTemp and n-2FreezerTemp for n-1FreezerTemp.

Here is a rule that I have for averaging 2 illuminance sensors.
It's working OK for me.
I'm on 2.2.8 but I don't think that is the issue.

Summary

Can you modify your rule to use 'variable = variable / 2' to see if it still works?

I'll do a clone and modify.

Oops did something wrong. Ignore comment. (Now deleted).
Well something is going wrong.
The way I have it, it looks like it is dividing by 4 not 2.

Summary

What are the delays for?

It would be helpful if you would replace those with Log actions that show the progressive value of sum. And, turn on Action Logging, so that the actual values can be seen in the logs, not just the rule display.

My original rule is working just fine and I'm not having any issues.
I put the delays in to just put a gap in between each variable change for my piece of mind. Just me I suppose.
The rule that is failing was at the request of the OP as he wanted me to try something as his rule does not work.
Maybe have a look at the original post to see if you can help with his issue.

This works as expected:

Note that the type of your local variable is Number, OP’s variable is Decimal.

Yeah, I tested with decimal also. Had three decimal Hub Variables, put values in two of them, summed those to the third, and then divided the third by 2. All work as expected.

That's interesting. Are these Hub Variables or Local Variables?

Would you please post the Settings portion of the App Status page (gear icon).

avgFreezerTemp and currentFreezerTemp are local variables.

n-2FreezerTemp and n-1FreezerTemp are global variables.

Is this a Rule-4.1 rule?

Please rebuild it as Rule 5.0 and see if that works.

It appears to be Rule-4.0

When 2.2.8 stabilizes and I upgrade, I will try this.

2.2.8 is done. We've moved on.

Turns out it was a borked rule - reason unknown.

I duplicated a similar rule that was functioning properly, modified it to include the variable = variable/2 logic and it appears to be working.