Casting error?

I have a very simple rule which calculates kW and kVA from Volts, Amps and power factor. It seems to work but creates cast errors and since my RPI crashes every few days, I’m keen to deal with all errors.

Any ideas what I’m doing wrong here?


rule "Power Calcs"
Item Irms changed or
Item HIrms changed


var Number I= Irms.state as DecimalType
var Number HI= HIrms.state as DecimalType
var Number V= Vrms.state as DecimalType
var Number PFactor= PF.state as DecimalType




Number Irms “Irms [%.1f A]” (energy,IC,Node22) {mqtt="<[magtt:home/rfm_gw/nb/node22/dev48:state:default]"}
Number Vrms “Vrms [%.1f V]” (energy,energy_chart,Node22) {mqtt="<[magtt:home/rfm_gw/nb/node22/dev49:state:default]"}
Number PF “PF [%.2f]” (energy,energy_chart,Node22) {mqtt="<[magtt:home/rfm_gw/nb/node22/dev50:state:default]"}
Number VA “VA [%.1f VA]” (energy,energy_chart,Node22)
Number W “W [%.1f W]” (energy,energy_chart,kWC,Node22)
Number HIrms “HWC Irms [%.1f A]” (energy,energy_chart,IC,Node22) {mqtt="<[magtt:home/rfm_gw/nb/node22/dev51:state:default]"}

It sure would be helpful to actually see the errors.

Without those I can’t say what would be wrong. The only thing that stands out is that the postUpdate and sendCommand actions often do not handle numbers very well. Try:



postUpdate(VA, (V*I).toString)

Thanks for the suggestion. Aside from actual syntax what is the difference between

VA.postUpdate(VI) and

Log file reports

2016-04-05 22:21:47.412 [ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during the execution of rule ‘Power Calcs’: Cannot cast org.openhab.core.types.UnDefType to org.openhab.core.library.types.DecimalType

Without going into a whole long dissertation on OO programming languages the main difference is that the postUpdate() action is a generic item that must be able to handle sending updates to ALL types of Items. Therefore it is defined as postUpdate(String, String) and it tries its best to convert that second String into something that the Item with the name of the first String can handle. The VA.postUpdate(V*I) is a call on the object that represents the actual Item. Because of that it only needs to know how to handle the types of data that the Item accepts but it also knows how to handle it better. Therefore this second method is much better at handling being passed numbers and such.

In general I find things work much better when using the VA.postUpdate method.

I, HI, V, or PFactor is undefined (i.e. doesn’t have a state yet).

1 Like

Thank you Rich - I really appreciate the effort you put into answering this question - that all makes lots of sense.