Multiply in postUpdate by 10^exponent

Rpi4
Openhab2 bullseye

I need to make mathematical operation:
var result = power_ac * 10^power_ac_scale

    val String power_ac = transform("JSONPATH", "$.c_power_ac", (rawMessage.state).toString)
    val String power_ac_scale = transform("JSONPATH", "$.c_power_ac_scale", (rawMessage.state).toString)

How can I do it in a rule please ?

Are you doing this to convert units? If so, using Units of Measurement will be by far the easiest. Otherwise you need to parse those numbers and use java.lang.Math.pow().

val power_ac = new BigDecimal(transform("JSONPATH", "$.c_power_ac", (rawMessage.state).toString))
val power_ac_scale = new BigDecimal(transform("JSONPATH", "$.c_power_ac_scale", (rawMessage.state).toString))
val result = power_ac * java.lang.Math(10, power_ac_scale.doubleValue)

:
Both items are integer. result is a float value and it’s computed as:
result = power_ac * 10^power_ac_scale

Example: power_ac=23566 and scale is -2, result is 235.66

It doesn’t matter. java.lang.Math.pow() requires primitive double values.

I notice there was a typo in my code above. I left off the pow.

1 Like

Please what should be on begin of .rules file? Openhab 2.5.4 !

import java.math.BigDecimal;
import java.lang.Math; ???

    val current = new BigDecimal(transform("JSONPATH", "$.c_current", (rawMessage.state).toString))
    val current_scale = transform("JSONPATH", "$.c_current_scale", (rawMessage.state).toString)
    val current_result = current * java.lang.Math(10, current_scale.doubleValue)

In last row are two errors - like picture

The method or field java is undefined(org.eclipse.xtext.diagnostics.Diagnostic.Linking)
The method or field doubleValue is undefined for the type String(org.eclipse.xtext.diagnostics.Diagnostic.Linking)

Of course result should be a float measurement.
Thanks.

I haven’t use OH 2.5.4 in over two years now. 2.5.4 is no longer supported so the best you’re going to get is vague memories. If it doesn’t work :person_shrugging: If you want to stay on such an old and unsupported version of any software, you should be changing anything on it anyway.

But there doesn’t need to be any imports. Both BigDecimal and Math are imported by default. And we used the full name of java.lang.Math anyway.

Again, like I said above,

That tells you what the problem is.

 val current_result = current * java.lang.Math.pow(10, current_scale.doubleValue)

If it still complains about the doubleValue, :person_shrugging: . That works for me.

But also, if you are relying purely on VSCode errors you will never get these lines to not show an error. They are not real errors. Just a limitation of the openHAB plug-in.

1 Like

Thank you Rich.