OpenHab 2 - Conversion to DecimalType error

Hi,
I’m using this code in my rule, to convert an Item to a Number:

var Number servo_set = TERMOSTATO_S_SET_POINT.state as DecimalType

but in the log i got this error, and the rule stop execution:

Error during the execution of rule Termostato: org.eclipse.smarthome.core.library.types.DecimalType

Thanks

Marco

Is that the complete error? It seems incomplete. The code looks right.

OK, I just saw that same error. Is your Item undefined? The error means it couldn’t cast to that type.

I,
you’re right, I fixed undefinited items

Thanks

Hi,

how did you fix the Problem? I´m not sure with the “undefinited items”

here is my rule:

var Number letzteAblesungWert = 56
rule "Stromverbrauch"
when
Item OGStrom_Zaehler received update
then
var Number Aktuell_Zaehler = OGStrom_Zaehler.state as DecimalType

// Verbrauch seit letzer Ablesung
OGStrom_Verbrauch_Seit_Letzter_Ablesung.postUpdate(OGStrom_Zaehler_Gesamt.state as DecimalType - letzteAblesungWert as DecimalType)

// aktueller Tagesverbrauch in KWh
OGstrom_Aktueller_Verbrauch_Kw.postUpdate(OGStrom_Aktueller_Verbrauch_W.state as DecimalType / 1000)
//logInfo(“StromRules:”, “States:” + OGstrom_Aktueller_Verbrauch_Kw.state)

//Gesamtzählerstand (HT/NT)
var Number gesamtZaehlerstand = (Aktuell_Zaehler /1000) + letzteAblesungWert as DecimalType
OGStrom_Zaehler_Gesamt.postUpdate(gesamtZaehlerstand)
end

and here is the error:

Rule ‘Stromverbrauch’: org.eclipse.smarthome.core.library.types.DecimalType

THX

Try this
letzteAblesungWert.state as DecimalType

Hi,

ok, tried…but now i get:

[ERROR] [.script.engine.ScriptExecutionThread] - Rule ‘Stromverbrauch’: An error occured during the script execution: The name ‘<“XFeatureCallImplCustom”>.state’ cannot be resolved to an item or type.

so, I’ve figured out its related to the “letzteAblesungWert”. I´ve tried:

letzteAblesungWert.state as DecimalType
letzteAblesungWert as DecimalType
letzteAblesungWert.state as Number
letzteAblesungWert as Number

Perhaps you need to look at the Item definition for letzteAblesungWert

Hi,

so the definition is a “var Number”.

Now i redesigned the rule…an its works…but I do not know why. I put everything in an “var Number”. See below.
Maybe someone can explain :wink:

rule "Stromverbrauch"
when
Item OGStrom_Zaehler received update
then
var Number aktuellerZaehler = OGStrom_Zaehler.state as DecimalType

//Gesamtzählerstand (HT/NT)
var Number gesamtZaehlerstand = (aktuellerZaehler /1000) + letzteAblesungWert
OGStrom_Zaehler_Gesamt.postUpdate(gesamtZaehlerstand)

// Verbrauch seit letzer Ablesung
var Number verbrauchLetzterAblesungDiff = (OGStrom_Zaehler_Gesamt.state as DecimalType - letzteAblesungWert)
OGStrom_Verbrauch_Seit_Letzter_Ablesung.postUpdate(verbrauchLetzterAblesungDiff)

// aktueller Verbrauch in KWh
OGstrom_Aktueller_Verbrauch_Kw.postUpdate(OGStrom_Aktueller_Verbrauch_W.state as DecimalType / 1000)
end

Then it doesn’t have a .state
OpenHAB Items (with .state) are quite different from variables defined in rules using var

1 Like