VS Code openHAB Extension!

thank you! I’ll try it :slight_smile:
Edit:
now i am getting a new error:
Error while connecting to openHAB REST API. getaddrinfo ENOTFOUND openhabianpi.local openhabianpi.local:8080

How do you enable additional security layer?

Hello! After using the IP adress of my Raspberry instead of openhabian.local as host it worked.

Edit: Not true.
I can now use the preview of my sitemap but i still get the error:
Error while connecting to openHAB REST API. connect ECONNREFUSED 127.0.0.1:8080

Could you try placing the config in the user settings instead of workspace settings?

Hi!

Got it working now :slight_smile: Thank you!
Here is my config for Workspace and User:

I haven’t used it myself yet, so only thing i can do is point you to some tutorials about that topic:

General Security Informations:
https://docs.openhab.org/installation/security.html

Apache Version:

NGINX Version:

I’m using VS code now for while to script and edit config files of OpenHAB. I’m also pushing everything to a git repository. In the past I was just using the Git CMD, but since VS Code also supports Git + network shares I wanted to try this.
The strange thing is that VS Code seems to see repositories that aren’t there. I’m testing this with a network share on my Pi3 (also tried this on another Pi with LibreElec installed, same…). When I do pull the same remote repository to a share on my NAS, the behavior is as it should.

I looks like VS Code doesn’t like a Pi-share… Git CMD or Git GUI are both working fine in the same shares.
I’ll try to add a screenshot this evening if this is not clear.

The syntax check isn’t working for me. There are no logs in the openhab server logfile. The ui preview works in vs code. It looks like that vs does not connect to the lsp port. Netstat shows the port as LISTEN.
My settings.json looks like

{
“openhab.host”: “192.168.1.201”,
“git.ignoreLegacyWarning”: true,
“openhab.port”: 8080,
“openhab.karafCommand”: “ssh openhab@%openhab.host% -p 8101”,
“openhab.useRestApi”: false,
“openhab.lspEnabled”: true
}

OH is official v2.2.
VS Code: 1.20.1
OH Ext.: 0.3.5
Can somebody help please.

I’m pretty sure you have to enable the use of Rest Api.

"openhab.useRestApi": true,

Setting openhab.useRestApi to true is killing my server. cpu load is at maximum and will run into java heap space error.
But if its enabled the vs code is showing the things and items. I thing syntax checking is not done via rest but via lsp server?

Which hardware do you use?

VM with 1 CPU and 1gb RAM in i5 (since 3 years)

I’m using the same settings (here xen4.4.1), for vm 1 CPU, 1GByte RAM, but no problems with “killing the server”. How many bindings/rules/items do you use?

~400 rules
~1100 items
14 bindings

that’s quite a bit… I don’t coming near this, so, we have to ask others if they observe problems…

I’ve recently started using this, but I don’t have full syntax checking. Also clicking on preview gives Error command’openhab.basicUI’not found

I’m running an APT-GET installed Snapshot build 1216

My user settings are:

{"openhab.host": "xxx.xxx.xxx.xxx",
 "openhab.port": 8080,
 "openhab.useRestApi": true,
 "openhab.username": "",
 "openhab.password": ""
} 

xxx.xxx.xxx.xxx is the ipAddress of my OpenHAB machine

In Workspace settings is the folder path to my OpenHAB machine, and an empty field called “settings”
Folder Settings are empty.

To get it working I just added the server address and left the rest as defaults, only adding the extra when it didn’t work.

What am I missing?

You shouldn’t need to add anything manually. Just right-click on the gear wheel in the left lower corner and chose settings. You get at least 2 lists, left side is standard settings, this one is sorted by categories, so scroll down to openHAB Configuration. To change settings, just click at the pen in the line and chose change settings, this will copy the line immediately to the right side.
Be aware that there are user settings and workspace settings. I don’t know which one will overrule the other.
For full syntax checking you will need at least

 "openhab.host": "xxx.xxx.xxx.xxx"
 "openhab.port": 8080
 "openhab.useRestApi": true
 "openhab.lspEnabled": true
 "openhab.lspPort": 5007

And it’s a good idea to set

 "openhab.restCompletions": true

either for getting code completions. I’m not sure it you have to enable the RestApi for syntax checking, but you definitely have to for code completions.

2 Likes

