Frugal home temperature measurements with OpenHAB

Break the command up and log out each step.

(function(data) {
  console.log('Raw data: ' + data);
  const replaced = data.replace("'", '"');
  console.log('Replaced: ' + data);
  const parsed = JSON.parse(replaced);
  utils.dumpObject(parsed);
  const parsedValue = parsed["1"];
  console.log('Parsed Value: ' + parsedValue);
  const calcResult = parsedValue / 10;
  console.log('Calculation Result: ' + calcResult);
  return calResult;
})(input)

That should log out each step of the parse and calculation.

That did not go so well…

2023-09-11 16:30:41.256 [ERROR] [.module.script.profile.ScriptProfile] - Failed to process script 'config:js:parseTuyaSensorData': org.graalvm.polyglot.PolyglotException: SyntaxError: Unexpected end of input
^[[B^[[B^[[A^[[A^[[A2023-09-11 16:30:54.473 [INFO ] [org.openhab.automation.script       ] - Raw data: { '1': 211, '2': 541, '4': 100 }
2023-09-11 16:30:54.475 [INFO ] [org.openhab.automation.script       ] - Replaced: { '1': 211, '2': 541, '4': 100 }
2023-09-11 16:30:54.476 [ERROR] [b.automation.script.javascript.stack] - Failed to execute script:
org.graalvm.polyglot.PolyglotException: SyntaxError: Unexpected end of input
        at <js>.:anonymous(<eval>:13) ~[?:?]
        at <js>.:program(<eval>:9) ~[?:?]
        at org.graalvm.polyglot.Context.eval(Context.java:399) ~[?:?]
        at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.eval(GraalJSScriptEngine.java:458) ~[?:?]
        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) ~[?:?]
        at org.openhab.core.automation.module.script.profile.ScriptProfile.executeScript(ScriptProfile.java:145) ~[?:?]
        at org.openhab.core.automation.module.script.profile.ScriptProfile.onStateUpdateFromHandler(ScriptProfile.java:127) ~[?:?]
        at org.openhab.core.thing.internal.CommunicationManager.lambda$13(CommunicationManager.java:498) ~[?:?]
        at org.openhab.core.thing.internal.CommunicationManager.lambda$15(CommunicationManager.java:519) ~[?:?]
        at java.lang.Iterable.forEach(Iterable.java:75) ~[?:?]
        at org.openhab.core.thing.internal.CommunicationManager.handleCallFromHandler(CommunicationManager.java:515) ~[?:?]
        at org.openhab.core.thing.internal.CommunicationManager.stateUpdated(CommunicationManager.java:496) ~[?:?]
        at org.openhab.core.thing.internal.ThingHandlerCallbackImpl.stateUpdated(ThingHandlerCallbackImpl.java:65) ~[?:?]
        at org.openhab.core.thing.binding.BaseThingHandler.updateState(BaseThingHandler.java:268) ~[?:?]
        at org.openhab.core.thing.binding.BaseThingHandler.updateState(BaseThingHandler.java:287) ~[?:?]
        at org.openhab.binding.exec.internal.handler.ExecHandler.execute(ExecHandler.java:299) ~[?:?]
        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) ~[?:?]
2023-09-11 16:30:54.485 [ERROR] [.module.script.profile.ScriptProfile] - Failed to process script 'config:js:parseTuyaSensorData': org.graalvm.polyglot.PolyglotException: SyntaxError: Unexpected end of input

Hmmm. The replace didn’t work.

Doh! I messed up the log statement.

(function(data) {
  console.log('Raw data: ' + data);
  const replaced = data.replace("'", '"');
  console.log('Replaced: ' + replaced);
  const parsed = JSON.parse(replaced);
  utils.dumpObject(parsed);
  const parsedValue = parsed["1"];
  console.log('Parsed Value: ' + parsedValue);
  const calcResult = parsedValue / 10;
  console.log('Calculation Result: ' + calcResult);
  return calResult;
})(input)

I logged data instead of replaced.

We really need to see what is in replaced to understand why the JSON.parse failed. Based on the stack trace that appears to be where the error is occurring. It’s not getting to the dumpObject.

Some loggings this time

2023-09-11 21:40:35.406 [INFO ] [org.openhab.automation.script       ] - Raw data: { '1': 211, '2': 570, '4': 100 }
2023-09-11 21:40:35.407 [INFO ] [org.openhab.automation.script       ] - Replaced: { "1': 211, '2': 570, '4': 100 }
2023-09-11 21:40:35.409 [ERROR] [b.automation.script.javascript.stack] - Failed to execute script:
org.graalvm.polyglot.PolyglotException: SyntaxError: Unexpected end of input
	at <js>.:anonymous(<eval>:13) ~[?:?]
	at <js>.:program(<eval>:9) ~[?:?]
	at org.graalvm.polyglot.Context.eval(Context.java:399) ~[?:?]
	at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.eval(GraalJSScriptEngine.java:458) ~[?:?]
	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) ~[?:?]
	at org.openhab.core.automation.module.script.profile.ScriptProfile.executeScript(ScriptProfile.java:145) ~[?:?]
	at org.openhab.core.automation.module.script.profile.ScriptProfile.onStateUpdateFromHandler(ScriptProfile.java:127) ~[?:?]
	at org.openhab.core.thing.internal.CommunicationManager.lambda$13(CommunicationManager.java:498) ~[?:?]
	at org.openhab.core.thing.internal.CommunicationManager.lambda$15(CommunicationManager.java:519) ~[?:?]
	at java.lang.Iterable.forEach(Iterable.java:75) ~[?:?]
	at org.openhab.core.thing.internal.CommunicationManager.handleCallFromHandler(CommunicationManager.java:515) ~[?:?]
	at org.openhab.core.thing.internal.CommunicationManager.stateUpdated(CommunicationManager.java:496) ~[?:?]
	at org.openhab.core.thing.internal.ThingHandlerCallbackImpl.stateUpdated(ThingHandlerCallbackImpl.java:65) ~[?:?]
	at org.openhab.core.thing.binding.BaseThingHandler.updateState(BaseThingHandler.java:268) ~[?:?]
	at org.openhab.core.thing.binding.BaseThingHandler.updateState(BaseThingHandler.java:287) ~[?:?]
	at org.openhab.binding.exec.internal.handler.ExecHandler.execute(ExecHandler.java:299) ~[?:?]
	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) ~[?:?]
2023-09-11 21:40:35.417 [ERROR] [.module.script.profile.ScriptProfile] - Failed to process script 'config:js:parseTuyaSensorData': org.graalvm.polyglot.PolyglotException: SyntaxError: Unexpected end of input

Grumble.

It’s only replacing the first matching '. We need it to replace all of them. That’s probably why it’s failing. If it wasn’t valid JSON before, it’s really not valid now.

Try with replaceAll instead of just replace.

This

(function(data) {
  console.log('Raw data: ' + data);
  const replaced = data.replaceAll("'", '"');
  console.log('Replaced: ' + replaced);
  const parsed = JSON.parse(replaced);
  utils.dumpObject(parsed);
  const parsedValue = parsed["1"];
  console.log('Parsed Value: ' + parsedValue);
  const calcResult = parsedValue / 10;
  console.log('Calculation Result: ' + calcResult);
  return calResult;
})(input)

Yields this

2023-09-11 21:55:36.324 [INFO ] [org.openhab.automation.script       ] - Raw data: { '1': 211, '2': 574, '4': 100 }
2023-09-11 21:55:36.326 [INFO ] [org.openhab.automation.script       ] - Replaced: { "1": 211, "2": 574, "4": 100 }
2023-09-11 21:55:36.328 [INFO ] [.openhab.automation.openhab-js.utils] - Dumping object...
2023-09-11 21:55:36.330 [INFO ] [.openhab.automation.openhab-js.utils] -   typeof obj = object
2023-09-11 21:55:36.332 [INFO ] [.openhab.automation.openhab-js.utils] -   Java.isJavaObject(obj) = false
2023-09-11 21:55:36.334 [INFO ] [.openhab.automation.openhab-js.utils] -   Java.isType(obj) = false
2023-09-11 21:55:36.337 [INFO ] [.openhab.automation.openhab-js.utils] -   getOwnPropertyNames(obj) = 1,2,4
2023-09-11 21:55:36.342 [INFO ] [.openhab.automation.openhab-js.utils] -   getAllPropertyNames(obj) = 1,2,4,__proto__,constructor,hasOwnProperty,isPrototypeOf,propertyIsEnumerable,toLocaleString,toString,valueOf,__defineGetter__,__defineSetter__,__lookupGetter__,__lookupSetter__
2023-09-11 21:55:36.344 [INFO ] [org.openhab.automation.script       ] - Parsed Value: 211
2023-09-11 21:55:36.345 [INFO ] [org.openhab.automation.script       ] - Calculation Result: 21.1
2023-09-11 21:55:36.347 [ERROR] [b.automation.script.javascript.stack] - Failed to execute script:
org.graalvm.polyglot.PolyglotException: ReferenceError: "calResult" is not defined
	at <js>.:anonymous(<eval>:19) ~[?:?]
	at <js>.:program(<eval>:9) ~[?:?]
	at org.graalvm.polyglot.Context.eval(Context.java:399) ~[?:?]
	at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.eval(GraalJSScriptEngine.java:458) ~[?:?]
	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) ~[?:?]
	at org.openhab.core.automation.module.script.profile.ScriptProfile.executeScript(ScriptProfile.java:145) ~[?:?]
	at org.openhab.core.automation.module.script.profile.ScriptProfile.onStateUpdateFromHandler(ScriptProfile.java:127) ~[?:?]
	at org.openhab.core.thing.internal.CommunicationManager.lambda$13(CommunicationManager.java:498) ~[?:?]
	at org.openhab.core.thing.internal.CommunicationManager.lambda$15(CommunicationManager.java:519) ~[?:?]
	at java.lang.Iterable.forEach(Iterable.java:75) ~[?:?]
	at org.openhab.core.thing.internal.CommunicationManager.handleCallFromHandler(CommunicationManager.java:515) ~[?:?]
	at org.openhab.core.thing.internal.CommunicationManager.stateUpdated(CommunicationManager.java:496) ~[?:?]
	at org.openhab.core.thing.internal.ThingHandlerCallbackImpl.stateUpdated(ThingHandlerCallbackImpl.java:65) ~[?:?]
	at org.openhab.core.thing.binding.BaseThingHandler.updateState(BaseThingHandler.java:268) ~[?:?]
	at org.openhab.core.thing.binding.BaseThingHandler.updateState(BaseThingHandler.java:287) ~[?:?]
	at org.openhab.binding.exec.internal.handler.ExecHandler.execute(ExecHandler.java:299) ~[?:?]
	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) ~[?:?]
2023-09-11 21:55:36.359 [ERROR] [.module.script.profile.ScriptProfile] - Failed to process script 'config:js:parseTuyaSensorData': org.graalvm.polyglot.PolyglotException: ReferenceError: "calResult" is not defined

Typo, calcResult.

Seems to work:

2023-09-13 10:03:44.404 [INFO ] [org.openhab.automation.script       ] - Raw data: { '1': 209, '2': 554, '4': 100 }
2023-09-13 10:03:44.405 [INFO ] [org.openhab.automation.script       ] - Replaced: { "1": 209, "2": 554, "4": 100 }
2023-09-13 10:03:44.407 [INFO ] [.openhab.automation.openhab-js.utils] - Dumping object...
2023-09-13 10:03:44.413 [INFO ] [.openhab.automation.openhab-js.utils] -   typeof obj = object
2023-09-13 10:03:44.415 [INFO ] [.openhab.automation.openhab-js.utils] -   Java.isJavaObject(obj) = false
2023-09-13 10:03:44.417 [INFO ] [.openhab.automation.openhab-js.utils] -   Java.isType(obj) = false
2023-09-13 10:03:44.419 [INFO ] [.openhab.automation.openhab-js.utils] -   getOwnPropertyNames(obj) = 1,2,4
2023-09-13 10:03:44.422 [INFO ] [.openhab.automation.openhab-js.utils] -   getAllPropertyNames(obj) = 1,2,4,__proto__,constructor,hasOwnProperty,isPrototypeOf,propertyIsEnumerable,toLocaleString,toString,valueOf,__defineGetter__,__defineSetter__,__lookupGetter__,__lookupSetter__
2023-09-13 10:03:44.423 [INFO ] [org.openhab.automation.script       ] - Parsed Value: 209
2023-09-13 10:03:44.424 [INFO ] [org.openhab.automation.script       ] - Calculation Result: 20.9

Thanks, again, for the help @rlkoshak ! Is there any way to get rid of this “info” from the log?
I would now like to display the result as

20.9 °C

Changed the output item type to number:temperature but that did not change the unit. The Exec command output is a string. Any ideas how to make this work?

Well, we added log statements to add this stuff to the log so the answer should be obvious. To remove the lines from the log, remove the logging statements.

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