Simple + calculation in items (?)

Hi, i have two photovoltaic power plants that i would like to see as a single value in the GUI…is there a very simple way to do such + calculation directly in items…so not to force another rule for this?
And if rule is required…how would that work? wait if one of the two is updated an do the calculation in another number variable?

Thanks, Norbert

You would need a rule if the inputs are the DecimalType states of two items, and the output is the sum of those two Number items:

items:

Number InputA
Number InputB
Number Total

rule:

import org.openhab.core.library.types.DecimalType // not needed on OH2
rule SumTwoInputs
when
  Item InputA changed or
  Item InputB changed
then
  if (InputA.state instanceof DecimalType && InputB.state instanceof DecimalType) {
    Total.postUpdate((InputA.state as DecimalType) + (InputB.state as DecimalType))
  } else {
    Total.postUpdate(Undefined)  // or UNDEF on OH2
  }
end

very cool, thanks Watou…this code makes my life easy.
I feared that the openhab concept would strictly split items from anything like calculation…

So this code also solves troubles through undefined/non-decimal values. great

1 Like