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?
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.
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