OH1: rule error (item not defined)

I have this rule:

// ----- 170406 MaxG; total grid profit / loss
rule "Grid profit and loss12"
  when
    Item spp_Date changed or
    //         s m   h D M Dow Y
    Time cron "0 0/1 * * * ? *"
    // here every minute
  then
    logInfo("ProfitLossTotal.rule", "spp_ProfitLoss...............: {}", spp_ProfitLoss)
    var Number spp_ProfitLossTotal.state = 9.96
    //spp_ProfitLossTotal.state = 9.96
    logInfo("ProfitLossTotal.rule", "spp_ProfitLossTotal.1........: {}", spp_ProfitLossTotal)
    spp_ProfitLossTotal.postUpdate(spp_ProfitLossTotal += spp_ProfitLoss)
    logInfo("ProfitLossTotal.rule", "spp_ProfitLossTotal.2........: {}", spp_ProfitLossTotal)
end

Since the spp_Date only changes once every morning around 0600, I wanted to see, if my rule actually works, so I added the cron definition.

Since spp_ProfitLossTotal is uninitialised (first use ever; it is defined in .items as Number) I set it to 9.96 – so I do not need an if(null) statement.
Once the rule would have run once, I would remove this line as ProfitLossTotal is now initialised and persisted.

What the rule should do: if the spp_date has changed add to spp_ProfitLossTotal the value of spp_ProfitLoss – to tally the daily ProfitLoss.

When I use

    spp_ProfitLossTotal.state = 9.96

I get this error:

// 2017-04-07 10:13:00.364 [INFO ] [.m.script.ProfitLossTotal.rule] - spp_ProfitLoss...............: spp_ProfitLoss (Type=NumberItem, State=-0.59)
// 2017-04-07 10:13:00.544 [ERROR] [.o.m.r.i.engine.ExecuteRuleJob] - Error during the execution of rule Grid profit and loss10
// java.lang.IllegalStateException: Could not invoke method: org.openhab.core.items.GenericItem.setState(org.openhab.core.types.State)
// on instance: spp_ProfitLossTotal (Type=NumberItem, State=Uninitialized)

When I use:

    var Number spp_ProfitLossTotal.state = 9.96

I get no error, but since the two following log entries do not occur, I know that the rule is not continuing.

The issue I see with the line above is, it may be a double definition, since spp_ProfitLossTotal is defined as an item. I certainly can’t get rid of it, as I need it to display on the sitemap.

I fI use:

    spp_ProfitLossTotal.postUpdate(new DecimalType(9.96))

I get:

2017-04-07 10:35:00.027 [INFO ] [.m.script.ProfitLossTotal.rule] - spp_ProfitLoss...............: spp_ProfitLoss (Type=NumberItem, State=-0.59)
2017-04-07 10:35:00.066 [INFO ] [.m.script.ProfitLossTotal.rule] - spp_ProfitLossTotal.1........: spp_ProfitLossTotal (Type=NumberItem, State=9.96000000000000085265128291212022304534912109375)
2017-04-07 10:35:00.188 [ERROR] [.o.m.r.i.engine.ExecuteRuleJob] - Error during the execution of rule Grid profit and loss12
java.lang.RuntimeException: The name '<XFeatureCallImplCustom> += <XFeatureCallImplCustom>' cannot be resolved to an item or type.

This is one of those rules, which are dead simply, yet puzzle me to the point of annoyance, because it does not work.

I just noticed this:

on my sitemap; can’t tell though which initialisation made it happen…

However, it still has an error in the rule, because it set it to 9.96, but did not add up…as the result should 9.37

This works, however, is this the effort I have to go through with OH to add two numbers??

// ----- 170406 MaxG; total grid profit / loss
rule "Grid profit and loss23"
  when
    Item spp_Date changed
    //         s m   h D M Dow Y
    //Time cron "0 0/1 * * * ? *"
    // here every minute
  then
    logInfo("ProfitLossTotal.rule", "spp_ProfitLoss...............: {}", spp_ProfitLoss)
    logInfo("ProfitLossTotal.rule", "spp_ProfitLossTotal.1........: {}", spp_ProfitLossTotal)

    var Number pl
    var Number plTot

    pl    = spp_ProfitLoss.state as DecimalType
    plTot = spp_ProfitLossTotal.state as DecimalType
    // pl = -0.59
    // plTot = 9.96
    pl    = (Math::round(pl.floatValue * 100.0) / 100.0)
    plTot = (Math::round(plTot.floatValue * 100.0) / 100.0)
    
    postUpdate(spp_ProfitLoss, pl)
    postUpdate(spp_ProfitLossTotal, plTot + pl)

    logInfo("ProfitLossTotal.rule", "spp_ProfitLossTotal.2........: {}", spp_ProfitLossTotal)
end

If both numbers are Items, yes. Get the Item’s values, add them, put the new values to Items.
As you’re not updating spp_ProfitLoss you don’t need to post that one back.
There are ways to reduce the line count e.g.

var Number pl  = spp_ProfitLoss.state as DecimalType
1 Like

Thanks for the confirmation and simplification…

This is what should work; we’ll see tomorrow, and if not, will update with what does.

I suspect pl rounding needs to come back, as I may end up with invented decimal places.

// ----- 170406 MaxG; total grid profit / loss
rule "Grid profit and loss"
  when
    Item spp_Date changed
  then
    var Number pl    = spp_ProfitLoss.state as DecimalType
    var Number plTot = spp_ProfitLossTotal.state as DecimalType
    plTot = (Math::round(plTot.floatValue * 100.0) / 100.0)
    postUpdate(spp_ProfitLossTotal, plTot + pl)
end

I havn’t tested it, but I guess you could collapse the main part of your rule to the following:

spp_ProfitLoss.postUpdate((Math::round((spp_ProfitLoss.state as DecimalType).floatValue * 100.0) / 100.0)
spp_ProfitLossTotal.postUpdate((Math::round((spp_ProfitLossTotal.state as DecimalType).floatValue * 100.0) / 100.0) + (Math::round((spp_ProfitLoss.state as DecimalType).floatValue * 100.0) / 100.0))

Appreciate the input… I am more for legibility of statements :wink: