# Power Sum in a rule

Hallo, i have this items

``````Number        fv_potenza          "Potenza Istantanea FV [%.0f W]"  (gChartP)        { channel="knx:device:bridge:generic:fv_potenza" } //(gChart)
Number        potenza_w           "Potenza Istantanea Cr [%.0f W]"    (gChartP)             { channel="knx:device:bridge:generic:potenza_w" } //(gChart)
Number        pot_crfv            "Potenza Consum TOT [%.0f W]" <heating>
``````

I want a rule that consider this aritmethic operation

if - fv_potenza + potenza_w < 0
then pot_crfv = 0
else pot_crfv = - fv_potenza + potenza_w

I’m tryng in several and several modes but i cant understand
Now i’m lost in the scripts… 1 Like
``````rule "Power sum"
when
// some trigger
then
val sum = (fv_potenza.state * -1) + potenza_w.state
if(sum < 0) {
pot_crfv.postUpdate(0)
}
else {
pot_crfv.postUpdate(sum)
end
``````

or more concisely

``````rule "Power sum"
when
// some trigger
then
val sum = (fv_potenza.state * -1) + potenza_w.state
pot_crfv.postUpdate(if(sum < 0) 0 else sum)
end
``````

And in JSR223 Jython

``````from core.rules import rule
from core.triggers import when

@rule("Power sum")
@when("<some trigger>")
def power_sum(event):
sum = (items["fv_potenza"].floatValue() * -1.0) + items["potenza_w"].floatValue()
events.postUpdate("pot_crfv", DecimalType(0) if sum < DecimalType(0) else sum
``````

sorry. I try it but it doesn’t work

Please post what you tried. In particular, did you give your Rule a trigger? Did you save it to a .rules file?

Yes a did a potenzasomma.rules in rules
This is rule

``````rule "potenze"
when
Item mc_potenza_w changed
then
val sum = (fv_potenza.state * -1) + potenza_w.state
potenza_somma.postUpdate(if(sum < 0) 0 else sum)
end
``````

This is item

``````Number   potenza_somma "Potenza Consumo TOTALE PROVA TOT [%.0f]"
``````

this is sitemap

``````Text   item=potenza_somma

``````

Item `mc_potenza_w` is something you haven’t shown us before. Does it change? You’ll be able to see in `events.log`

Sorry i write wrong. it is “potenza_w”

``````rule "potenze"
when
Item potenza_w changed
then
val sum = (fv_potenza.state * -1) + potenza_w.state
potenza_somma.postUpdate(if(sum < 0) 0 else sum)
end
``````

Log Says

``````2019-07-31 00:22:01.888 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'potenze': Unknown variable or command '*'; line 5, column 16, length 21
``````

I think the rules interpreter doesn’t know what kind of thing the Item state is, so it doesn’t know what * is without context.
Try

`````` val sum = ( (fv_potenza.state as Number) * -1) + (potenza_w.state as Number)
``````

OOOOOh yes!
Now works. Thanks a lot