Code completion for rules not working (or is it?)

I have been at this all day and I am at a point at which I am not sure anymore what to expect.

I have OpenHAB running on a Synology with a Z-Wave setup. As far as bindings, things and items are concerned everything works. So I jumped into rules. The rules binding is not available for some reason so I went straight to Visual Studio Code with the OpenHAB plug-in.

I do get code coloring and I do get something presented to me when I press Ctrl+Space but it is utterly useless and does not seem to make much sense in the context. I find mainly words I typed earlier in that list, no properties or methods related to the object before the dot.

So… Is code completion not working or is it just useless?

Here’s my settings.json file. I made sure it is the same for user and workspace because I do not understand the difference.

{
    "openhab.useRestApi": true,
    "openhab.host": "192.168.1.102",
    "openhab.port": 8080,
    "openhab.username": "MyUserName",
    "openhab.password": "MyPassword",
    "workbench.iconTheme": "openhab",
    "telemetry.enableTelemetry": false,
    "telemetry.enableCrashReporter": false
}

I do see my things and items under the OpenHAB tab. I just cannot make any rule work because I can only guess what conversion options I have when I simply want to pull a value from an item object to compare to a constant number. I can inspect the openhab.log file for errors and this, although a poor way of debugging, did get me a couple of steps further but it does not get me beyond this:

[ntime.internal.engine.RuleEngineImpl] - Rule 'OverVoltage Response': An error occurred during the script execution: index=1, size=1

Much to my surprise I cannot find any example of what I would have expected to be a very common scenario: when an item value is updated I want to see if the value exceeds some number and if so, act on it.

I realize I am asking two question but to me they are strongly related: the lack of IDE support inhibits me to write correct rules. I hope there is something wrong with my setup because the way it is behaving on my system, the plug-in is quite useless.

This is the rule (the latest thing I tried). I am struggling with the conditional expression that compares the item value with the number 252. I expect to be able to get a number from the item by doing something like Output_Voltage.state.value or Output_Voltage.value or Output_Voltage.AsNumber. The IDE is not helping me, I have no object browser and I do not know where my item definitions are (I created them using the paper UI).

rule "OverVoltage Response"
when
    Item Output_Voltage received update 
then
    var voltage = Output_Voltage.state.toString()

    if (voltage > "252.0")
    {
        logInfo("Over voltage detected > 252 Volt), => switching load ON.")
    }
    else
    {
        logInfo("All well, voltage is under 252 Volt.")
    }
end

Please post your rule here

I updated the question, included the rule.

It’s the logInfo() command. The definition is logInfo(logger as string,message as string), so you are missing a parameter.

In question of code completion, there are different reasons why that could fail. E.g. it’s depending on how you are connecting to the server (samba…)

rule "OverVoltage Response"
when
    Item Output_Voltage received update 
then
    val threshold = 252.0
    if (Output_Voltage.state.floatValue() > threshold) {
        logInfo("OverVoltageRule", "Over voltage detected > {} Volt), => switching load ON.", threshold)
    } else {
        logInfo("OverVoltageRule", "All well, voltage is under {} Volt.", threshold)
    }
end

FWIW, I haven’t really seen any code completion working for me on VSCode (I used to use RulesDSL but now switched to Jython for rule coding). Not even to complete previously known variable names. I haven’t been bothered enough to figure it out. For now I’m just happy that I have some sort of syntax highlighting.

Indeed, the rule runs OK now. Thank you.

I do not see how the file system protocol could interfere with code completion though. I am accessing the OpenHAB files on the Synology over Samba 3.0 but a REST service uses HTTP and talks to a different process. These should be independent.

OK, so I am not the only one. Then I might as well stop trying.

All-in-all I am quite excited about OpenHAB. I am coming from Domoticz so the bar was not set very high and I have a lot of criticism already but at least with this it is obvious some folks actually thought about it before something was slapped together. This lack of code completion is a bit disappointing though.

Thank you.

I’d highly recommend using JSR223 rules engine instead of rulesdsl. It takes a bit of time to set up currently but it’s worth the effort. See [beta testers wanted!] Jython addon w/ helper libraries (requires OH 2.5.x)

PS you can continue using rulesdsl even if you’ve set up jython. They can run side by side.

