# Openhab rule with math operation does not work

I am trying to get this openhab rule to work. I want to convert a number item to a different number using a mathematical formula. The relevant items are:
indent preformatted text by 4 spaces Number itm_oilinductance “Oil Raw Reading [%.1f]” { mqtt="<[mymosquitto:home/rfm_gw/nb/node63/dev63:state:default]" }
String itm_oilpercent “Oil Tank Percent Full [%s]”

and the rule is:

``````    var Number oil = 0
``````

var String oilconv
var String oil2

rule OilUpdate
when
then
oil = (itm_oilinductance.state as Decimaltype).floatValue
oilconv = (3 * Math.pow(10,-11) * Math.pow(oil,3)) + (2 * Math.pow(10,-6) * Math.pow(oil, 2)) + (.0291 * oil) - 129.05
itm_oilpercent.postUpdate(oilconv)
end

I post the correct value for itm_oilconductance, so I know that I am receiving that value through mqtt. However, the arithmetic operations do not appear to work, as I see no value for itm_oilpercent. What am I doing wrong?

Many thanks,

-Mark

Trying the formatting again:

Items:

``````    Number	itm_oilinductance	"Oil Raw Reading [%.1f]"		{ mqtt="<[mymosquitto:home/rfm_gw/nb/node63/dev63:state:default]" }
``````

String itm_oilpercent “Oil Tank Percent Full [%s]”

Rule

``````    rule OilUpdate
when
then
oil = (itm_oilinductance.state as Decimaltype).floatValue
oilconv = (3 * Math.pow(10,-11) * Math.pow(oil,3)) + (2 * Math.pow(10,-6) * Math.pow(oil, 2)) + (.0291 * oil) - 129.05
itm_oilpercent.postUpdate(oil)
``````

end

You declare oil and oilconv as Strings. You cannot do math with Strings.

Declare them as Number and get true of the floatValue call.

1 Like

The two rules are different, so not quite sure about your rule logic (for example you use oil or oilconv to update itm_oilpercent), in addition to what Rich said:
I believe you need quotation marks in the rule header

``````rule "OilUpdate"
when
....
``````

What will help you tremendously in your debugging is to use logInfo statements a lot. This way you see in your logfiles whether your rule gets even triggered, you can print out values of variables, monitor which lines are executed, etc.

1 Like