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