@kubawolanin just set up SSH to connect to openHAB with private key and use the tail for the log. i also set up sftp to automattically transfer files. I will post my solution here maybe you think it could be helpfull to be added to the github readme. Or have some ideas to improve the Setting up proccess to make it easier?

  1. Setting up logging without exposing system to the public, and keeps working after upgrade. One time setup. openhab-cli command had some Problems but a good solution could be found.

1.1 PC used to edeit code with VSCode, generate key, this is necessary to avoid typing in the password every time.

ssh-keygen

1.2 Copy content of public key id_XXXXX.pub to Openhab Server

// Linux
ssh-copy-id -i ~/.ssh/mykey user@host
// Windows
scp "<PathToFile>/id_XXXXX.pub" pi@<Ip-of-openHAB-Server>:~/.ssh/authorized_keys
//scp "<PathToFile>/id_XXXXX.pub" openhabian@openhabianpi:~/.ssh/authorized_keys
// manually 
// copy content of `id_XXXXX.pub` to   
// existing or created file `/.ssh/authorized_keys`

1.3 Change shh config on server

sudo nano /etc/ssh/sshd_config
PubkeyAuthentication yes
AuthorizedKeysFile  .ssh/authorized_keys .ssh/authorized_keys2

1.4 Set Karaf command in VSCode, press ctrl+comma and add at the left left side

// I am not a fan of this, needs extra configuration, is not persistent when updating, and opens system to public
"openhab.karafCommand": "ssh username@%openhabhost% -p 8101 -t 'log:tail'",
// Don't do this there are too much drawbags
"openhab.karafCommand": "ssh username@%openhabhost% openhab-cli console -p habopen `'log:tail'`",
// do this, one drawback it is not colored. 
"openhab.karafCommand": "ssh username@%openhabhost% tail -q -f /var/log/openhab2/events.log -f /var/log/openhab2/openhab.log",

Drawbacks solution two

  1. Do not append 'log:tail' there seems a bug which will prevent the log from beeing continously written. Do we have to type something again. No password but log:tail.
  2. A second bug seems to be ansi color codes which i did not get to work propperly yet. The escape sequence for Ansi codes get stripped of when piped to the terminal console from the ssh session. But the Code part will persist and clutter the Output.
  3. Karaf console default timeout is set to 30 minutes, so it has to be reopened often or the default settings have to be changed.
  4. It is a ssh connection in a ssh connection.

Restart VSCode, openhab extension seems to have a missing feature which reads in configuration when they are changed it only loads new/changed configs when restarted.

1.5 Try to run Karaf ctrl+shift+p type openh select openHAB: Open Karaf Console.
1.6 Wait for the connection and the tailing log.

The best thing is you have access to the console changing debug levels retrieve infos etc, imho very ussefull. You can use the terminal like an ssh session.

Additional information i use windows 10 build in openSSH.

  1. Add folder /etc/openhab2 as sftp to automatically transfer files when you save from/to Server to/from pc wich edits the files.

2.1 install SFTP extension, restart VSCode
2.2 Open a folder which is destined to be your new openhab content folder. Menu File/Open Folder
2.3 Configure it SFTP, press ctrl+shift+p type sftp choose SFTP: config
My basic setting looks as follows, have a look at the project page for more details

{
    "protocol": "sftp",
    "host": "<IP>",
    "username": "<Username e.g pi>",
    "password": "<password e.g raspberry>",
    "context": "<path to your local openHAB files e.g D:/CODE/OpenHAB_FILES>",
    "remotePath": "<path to remote openHAB files e.g /etc/openhab2/>",
    "uploadOnSave": true
}

2.3 Fetch remote files, press ctrl+shift+p type sftp choose SFTP: Download
all your remote files should be in your local folder now.
2.4 Edit a file and watch the log go crazy! :wink:

@kubawolanin edited the logging now it works very nice. This is now a very neat working environment, do you think this could be part of the readme?

2 Likes

Did all that, restarted Visual Studio Code and still opening the sitemap and clicking on the top right openhab icon gives me

Error command ‘openhab.basicUI’ not found

Is there any way of checking that it can access Rest and anything else that it needs? I can see a bunch of JSON formatted text in a web browser if I type in

http://xxx.xxx.xxx.xxx:8080/rest

Does that mean rest is OK?

1 Like

If it look similar to this, yes:

Which version of the Extension are you using?