I think the code completion relies on a LISP server provided by oipenHAB. That need to be enabled, and VSCode talking to right url/port

1 Like

I would not know how to make sure that LISP server is active or not. But it got weirder…

After giving me seemingly random non-sense like words I typed earlier and/or a list of my items in any editing contexts, tonight it started to show stuff that looks sort of right:

It does not give me useful options in all cursor positions but here after “state.” it actually look like sensible options. I have only been messing with editor encoding options, did not change anything substantial that could explain the improvement. It is still far from good but this is some guidance. There was some more in the list:

These look like methods and properties of state.

I give it a 3 out if 10.

I suppose you could look, in your openhab.log

2019-12-18 22:20:11.347 [INFO ] [thome.model.lsp.internal.ModelServer] - Started Language Server Protocol (LSP) service on port 5007

and VSCode extension settings

to see if they at least match.
I expect there’s other ways to have access problems from wherever you run VSCode to wherever you run openHAB.

You may just have to ask for your money back.

You may just have to ask for your money back

That, and some flowers to get me back into my regular good mood again.

I will check this out tomorrow, thanks again for helping out a grumpy old man.

1 Like

To bring some light in the dark:

At least some kind of completion “should” come from the LSP server implementation,
but i doubt that it is working completely.
There was a big contribution for this and it has never been touched significantly since that and is still out of my knowledge.

The code completions you are seeing above seem to be taken from vscode directly somewhere,
which is trying to at least provide something at all.
Needless to say that this can’t be helpful for you.

And as always (and said already dozens of times):
Anyone with some programming skills is welcome to help out improving the extension.
Language Server Protocol is and will be out of my personal scope for quite some time.

2 Likes

afaik it is essential for a properly working code completion, that both work directory and settings for the vscode plugin are correct.
I’m using a standard openhabian installation, but it’s on a vm, so the name of the openHAB server is different to openhabianpi :slight_smile:
But that’s my configuration:

{
    "openhab.host": "192.168.178.210", 
    "openhab.remoteLspEnabled": true,
    "openhab.useRestApi": true,
    "openhab.port": 8080,
    "openhab.remoteLspPort": 5007,
    "openhab.username": "",
    "openhab.paperPath": "paperui",
    "openhab.sitemapPreviewUI": "basicui",
    "openhab.password": "",

    "terminal.integrated.shell.windows": "C:\\WINDOWS\\System32\\cmd.exe",
    "workbench.settings.openDefaultSettings": true,
    "editor.renderWhitespace": "all"
}

I also have bound the openhab-conf directory via net use to the drive letter O (like openHAB…) :slight_smile: and opened this directory as working directory in vscode
-> code completion works like a charm.

Please be aware that some plugin settings got renamed from version to version, vscode will not change the settings automatically, to please ensure all settings have the correct parameter name.

AFAIK code completion is working for me as well, also on a openhabian installation, but this on a raspi3. VSC is installed on a Win10 maschine and the conf folder is setup as a network drive.

My settings.json looks like this:

{
    "openhab.host": "MyMaschineName",
    "openhab.port": 8080,
    "openhab.remoteLspPort": 5007,
    "workbench.startupEditor": "newUntitledFile",
    "workbench.iconTheme": "openhab",
}

Thanks for the example configuration file.

I added the LSP-related parameters:

“openhab.remoteLspEnabled”: true,
“openhab.remoteLspPort”: 5007,

and I am afraid I do not notice an improvement (I restarted OpenHAB).

In Paper UI I have this option under Services->Configuration->Misc to configure “Language Server (LSP)”. All I see here is a port setting and this is already 5007 by default. In expert mode I have the option to add a parameter.

Is there anything I should do here?

This is what mine looks like at similar situation
vscodecomp

1 Like

I had something similar two days ago but now it is just items and/or thing names again.
I am wondering though:

Do you get this the moment you type the dot or do you still have to press Ctrl+Space?
What does it look like for the item level (after the dot before “state”)?

That was an existing line, in which I inserted a dot.
I have an unfair advantage running VSCode on the same laptop hosting openHAB, so I don’t get network delays.

The following was typing a completely fresh new rule line, Item was autocompleted from a proffered list of names like test_xxx, then typed the dot
vscodecomp