unfortunately i have not found a solution for the following problem, i have tried several things, but unfortunately i can’t get a result. i want to read the value for
alarmIntervallMin
(=60) from the metadata of an item, but unfortunately i fail …
(OH 3.4.2, JSScripting Helper Library 4.1.0)
your code gives the following error for both lines:
2023-03-08 19:45:02.292 [ERROR] [b.automation.script.javascript.stack] - Failed to execute script:
org.graalvm.polyglot.PolyglotException: TypeError: null has no such function "getMetadata"
at <js>.:program(test.js:358) ~[?:?]
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:400) ~[?:?]
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:249) ~[java.scripting:?]
at org.openhab.automation.jsscripting.internal.scriptengine.DelegatingScriptEngineWithInvocableAndAutocloseable.eval(DelegatingScriptEngineWithInvocableAndAutocloseable.java:58) ~[?:?]
at org.openhab.automation.jsscripting.internal.scriptengine.InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.eval(InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.java:84) ~[?:?]
at org.openhab.automation.jsscripting.internal.scriptengine.DelegatingScriptEngineWithInvocableAndAutocloseable.eval(DelegatingScriptEngineWithInvocableAndAutocloseable.java:58) ~[?:?]
at org.openhab.automation.jsscripting.internal.scriptengine.InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.eval(InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.java:84) ~[?:?]
at org.openhab.core.automation.module.script.internal.ScriptEngineManagerImpl.loadScript(ScriptEngineManagerImpl.java:185) ~[?:?]
at org.openhab.core.automation.module.script.rulesupport.loader.AbstractScriptFileWatcher.createAndLoad(AbstractScriptFileWatcher.java:276) ~[?:?]
at org.openhab.automation.jsscripting.internal.fs.watch.JSScriptFileWatcher.createAndLoad(JSScriptFileWatcher.java:63) ~[?:?]
at org.openhab.core.automation.module.script.rulesupport.loader.AbstractScriptFileWatcher.importFile(AbstractScriptFileWatcher.java:256) ~[?:?]
at org.openhab.core.automation.module.script.rulesupport.loader.AbstractScriptFileWatcher.lambda$5(AbstractScriptFileWatcher.java:248) ~[?:?]
at java.util.Optional.ifPresent(Optional.java:183) ~[?:?]
at org.openhab.core.automation.module.script.rulesupport.loader.AbstractScriptFileWatcher.importFileWhenReady(AbstractScriptFileWatcher.java:246) ~[?:?]
at java.lang.Iterable.forEach(Iterable.java:75) ~[?:?]
at org.openhab.core.automation.module.script.rulesupport.loader.AbstractScriptFileWatcher.processWatchEvent(AbstractScriptFileWatcher.java:223) ~[?:?]
at org.openhab.automation.jsscripting.internal.fs.watch.JSScriptFileWatcher.processWatchEvent(JSScriptFileWatcher.java:56) ~[?:?]
at org.openhab.core.service.WatchQueueReader.lambda$5(WatchQueueReader.java:357) ~[?:?]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515) [?:?]
at java.util.concurrent.FutureTask.run(FutureTask.java:264) [?:?]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304) [?:?]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) [?:?]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) [?:?]
at java.lang.Thread.run(Thread.java:829) [?:?]
2023-03-08 19:45:02.298 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/etc/openhab/automation/js/test.js': org.graalvm.polyglot.PolyglotException: TypeError: null has no such function "getMetadata"
Your code looks correct to me. I think your problem is that you are taking this
to mean that it is not working, In fact. that log statement is entirely correct. The configuration of item metadata is an object and when you log out an object it gets converted to a string and the default string conversion for objects results in [object Object]. If you want to the contents of the object in string form then you need
JSON.stringify(alarmIntervallMin8)
Or, just trust that your object is there and go the final step: alarmIntervallMin8.alarmIntervalMin and you should see your value.
Rich’s code should work too, and be more readable. It looks like the item name didn’t fully translate over from your original to his example, if you didn’t correct the item name then is it not a surprise you got an error.
Are you sure that LaCrossSensor1_Temperature is an Item? The error indicates that items.LaCrosseSensor1_Temperature returned null.
Are you certain you are on version 4.1 of openhab-js? You have to install it separately. It doesn’t come with the addon except in the OH 4.0 snapshots. The ability to use items.ItemName or item['ItemName'] wasn’t added until openhab-js 4.0, nor was the reimplementation of the metadata interface.
I’m working on a rule right as I type this that uses this syntax. Some examples:
No the original item is LaCrosseSensor1_Temperatur_LastUpdate
You truncated the _LastUpdate part in your example. So if this issue was not caught when OP tested your example code that is, in deed, the source of the error.