State of "Number" Group is type string

I’ve got this Group Item:

version: 1
items:
  gWindowsOpen:
    type: Group
    group:
      type: Number
      function: Count
      parameters:
        - OPEN
    label: Vensters geopend
    icon: window

And this code:

var gWindowsOpenState = items.getItem("gWindowsOpen").state;

console.log("gWindowsOpenState = " + gWindowsOpenState);
console.log("typeof gWindowsOpenState = " + typeof gWindowsOpenState);

… has this output:

11:18:22.031 [INFO ] [utomation.jsscripting.rule.scratchpad] - gWindowsOpenState = 1
11:18:22.034 [INFO ] [utomation.jsscripting.rule.scratchpad] - typeof gWindowsOpenState = string

That doesn’t look logical…

I tried looking around in the code (of GitHub - openhab/openhab-core: Core framework of openHAB · GitHub, I presume), but I was unable to import it into Eclipse IDE. (So if anyone has tips on that front, also welcome!)

Should I open a GitHub issue? Or am I wrong, and is this logical?

Ah that’s tricky! In JavaScript rules, the state of an item is a string; if you need the value as number type, you have to use numericState. This is the same for real number items and for number group items.

You’d need the openhab npm package with some kind of “Intellisense” like in VS Code/Codium to easily see this. Unfortunately I don’t know how to do this in Eclipse IDE.
For me it was enough to npm init and npm install the openhab npm package in my “working folder”. I have the js files in a separate folder and copy them to automation/js when they are finished.

You can see this in the docs and in the UI code editor as well, though I find it doesn’t always work in the UI code editor. It depends on how you retrieve the Item Oject I think (e.g. items.vWeather_Temp doesn’t work but items.getItem('vWeather_Temp') does. ctrl-space will show the list of potential completions same as in most IDEs.

There is also quantityState and boolState in addition to state and numericState.

Note, in MainUI the intellisense is Item aware.

I don’t think you get that with Eclipse, VSCode, et al even if you install the openhab-js node module.

The details in the docs can be found at JavaScript Scripting - Automation | openHAB

  • .state ⇒ string
  • .numericState ⇒ number|null: State as number, if state can be represented as number, or null if that’s not the case
  • .quantityState ⇒ Quantity|null: Item state as Quantity or null if state is not Quantity-compatible or without unit
  • .boolState ⇒ boolean|null: Item state as boolean or null if not boolean-compatible or is NULL or UNDEF, see below for mapping of state to boolean
  • .rawState ⇒ HostState

Of course… I knew this! :frowning:

Not quite sure what you’re asking here, but editing JavaScript in Eclipse is no good, so that shouldn’t be attempted. If what you wanted to do is to get OH core in Eclipse, that’s “easy”, that already works with the existing Eclipse setup you have for add-ons. All you need to do is to clone openhab-core somewhere, and then from Eclipse go to File → Import → Existing Maven Project → select the root folder of the repo you cloned. It will chew a bit, and then list all the core bundles already checked for import. You can unselect some if you want to, but I prefer to import them all.

However, this will take a long time, because it will immediately start to try to install dependencies and build all the bundles. We’re probably talking hours and hours, and it might not ever finish, because some of the org.openhab.core.model bundles have a tendency to enter an endless build loop. So, what you want to do is to go to Project → Build Automatically and uncheck that before starting the import. The import will still take time, but won’t go on for eternity. Once all the bundles have been imported, mass select and close all/most of them, only open the bundles you’re interested in debugging. Make sure that none of the org.openhab.core.model bundles are open at the very least, and then reenable Build Automatically. It will chew for a while, but should eventually get there.

Once that’s in place, you can resolve the “demo app” and run it as usual, using the debugger where you want to.

I can explain why this doesn’t always work, it’s because it’s something of a nightmare to make. “Intellisense” is an MS trademark, so it’s not called that outside VS. But, the functionality is the same, in code mirror (that is used in MainUI) it’s simply called “hinting”. I’ve written some “hinting” for YAML rules when doing the work on YAML rules, and to be frank, you pretty much have to code every situation where you want a lookup to work. I was shocked at how primitive this system is, you have to handle indentation level, looking up parent elements, cursor position, what and where to fill in etc. yourself from JavaScript. I think making the hinting do all you’d want it to is pretty much a “forever project”.