No decimals after transfomation

Hi,

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:

divideby10.js
divideby100.js

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

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

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 ;
}){input)

It’s missing a closing parens.

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

image

```
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: ""**
**config:**
**  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 ;
}){input)

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(ScriptTransformationService.java:218) ~[?:?]
	at org.openhab.binding.modbus.internal.SingleValueTransformation.transform(SingleValueTransformation.java:140) ~[?:?]
	at org.openhab.binding.modbus.internal.CascadedValueTransformationImpl.transform(CascadedValueTransformationImpl.java:51) ~[?:?]
	at org.openhab.binding.modbus.internal.ValueTransformation.transformState(ValueTransformation.java:48) ~[?:?]
	at org.openhab.binding.modbus.internal.handler.ModbusDataThingHandler.lambda$16(ModbusDataThingHandler.java:1001) ~[?:?]
	at java.util.HashMap$KeySpliterator.forEachRemaining(HashMap.java:1707) ~[?:?]
	at java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:762) ~[?:?]
	at org.openhab.binding.modbus.internal.handler.ModbusDataThingHandler.processUpdatedValue(ModbusDataThingHandler.java:969) ~[?:?]
	at org.openhab.binding.modbus.internal.handler.ModbusDataThingHandler.onRegisters(ModbusDataThingHandler.java:847) ~[?:?]
	at org.openhab.binding.modbus.internal.handler.ModbusDataThingHandler.lambda$11(ModbusDataThingHandler.java:801) ~[?:?]
	at java.util.Optional.ifPresent(Optional.java:178) ~[?:?]
	at org.openhab.binding.modbus.internal.handler.ModbusDataThingHandler.onReadResult(ModbusDataThingHandler.java:801) ~[?:?]
	at org.openhab.binding.modbus.handler.ModbusPollerThingHandler$ReadCallbackDelegator.lambda$2(ModbusPollerThingHandler.java:144) ~[?:?]
	at java.util.concurrent.CopyOnWriteArrayList.forEach(CopyOnWriteArrayList.java:807) ~[?:?]
	at org.openhab.binding.modbus.handler.ModbusPollerThingHandler$ReadCallbackDelegator.notifyChildren(ModbusPollerThingHandler.java:142) ~[?:?]
	at org.openhab.binding.modbus.handler.ModbusPollerThingHandler$ReadCallbackDelegator.handleResult(ModbusPollerThingHandler.java:88) ~[?:?]
	at org.openhab.binding.modbus.handler.ModbusPollerThingHandler$ReadCallbackDelegator.handle(ModbusPollerThingHandler.java:105) ~[?:?]
	at org.openhab.core.io.transport.modbus.internal.ModbusLibraryWrapper.invokeCallbackWithResponse(ModbusLibraryWrapper.java:337) ~[?:?]
	at org.openhab.core.io.transport.modbus.internal.ModbusManagerImpl$PollOperation.lambda$1(ModbusManagerImpl.java:216) ~[?:?]
	at org.openhab.core.io.transport.modbus.internal.SimpleStopWatch.timeRunnable(SimpleStopWatch.java:152) ~[?:?]
	at org.openhab.core.io.transport.modbus.internal.ModbusManagerImpl$PollOperation.accept(ModbusManagerImpl.java:216) ~[?:?]
	at org.openhab.core.io.transport.modbus.internal.ModbusManagerImpl$PollOperation.accept(ModbusManagerImpl.java:1) ~[?:?]
	at org.openhab.core.io.transport.modbus.internal.ModbusManagerImpl.executeOperation(ModbusManagerImpl.java:613) ~[?:?]
	at org.openhab.core.io.transport.modbus.internal.ModbusManagerImpl$ModbusCommunicationInterfaceImpl.lambda$1(ModbusManagerImpl.java:810) ~[?:?]
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539) ~[?:?]
	at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:305) ~[?:?]
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:305) ~[?:?]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136) ~[?:?]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) ~[?:?]
	at java.lang.Thread.run(Thread.java:833) ~[?:?]
Caused by: javax.script.ScriptException: org.graalvm.polyglot.PolyglotException: ReferenceError: "inputData" is not defined
	at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.toScriptException(GraalJSScriptEngine.java:483) ~[?:?]
	at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.eval(GraalJSScriptEngine.java:460) ~[?:?]
	at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.eval(GraalJSScriptEngine.java:426) ~[?:?]
	at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:262) ~[java.scripting:?]
	at org.openhab.automation.jsscripting.internal.scriptengine.DelegatingScriptEngineWithInvocableAndAutocloseable.eval(DelegatingScriptEngineWithInvocableAndAutocloseable.java:53) ~[?:?]
	at org.openhab.automation.jsscripting.internal.scriptengine.InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.eval(InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.java:78) ~[?:?]
	at org.openhab.automation.jsscripting.internal.scriptengine.DelegatingScriptEngineWithInvocableAndAutocloseable.eval(DelegatingScriptEngineWithInvocableAndAutocloseable.java:53) ~[?:?]
	at org.openhab.automation.jsscripting.internal.scriptengine.InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.eval(InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.java:78) ~[?:?]
	at org.openhab.core.automation.module.script.ScriptTransformationService.transform(ScriptTransformationService.java:215) ~[?:?]
	... 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(Context.java:399) ~[?:?]

This is the script:

// Wrap everything in a function
(function(i) {
    return (parseFloat(inputData) - 300) / 10;
})(input)
// 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;
})(input)
2 Likes

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

Works fine now… Thank you!

1 Like