VS Code OH5 - Error while connecting to openHAB REST API

Hi OH community

I recently did a fresh install of OH5 - OpenHabian on a RPi4. I have used VS Code with OH3.3 in the past without any issues. However I am getting an error when trying to connect.

(I can easily browse the folders in OH5 via Samba from my OS X)

Can you guide me to how to getting this solved so I can start configuring my home?

Thanks

Thomas

Usage of deprecated config => openhab.host <= detected.
Usage of deprecated config => openhab.port <= detected.
Usage of deprecated config => openhab.username <= detected.
[Error - 22.58.59] Connection to server is erroring. Shutting down server.
[Error - 22.58.59] Connection to server is erroring. Shutting down server.
Could not reload items for HoverProvider
Could not reload items for Items Explorer
Could not reload items for Things Explorer
---
    Error:
        Error while connecting to openHAB REST API.

    Message:
        Error: getaddrinfo ENOTFOUND openhabianpi

Don’t know exactly, if I can help, but just a question !

Do you have a “settings.json”-File for VSC in your “OPENHAB_CONF | /etc/openhab” - folder. The file is stored in a sub-folder “.vscode” an d looks like

{
    "openhab.connection.host": "192.168.178.76",      // IP-Adresse OH-Rechner
    "openhab.connection.port": 8080,
    "openhab.languageserver.remoteEnabled": false,
    "openhab.languageserver.remotePort": 5007,
    "openhab.useRestApi": true,
    "openhab.consoleCommand": "ssh openhab@%openhabhost% -p 8101",      
    "openhab.connection.authToken": "oh.vscode.thisIsTopSecrt"
}

in my setup.

And I’ve linked the folder as a drive to my windows-computer.

Thanks for sharing Peter!

I copied yours :slight_smile:
it guided me somehow in the right direction, however, my REST API is disabled as it was asking for both password and username. I added it to the settings.json file in VSC.

 "openhab.connection.basicAuth.username": "VerySecret",
 "openhab.connection.basicAuth.password": "VerySecret",

I also created the Auth Token code, copied in, and it confirmed it was configured in the Output window.

Now I get the error:

Could not reload items for Items Explorer
Could not reload items for Things Explorer
---
    Error:
        Error while connecting to openHAB REST API.

    Message:
        Error: Request failed with status code 401
---

Is it just me, or have this become much more complicated to set up the configuration environment compared to OH 2 and 3? :slight_smile:

Just a silly question(s) from my side.

You are using the host-connection-adress of your Pi ?!

Normally if you’re using a (the) token, you don’t need “user/password” anymore.

Have you looked if your enviroment-variables are set ?

hab4@raspi58:~ $ env | grep -i open
OPENHAB_HOME=/usr/share/openhab
OPENHAB_USERDATA=/var/lib/openhab
OPENHAB_CONF=/etc/openhab
OPENHAB_HTTPS_PORT=8443
OPENHAB_BACKUPS=/var/lib/openhab/backups
OPENHAB_HTTP_PORT=8080
OPENHAB_LOGDIR=/var/log/openhab
OPENHAB_GROUP=openhab
OPENHAB_RUNTIME=/usr/share/openhab/runtime
OPENHAB_USER=openhab
hab4@raspi58:~ $ openhab-cli info

Version:     5.0.2 (Build)

User:        openhab (Active Process 34564)
User Groups: openhab tty dialout audio bluetooth gpio

Directories: Folder Name      | Path                        | User:Group
             -----------      | ----                        | ----------
             OPENHAB_HOME     | /usr/share/openhab          | openhab:openhab
             OPENHAB_RUNTIME  | /usr/share/openhab/runtime  | openhab:openhab
             OPENHAB_USERDATA | /var/lib/openhab            | openhab:openhab
             OPENHAB_CONF     | /etc/openhab                | openhab:openhab
             OPENHAB_LOGDIR   | /var/log/openhab            | openhab:openhab
             OPENHAB_BACKUPS  | /var/lib/openhab/backups    | openhab:openhab

URLs:        http://192.168.178.76:8080
             https://192.168.178.76:8443


Maybe that doesn’t help, but it can’t damage :wink:

Edit:

Ijust produced this error-message in VSC after I stopped OH on my Pi (sudo systemctl stop openhab)

openHAB vscode extension has been activated
Could not reload items for Items Explorer
Could not reload items for Things Explorer
---
    Error:
        Error while connecting to openHAB REST API.

    Message:
        Error: connect ECONNREFUSED 192.168.178.76:8080
---
---
    Error:
        Error while connecting to openHAB REST API.

    Message:
        Error: connect ECONNREFUSED 192.168.178.76:8080
---

Hi Peter

I have tried evening after evening doing trial and error now. I still get the same error. I can see that it does not accept that I don’t have a username and password when I copy in the TokenAPI.

Adding to that. I can’t manage to make SSH to the RPi, I am getting the message that the RSA key have expired, and that I need to copy it to a file in a folder called .ssh

I am running around in circles now unfortunately. Such a shame that I can’t get the editor environment up and running so I can start working on setting up OH5

As I have no Mac, I don’t know how the connection works there.

I have a Windos-Machine. And even if I have Samba-Shares for use with ssh.

To use VSC from my Windows-Machine to get Acces to my OH-Folders I have to connect my openHAB-Pi as a drive to Windows

First thing first: you don’t need VSC at all for configuring openHAB, you can fully configure openHAB by MainUI.

Only if you want to configure openHAB via text files, an editor is needed, from one of these is Visual Studio Code.

Please note the docs for configuring VSC with openHAB:

If that is the case, do the following from the docs:

Please be sure to mount the network share to a drive letter.

Please mount only the openHAB conf folder to that drive letter!

Before opening that drive letter content in VSC, make sure you only have one valid place for your VSC settings on your Mac, as there are several ones:

I don’t know the locations on a Mac, you need to read up on this.
I strongly recommend to use only the workspace settings, not the global settings. Comment out all settings in your global settings.json.
Then create in your openHAB conf folder a hidden directory .vscode/settings.json

Copy your settings to that file, don’t forget to create the token, here is another example:

{
    "openhab.connection.host": "192.168.2.212",
    "openhab.connection.authToken": "oh.restapi.yourSecretToken",
    "openhab.connection.port": 8080,
    "openhab.languageserver.remoteEnabled": false,
    "openhab.languageserver.remotePort": 5007,
    "openhab.useRestApi": true
}

Now open the drive letter in VSC and you should see all files needed.

Don’t enable the ssh way at this moment, first try the simple way. The goal is to only have one single place for your VSC settings!

(This is a manual installation, yours may look a bit different)

1 Like

Hi Sihui

I followed your step by step guide and very good explanation. It works now!

I had mixed up settings across global / workspace “settings.json”
I copied in your suggested config, and it worked straight away!

As i understand it, the REST API allows usage of the language server function, so I can get config language support during my work - that would work if I change the config from “false” to “true” - is that correct understood?

The reason for why I try to stick to config via text files is this is how I started in OH2/3, and I have an old ELKO IHC sytem with an old binding, which I previously had successfully integrating via text configuration.

Thank you very much for your help!
BR

Thomas

I am not sure as I have never used that.
More important, I think, is the openHAB Extension: