Cast number ... DecimalType -- pulling my hair out

I am getting errors from the following rule:

if (((Battery_SOC.state as DecimalType) >= spp_GridSupportSocLevelMax)
2017-05-21 23:26:51.974 [ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during the execution of rule 'SP PRO GO: STOP MinSoc grid support anytime at SocLevelMax': Could not invoke method: org.openhab.model.script.lib.NumberExtensions.operator_greaterEqualsThan(org.openhab.core.types.Type,java.lang.Number) on instance: null

changing the rule to:

if (((Battery_SOC.state as DecimalType) >= (spp_GridSupportSocLevelMax as DecimalType))
2017-05-21 23:35:36.616 [ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during the execution of rule 'SP PRO GO: STOP MinSoc grid support anytime at SocLevelMax': Cannot cast org.openhab.core.library.items.NumberItem to org.openhab.core.library.types.DecimalType

The item.state are:

2017-05-22 00:10:37.047 [INFO ] [.m.script.GridSupport1on1.rule] - Battery_SOC.state = 66.75
2017-05-22 00:10:37.687 [INFO ] [.m.script.GridSupport1on3.rule] - spp_GridSupportSocLevelMin.state = 50
2017-05-22 00:10:38.104 [INFO ] [.m.script.GridSupport1on4.rule] - spp_GridSupportSocLevelMin.state (dec) = 50

The items are setpoints (integers?!):

Setpoint item=spp_GridSupportSocLevelMin	minValue=31 maxValue=50 step=1
Setpoint item=spp_GridSupportSocLevelMax	minValue=40 maxValue=60 step=1

tried:

    var Number batSoc = Battery_SOC.state as DecimalType
    var Number lvlMin = spp_GridSupportSocLevelMin as DecimalType

    if (batSoc <= lvlMin)

and also get:

2017-05-22 00:30:18.995 [ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during the execution of rule 'SP PRO GO: START MinSoc grid support anytime at SocLevelMin': Cannot cast org.openhab.core.library.items.NumberItem to org.openhab.core.library.types.DecimalType

I need the comparison for the DecimalType (Battery_SOC) – not the change it to in()

Any help appreciated.

Hello,

I’ve got a rule checking battery states, sending a mail if below 10%.
It’s no more than:

//Item:
Number		WoZi_Bat //connected to a zwave device
//rule comparison
if (WoZi_Bat.state<10 || Terasse_battery.state<10)

and works without problems.
What Type are your items? If all are numbers, I would say you don’t need any casting

Regards,
-OLI

You should test if your state is an instance of DecimalType:

if(!(Battery_SOC.state instanceof DecimalType))  //if not instance of DecimalType
    Battery_SOC.postUpdate(0)                    // Set to 0, as Battery_SOC is undef or null
...

Thanks guys


if ( (Battery_SOC.state) <= 30)
if ( (Battery_SOC.state as Number) <= 30)
if ( (Battery_SOC.state as DecimalType) <= 30)

works for me too – and is not the issue

It is the SetPoint value in GridSupportSocLevelMin it does not like

interesting


here the complete rule for reference:

// ----- 170521 MaxG: START MinSoc grid support anytime at SocLevelMin
rule "SP PRO GO: START MinSoc grid support anytime at SocLevelMin"
  when
    Item Battery_SOC received update
  then
    logInfo("GridSupport1on1.rule", "Battery_SOC.state = {}", Battery_SOC.state)
    logInfo("GridSupport1on2.rule", "spp_GridSupportSocLevelMin.state = {}", spp_GridSupportSocLevelMin.state)
    logInfo("GridSupport1on3.rule", "spp_GridSupportManual.state = {}", spp_GridSupportManual.state)

    // testing
    if (!(Battery_SOC.state instanceof Decimaltype)
      logInfo("GridSupport1on0.rule", "Battery_SOC is NOT a DecimalType")

    if (((Battery_SOC.state as Number) <= spp_GridSupportSocLevelMin) && (spp_GridSupportManual.state == OFF)) {
      logInfo("GridSupport1on4.rule", "evaluating Battery_SOC <= spp_GridSupportSocLevelMin")

      if (spp_GridSupportAllowed.state == OFF) {
        // set MinSoc flag ON
        sendCommand(spp_GridSupportMinSoc, ON)
        // switch grid support on
        sendCommand(spp_GridSupportAllowed, ON)
        postUpdate(spp_GridSupportAllowed, ON)
        logInfo("GridSupport1on.rule", "GridSupportAllowed and MinSoc is now ON because of SOC <= SocLevelMin")
      }
    }
end

You’re right: It is not a DecimalType! :frowning:

What do I need to do to make this comparison work?
Which cast to what?

Also, refer to all error messages I have listed, which tell me what the problem is; but I do not know how to fix it.
It is a simple casting issue (I think)


The rule worked with comapring the BAttery_SOC with a literal number, but not when using the SetPoint value.

Also, where would I find the reference doc for the instanceof, casting types?

Are there isNumber, isInt, etc. functions?
Is there a Reference Manual?

    // testing
    if (!(Battery_SOC.state instanceof Decimaltype)
      logInfo("GridSupport1on0.rule", "Battery_SOC is NOT a DecimalType")
    if (!(spp_GridSupportSocLevelMin.state instanceof Decimaltype)
      logInfo("GridSupport1on0.rule", "spp_GridSupportSocLevelMin is NOT a DecimalType")

both come back as not DecimalType.
What are they then?

2017-05-22 09:20:49.254 [INFO ] [.m.script.GridSupport1on1.rule] - Battery_SOC.state = 53.00
2017-05-22 09:20:51.809 [INFO ] [.m.script.GridSupport1on2.rule] - spp_GridSupportSocLevelMin.state = 60
2017-05-22 09:20:52.831 [INFO ] [.m.script.GridSupport1on3.rule] - spp_GridSupportManual.state = OFF
2017-05-22 09:20:54.405 [INFO ] [.m.script.GridSupport1on0.rule] - Battery_SOC is NOT a DecimalType
2017-05-22 09:20:55.983 [INFO ] [.m.script.GridSupport1on0.rule] - spp_GridSupportSocLevelMin is NOT a DecimalType
2017-05-22 09:20:57.568 [ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during the execution of rule 'SP PRO GO: START MinSoc grid support anytime at SocLevelMin': Could not invoke method: org.openhab.model.script.lib.NumberExtensions.operator_lessEqualsThan(org.openhab.core.types.Type,java.lang.Number) on instance: null

There is a difference between Decimaltype and DecimalType.
The if() bracketing is wrong too.

Bear in mind your “setpoint items” are not items of type setpoint. You have just chosen to display/update some items on your sitemap using the ‘setpoint’ gizmo. Somewhere, somehow you will have defined the real types of those Items (I expect as Number types)

I think there is a catch-22 with setpoints. If your item starts off as undefined, the setpoint gizmo cannot alter it (how would you do “more of undefined”). The item needs to be initialized first somehow

2 Likes

Oops
 typo fixed – as a result both are now saying IS DecimalType

However, still get the error of:

2017-05-22 10:28:34.746 [INFO ] [.m.script.GridSupport1on1.rule] - Battery_SOC.state = 68.75
2017-05-22 10:28:34.772 [INFO ] [.m.script.GridSupport1on2.rule] - spp_GridSupportSocLevelMin.state = 73
2017-05-22 10:28:34.785 [INFO ] [.m.script.GridSupport1on3.rule] - spp_GridSupportManual.state = OFF
2017-05-22 10:28:34.798 [INFO ] [.m.script.GridSupport1on0.rule] - Battery_SOC IS a DecimalType
2017-05-22 10:28:34.810 [INFO ] [.m.script.GridSupport1on0.rule] - spp_GridSupportSocLevelMin IS a DecimalType
2017-05-22 10:28:34.890 [ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during the execution of rule 'SP PRO GO: START MinSoc grid support anytime at SocLevelMin': The name '<XFeatureCallImplCustom> <= <XFeatureCallImplCustom>' cannot be resolved to an item or type.

With the comparison:

if ( (Battery_SOC <= spp_GridSupportSocLevelMin) && (spp_GridSupportManual.state == OFF) ) 

The SetPoint value have been initialised with:

rule "Bootstrap any setpoint / level value"
  when
    System started
  then
    logInfo("BootStrap.rule", "in Bootstrap rule: Levels...")

    // spp_GridSupportSocLevelMin
    if (spp_GridSupportSocLevelMin.state == Uninitialized) {
      logInfo("BootStrap.rule", "spp_GridSupportSocLevelMin set to 31")
      postUpdate(spp_GridSupportSocLevelMin, 31)
    }

    // spp_GridSupportSocLevelMax
    if (spp_GridSupportSocLevelMax.state == Uninitialized) {
      logInfo("BootStrap.rule", "spp_GridSupportSocLevelMax set to 40")
      postUpdate(spp_GridSupportSocLevelMax, 40)
    }
   ... plus more...
end

changing the if to:

    if ( (Battery_SOC.state <= spp_GridSupportSocLevelMin.state) && (spp_GridSupportManual.state == OFF) )

now works


I will have to backtrace, why I even had the problems in the first place. :blush:

Thank you for your help!

also found documentation: https://www.eclipse.org/xtend/documentation/203_xtend_expressions.html