How to access items from npm own JavaScript library?

Hi, everyone.

I’m running OH 4.3.1 on a Raspberry Pi (CENTOS 9). I’m migrating my rules to JavaScript (ECMAScript 2022+), and I’m having an issue with my own library. I use text-based configuration.

I have created a library called “lights“ and installed it via npm under /etc/openhab/automation/js/node_modules/lights with the name index.js

This is the relevant part of the library’s code that I’m trying to test:

/*
    Version: 1.0
*/

// remove namespaces that are not needed by your code
//const { actions, cache, items, things, time, triggers, utils, Quantity } = require('openhab');
const { items } = require('openhab');
const { HSBType, DecimalType, PercentType, ON, OFF} = require('@runtime');

/**
 * @note Changes the color of the lights in the requested room to yellow light study mode
 * @param roomName: The code of the room to be updated (e.g. LR, MBR, KT, etc.)
 */
function setYellowLightStudyScene(roomName) {
    console.info("Lights Library > Set Yellow Light Study Scene > STARTING.")
    var group_lights_color = items.getItem("group_%s_Lamp_1_Color", roomName)
    if (group_lights_color != null) {
        console.info("Lights Library > Set Yellow Light Study Scene > Color lights found.")
        console.info("Lights Library > Set Yellow Light Study Scene > Updating light color")
        group_lights_color.sendCommand(new HSBType(new DecimalType(38), new PercentType(54), new PercentType(100)))
    } else {
        console.info("Lights Library > Set Yellow Light Study Scene > Cannot find the group_%s_Lamp_1_Color. Trying with Ambiance lights.", roomName)
        console.info("Lights Library > Searching for group_%s_Lamp_1_Brightness and group_%s_Lamp_1_ColorTemperature", roomName, roomName)
        var group_lights_brightness = items.getItem("group_%s_Lamp_1__Brightness", roomName)
        var group_lights_colorTemperature = items.getItem("group_%s_Lamp_1_ColorTemperature", roomName)
        if (group_lights_brightness != null && group_lights_colorTemperature != null) {
            console.info("Lights Library > Set Yellow Light Study Scene > Ambiance lights found. Updating the color of the lights to yellow")
            group_lights_brightness.sendCommand(100)
            group_lights_colorTemperature.sendCommand(100)
        } else {
            console.error("Lights Library > Set Yellow Light Study Scene > Cannot find the ambiance light items either. No lights were updated.")
        }
    }
    console.info("Lights Library > Set Yellow Light Study Scene > ENDING.")
}

I’m testing it using the Scratchpad with the following code:

var Lights = require('lights')
Lights.setYellowLightStudyScene("MBR")

Here is the item configuration:

Group:Color             group_MBR_Lamp_1_Color "Master Bedroom Lamp Color" <light>      (group_MBR_Lamp_1, group_Lights_Color) ["Setpoint"]
Group:Dimmer            group_MBR_Lamp_1_ColorTemperature "Master Bedroom Color Temperature" <light> (group_MBR_Lamp_1) ["Setpoint", "ColorTemperature"]

This is the outcome:

03/15/2026 07:57:55.632 [INFO ] [openhab.event.RuleUpdatedEvent       ] - Rule 'scratchpad' has been updated.
03/15/2026 07:57:57.332 [WARN ] [tion.openhab-js.items.ItemPersistence] - RiemannType is not available on your openHAB version!
03/15/2026 07:58:02.487 [INFO ] [enhab.automation.script.ui.scratchpad] - 2026-03-15T07:58:02.485+01:00[Europe/Berlin]
03/15/2026 07:58:02.493 [INFO ] [enhab.automation.script.ui.scratchpad] - Lights Library > Set Yellow Light Study Scene > STARTING.
03/15/2026 07:58:02.498 [INFO ] [enhab.automation.script.ui.scratchpad] - Item: null
03/15/2026 07:58:02.503 [INFO ] [enhab.automation.script.ui.scratchpad] - Lights Library > Set Yellow Light Study Scene > Cannot find the group_MBR_Lamp_1_Color. Trying with Ambiance lights.
03/15/2026 07:58:02.508 [INFO ] [enhab.automation.script.ui.scratchpad] - Lights Library > Searching for group_MBR_Lamp_1_Brightness and group_MBR_Lamp_1_ColorTemperature
03/15/2026 07:58:02.513 [ERROR] [enhab.automation.script.ui.scratchpad] - Lights Library > Set Yellow Light Study Scene > Cannot find the ambiance light items either. No lights were updated.
03/15/2026 07:58:02.517 [INFO ] [enhab.automation.script.ui.scratchpad] - Lights Library > Set Yellow Light Study Scene > ENDING.
03/15/2026 07:58:02.521 [INFO ] [enhab.automation.script.ui.scratchpad] - Ending

