Variable Maths

I have a number of temperature sensors around the house, in one form or another. What I’d like to do is find the average temperature for a given number of sensors. So for example I could display the average Ground floor temperature by dividing the sum of all the temperature sensors on the Ground floor. I know is not overly scientific but it would suit my requirements.

Is there a way of doing maths against variables? Examples would be greatly appreciated. I’ve tried searching the forum but haven’t managed to find what I’m looking for.

Thanks

Garry

Is there a reason you are avoiding using the averaging capability of OH Groups?

Perfect. Thanks for pointing me in the right direction, that’s all I needed.

:+1:

You can do arithmetic in Rules, I know little about that and crib from other people’s examples (typically temperature and other sensor reading manipulations). More exotic maths (trigonometry etc.) requires imports of maths libraries I believe. Example

Though be aware that there is a potential bug with Groups and AVG when using percents. It should work with temperatures though.

If you wanted to do it in a rule instead (perhaps you do have percents) I’d recommend:

  • put all the temps for a floor in a Group

  • create an Item to store the average

  • create a rule that triggers when the Group received update

    val sum = MainFloorTemps.members.map[state as Number].reduce[s, val| s = s+val]
    MainFloorAvg.postUpdate(sum / MainFloorTemps.members.size)

See Working with Groups in Rules for a brief explanation and example of map and reduce.