openhab / openhab-js

openHAB JavaScript Library for JavaScript Scripting Automation
https://www.openhab.org/addons/automation/jsscripting/
Eclipse Public License 2.0
38 stars 31 forks source link

items.addItem / createItem #114

Closed JacekKac closed 2 years ago

JacekKac commented 2 years ago

can you update documentation and include add/create item syntax? i am not sure how to use in in javascript:


items.addItem({
    type: 'Number',
    name: 'nTest',
    label: 'Test Number Item'

  });

2022-05-11 13:07:09.186 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/etc/openhab/automation/js/ItemCreator.js': org.graalvm.polyglot.PolyglotException: TypeError: s.replace is not a function ot.PolyglotException: TypeError: s.replace is not a function at .safeItemName(/etc/openhab/automation/node_modules/openhab/items/managed.js:457) ~[?:?] at .createItem(/etc/openhab/automation/node_modules/openhab/items/managed.js:271) ~[?:?] at .:program(ItemCreator.js:24) ~[?:?] at org.graalvm.polyglot.Context.eval(Context.java:379) ~[?:?] 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:56) ~[?:?] at org.openhab.automation.jsscripting.internal.scriptengine.InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.eval(InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.java:79) ~[?:?] at org.openhab.automation.jsscripting.internal.scriptengine.DelegatingScriptEngineWithInvocableAndAutocloseable.eval(DelegatingScriptEngineWithInvocableAndAutocloseable.java:56) ~[?:?] at org.openhab.automation.jsscripting.internal.scriptengine.InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.eval(InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.java:79) ~[?:?] at org.openhab.core.automation.module.script.internal.ScriptEngineManagerImpl.loadScript(ScriptEngineManagerImpl.java:189) ~[?:?] at org.openhab.core.automation.module.script.rulesupport.loader.ScriptFileWatcher.createAndLoad(ScriptFileWatcher.java:251) ~[?:?] at org.openhab.automation.jsscripting.internal.fs.watch.JSScriptFileWatcher.createAndLoad(JSScriptFileWatcher.java:55) ~[?:?] at org.openhab.core.automation.module.script.rulesupport.loader.ScriptFileWatcher.importFile(ScriptFileWatcher.java:231) ~[?:?] at org.openhab.core.automation.module.script.rulesupport.loader.ScriptFileWatcher.lambda$2(ScriptFileWatcher.java:223) ~[?:?] at java.util.Optional.ifPresent(Optional.java:183) ~[?:?] at org.openhab.core.automation.module.script.rulesupport.loader.ScriptFileWatcher.importFileWhenReady(ScriptFileWatcher.java:221) ~[?:?] at org.openhab.core.automation.module.script.rulesupport.loader.ScriptFileWatcher.processWatchEvent(ScriptFileWatcher.java:198) ~[?:?] at org.openhab.automation.jsscripting.internal.fs.watch.JSScriptFileWatcher.processWatchEvent(JSScriptFileWatcher.java:49) ~[?:?] at org.openhab.core.service.WatchQueueReader.lambda$5(WatchQueueReader.java:356) ~[?:?] 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:834) [?:?]



what is the difference between create and add item?
florian-h05 commented 2 years ago

Hello @JacekKac,

as you can see at https://github.com/openhab/openhab-js#items and https://openhab.github.io/openhab-js/items.html#.replaceItem, the syntax for addItem() and replaceItem() (which adds the Item if it not exists) is already documented and these methods have been verified to work.

In recent PR #109, big improvements were made to those methods and the difference between createItem() and addItem() has also been discussed there. To conclude about those two, as a user you can forget that createItem() exists.

Those changes are not available in the officially published version of openHAB yet, but you could install the most recent version of openhab-js with npm i openhab/openhab-js.

Could you please tell me your openHAB version (you can find it in the web ui under Help & About) and whether you have updated/installed openhab-js manually.

JacekKac commented 2 years ago

i don't have access to my openhab instance right know, but what i did was: installed openhab plugin from GUI, and then installed manuall addon by npm -i openhab/openhab-js in /etc/openhab/automation. i can use items.getItem but when i use items.replaceItem with given config (even basic) it fails.

florian-h05 commented 2 years ago

Hmm, once you have access to your openHAB instance again, can you upgrade openhab-js with npm i openhab in /etc/openhab/automation/js?

Two days ago, a new version was released to npm that includes the recent changes to replaceItem.

JacekKac commented 2 years ago

ok after upgrading openhab to Version: 3.3.0.M5 (Build) and npm i to newest code it is working.