No decimals after transfomation


I’ve got some temperature measurements via modbus in openhab.

The PT1000 elements displayed at 2400 instead of 24.00 degrees
The PT100 elements displayed at 240 instead of 24.0 degrees

I’ve created two files in the transform folder:


With the following code i took from the internet. The divideby100 is with 100 instead of 10 ofcourse.

(function (inputData) {
return parseFloat(inputData / 10 ;

But now my measurments haven’t got the decimals, so a measurment like 2434 is now displayed as 24.
Is it possible to get show the decimals?

Gotta be careful with that code from the internet. There’s a typo.

(function (inputData) {
  return parseFloat(inputData) / 10 ;

It’s missing a closing parens.

Note: please use code fences when posting code or logs to the forum.


code goes here

Note 2: Tutorials and Examples is a place to post a tutorial, not to ask for one. I’ve moved this to a more appropriate category.

This has nothing to do with the transformation. What is the State Description Pattern set on the Item? What type of Item is it?

Thanks for your reply,

I used it as a Number, when i changed it to string it showed the decimals.

Also tried to us number:temperature but that also didn’t show the decimals.

But the item as a string can’t be analyzed. So that isn’t the sollution also.

And this?

Edit Item Metadata: stateDescription

This is empty.

**value: ""**
**  readOnly: false**

Made an new item as String, then changed it to Number_temperature and now it is with the decimals and i can analyze it.

Hi Rich (or anyone else).

Im still no good at these scripts. But I have a quite simular problem.

I have a modbus devices which return weird results from temperature sensors. For some reason they have set 0.00 degree celcius = 300. Which means the device will return 525, when the actual temperature is 22,5 degree celcius.

Because of that I need to transform the state by substracting 300, and divide by 10.

Question is, will the same script be able to handle but substraction and division in the same line like this?

(function (inputData) {
  return parseFloat(inputData) -300 / 10 ;

I tried the buld-in transformation in modbus, but that didnt work.

Thanks in advance.

I see no reason why that won’t work but beware of order of operations. The usual math rules will apply meaning the division will happen before the subtraction which is probably not what you want. As with math you can force the order using parens.

return (parseFloat(inputData) - 300) / 10;

If that doesn’t work, add some logging so you can see each step of the calculation and hopefully figure out where it’s going wrong.

Didn’t work how? Wrong answer or error?

Thanks… I´ll give it a try.

Nothing happened at all. Like the transformation never occured.

Hmm, didnt work… I get a “inputdata not defined”.

2023-09-15 19:21:43.040 [ERROR] [s.internal.SingleValueTransformation] - transformation throws exception [transformation=JS(minus300divide10.js), response=1001]
org.openhab.core.transform.TransformationException: Failed to execute script.
	at org.openhab.core.automation.module.script.ScriptTransformationService.transform( ~[?:?]
	at org.openhab.binding.modbus.internal.SingleValueTransformation.transform( ~[?:?]
	at org.openhab.binding.modbus.internal.CascadedValueTransformationImpl.transform( ~[?:?]
	at org.openhab.binding.modbus.internal.ValueTransformation.transformState( ~[?:?]
	at org.openhab.binding.modbus.internal.handler.ModbusDataThingHandler.lambda$16( ~[?:?]
	at java.util.HashMap$KeySpliterator.forEachRemaining( ~[?:?]
	at$Head.forEach( ~[?:?]
	at org.openhab.binding.modbus.internal.handler.ModbusDataThingHandler.processUpdatedValue( ~[?:?]
	at org.openhab.binding.modbus.internal.handler.ModbusDataThingHandler.onRegisters( ~[?:?]
	at org.openhab.binding.modbus.internal.handler.ModbusDataThingHandler.lambda$11( ~[?:?]
	at java.util.Optional.ifPresent( ~[?:?]
	at org.openhab.binding.modbus.internal.handler.ModbusDataThingHandler.onReadResult( ~[?:?]
	at org.openhab.binding.modbus.handler.ModbusPollerThingHandler$ReadCallbackDelegator.lambda$2( ~[?:?]
	at java.util.concurrent.CopyOnWriteArrayList.forEach( ~[?:?]
	at org.openhab.binding.modbus.handler.ModbusPollerThingHandler$ReadCallbackDelegator.notifyChildren( ~[?:?]
	at org.openhab.binding.modbus.handler.ModbusPollerThingHandler$ReadCallbackDelegator.handleResult( ~[?:?]
	at org.openhab.binding.modbus.handler.ModbusPollerThingHandler$ReadCallbackDelegator.handle( ~[?:?]
	at ~[?:?]
	at$PollOperation.lambda$1( ~[?:?]
	at ~[?:?]
	at$PollOperation.accept( ~[?:?]
	at$PollOperation.accept( ~[?:?]
	at ~[?:?]
	at$ModbusCommunicationInterfaceImpl.lambda$1( ~[?:?]
	at java.util.concurrent.Executors$ ~[?:?]
	at java.util.concurrent.FutureTask.runAndReset( ~[?:?]
	at java.util.concurrent.ScheduledThreadPoolExecutor$ ~[?:?]
	at java.util.concurrent.ThreadPoolExecutor.runWorker( ~[?:?]
	at java.util.concurrent.ThreadPoolExecutor$ ~[?:?]
	at ~[?:?]
Caused by: javax.script.ScriptException: org.graalvm.polyglot.PolyglotException: ReferenceError: "inputData" is not defined
	at ~[?:?]
	at ~[?:?]
	at ~[?:?]
	at javax.script.AbstractScriptEngine.eval( ~[java.scripting:?]
	at org.openhab.automation.jsscripting.internal.scriptengine.DelegatingScriptEngineWithInvocableAndAutocloseable.eval( ~[?:?]
	at org.openhab.automation.jsscripting.internal.scriptengine.InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.eval( ~[?:?]
	at org.openhab.automation.jsscripting.internal.scriptengine.DelegatingScriptEngineWithInvocableAndAutocloseable.eval( ~[?:?]
	at org.openhab.automation.jsscripting.internal.scriptengine.InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.eval( ~[?:?]
	at org.openhab.core.automation.module.script.ScriptTransformationService.transform( ~[?:?]
	... 29 more
Caused by: org.graalvm.polyglot.PolyglotException: ReferenceError: "inputData" is not defined
	at <js>.:anonymous(<eval>:3) ~[?:?]
	at <js>.:program(<eval>:2) ~[?:?]
	at org.graalvm.polyglot.Context.eval( ~[?:?]

This is the script:

// Wrap everything in a function
(function(i) {
    return (parseFloat(inputData) - 300) / 10;
// input variable contains data passed by openhab

I wonder if this happens because I have a unit set °C.
This is the state output from the logfile:

2023-09-15 19:29:07.510 [INFO ] [openhab.event.ItemStateUpdatedEvent ] - Item 'input0000' updated to 478 °C
2023-09-15 19:29:07.511 [INFO ] [openhab.event.ItemStateUpdatedEvent ] - Item 'input0000' updated to 478 °C
2023-09-15 19:29:07.511 [INFO ] [openhab.event.ItemStateUpdatedEvent ] - Item 'input0002' updated to 470 °C
2023-09-15 19:29:07.512 [INFO ] [openhab.event.ItemStateUpdatedEvent ] - Item 'input0003' updated to 536 °C
2023-09-15 19:29:07.512 [INFO ] [openhab.event.ItemStateUpdatedEvent ] - Item 'input0006' updated to 562 °C
2023-09-15 19:29:08.547 [INFO ] [openhab.event.ItemStateUpdatedEvent ] - Item 'input0009' updated to 566 °C

I am pretty sure this should be:

(function(inputData) {
    return (parseFloat(inputData) - 300) / 10;

Ahh damn… You´re right… My mistake :slight_smile:

Works fine now… Thank you!

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.