Running the library’s code in the Scratchpad works, but calling the library with the same code doesn’t. It doesn’t work with or without the OH JS library call.

const { items } = require('openhab');

Am I missing something?

I don’t see an (obvious) issue, but the pattern matching in the item name might be an issue.

Can you try console logging all Items using items.getItems()?

It turns out, the getItem function doesn’t like this notation:

items.getItem("group_%s_Lamp_1_Color", roomName)

… it likes this one:

items.getItem("group_" + roomName + "_Lamp_1_Color")

Thanks for your prompt response, @florian-h05!

I was already suspecting that … how did you come to that notation?

As far as I remember, it’s a notation used when dealing with strings in many languages. It replaces some special placeholders (e.g., %s for strings, %d for decimals, etc.) with the value of the respective variable, but maybe it only works for printing.

That’s nothing supported by JS. If it works for console logging in JS Scripting, that’s because internally we delegate to Java logging, which supports that.

Probably I have it from Java in my mind and it works for some things. I’ll keep that in mind, thanks a lot!

1 Like

BTW, if you want a more elegant syntax than … + …, you can use:

`group_${roomName}_Lamp_1_Color`

I actually have indeed been looking for something more elegant, and I like this notation ${variable}, but I have migrated my rules from DSL to Scala, and now to JavaScript, and one of the biggest changes has been the string syntax, so I’m also looking for something standard to many languages, especially supported by Java.

I’ve never understood why Java is not one of the supported programming languages for OH. :man_shrugging:

1 Like

That’s a good question :joy:
I think the main issue might be the requirement to compile. Just FYI, there are a few marketplace add-ons supporting Java, and some authors may plan to open a PR for them.

Well, I was using Scala thanks to JRule, until it became unsupported more than a year ago. I went through the pain of recompiling JRule myself with every OH update to keep my automations working, until I couldn’t compile it with OH 5.0. Then I decided to move to an off-the-shelf rule system to avoid the same issue again.

Maintaining JRule (or a binding for that matter) is out of my current league, so I had to move on.

My current issue with JavaScript is the autocompletion ( VS Code OH5 - Error while connecting to openHAB REST API - #12 by apella12 ). It takes quite some time to find the proper methods just with the documentation.

By installing openhab from npm and importing manually const { items, rules, ... } = require('openhab'), your IDE will see the type definitions of openhab-js and provide autocompletion.

The script editor in the UI is also able to provide autocompletion.

Sorry, I confused the ticket. This was the right one: VS Code - Error while connecting to the openHAB REST API

With the UI, I haven’t been able to get data from the items, things, etc.:

Only some simple stuff:

I’ve tested with a new rule and with the Scratchpad, but got the same results.

I don’t remember whether I have installed OH using npm on my development computer. I’ll check that out.

Update: Autocomplete with the openhab-js library is now working on VS Code. If I could connect to the REST API and Language Server, would the autocomplete also consider the items and things in the OH server?

Items autocompletion works for me after typing items., but it isn’t implemented for items.getItem(...:

I am on openHAB 5.2.0.M1 though, but checking the demo.openhab.org instance on 5.1.3, this works there as well.
Thing name autocompletion isn’t implemented anywhere I think.

The language server is only needed for openHAB’s DSL. The REST API is where the information about Items & Things comes from, but the openHAB VS Code extension doesn’t provide Item name completion for JS. The extension has no active maintainer AFAIK, last commit three years ago: GitHub - openhab/openhab-vscode: VS Code extension for openHAB configuration files · GitHub. I can see a PR v1.0.1: JS hover tooltips and log-based state extraction by s0170071 · Pull Request #329 · openhab/openhab-vscode · GitHub that seems to add Item name autocompletion.

It’s working both ways for me. But I’m on 5.1.1 at the moment. Maybe there’s a regression?

@nelson.aponte, also, don’t forget about the developer sidebar where you can pin the relevant Things, Items, etc. and not only see them but interact with them, copy the UIDs, etc.

I have tested it on 5.1.3, 5.2.0.M1 and a milestone build from today, and it works on all three. So if it doesn’t work for @nelson.aponte , there must be a specific issue in his setup. Anything in the browser console?

Perhaps there is a difference with 4.3. Once I’m done updating the rules, I’ll update OH to 5+ and check again.

Thanks a lot, @florian-h05 and @rlkoshak!

1 Like

Hi, @rlkoshak and @florian-h05.

I upgraded yesterday to 5.1.3, and I can confirm that the Scratchpad does autocomplete, just as you mentioned @rlkoshak.

Only VS Code doesn’t autocomplete item names, even though the REST API returns all items. Is it a bug or is it expected?

It’s not implemented, so yes it’s expected. The good news is, that it is worked on, the VS Code extension slowly gets developed again after several years of no changes.