Controlling OpenHAB from Emacs

Yes all the memes are true! Once you start using Emacs (and especially once you get a taste of Emacs Lisp) you start wanting to do “everything” from Emacs! :grinning:

I realize there are a lot of people here who prefer GUI. However from perspective of keyboard driven user like myself (tiling window manager, Emacs, etc.) it’s a hassle to reach for the mouse, switch to browser and use GUI. I prefer few keypresses in completing-read style interface (without needing to leave comfort of Emacs, where I spend a lot of my computer time) and have cobbled together interfaces for almost everything this way by now. And the other day, I did for OpenHAB, too. :grinning: So I wanted to share it.

There is only one function, which works via OpenHAB’s REST interface, so make sure you have enabled that. It’s a very simple function which operates on a list of Items you provide to it and allows you to change them to whatever states you define. No querying for state or anything fancy like that (because most of times I simply want to turn ON/OFF some light or similar…).

First define our variables and one function:

(defvar my-openhab-command-list '()
  "List of possible commands to issue.

These will be used by the function `my-openhab-command' to select
a command to send.")

(defvar my-openhab-item-list '()
  "List of item names to control.

These will be used by the function `my-openhab-command' to select
an item to command.")

(defvar my-openhab-base-url nil
  "Full (base) URL of OpenHAB server.

Must include protocol (default is http) and port (default is
8080), e.g.:

However do not include any parts of REST path after port.

Used by function `my-openhab-command' for its URL argument.")

(defun my-openhab-command (&optional item command)
  "Send COMMAND to ITEM in OpenHAB via REST interface."
  (let ((_item (or item
                     (completing-read "Item: "
        (_command (or command
                        (completing-read "Command: "
     (concat "curl -X POST "
             "--header 'Content-Type: text/plain' "
             "--header 'Accept: application/json' "
             "-d '" _command "' "
             my-openhab-base-url "/rest/items/" _item))))

Execute those forms above in the normal way (Emacs users should already know, but let me know if anyone needs help with this).

Then add the following settings to your init file, and execute them, too.

Now obviously these are some of my own items, I include them as examples but substitute your own.

(setq my-openhab-command-list '("ON" "OFF"))

(setq my-openhab-item-list '("i433_remote_button_1of5 Bedroom light string"
	            		     "i433_remote_button_2of5 Living room light string"
            			     "i433_remote_button_3of5 Christmas palm tree light string"
            			     "i433_remote_button_4of5 Front window seasonal light string"
            			     "i433_remote_button_5of5 Dining room main light"))

(setq my-openhab-base-url "")

One important note about the format of the item list. For each item in list, the first “word” should be your actual item name in OpenHAB. Then (optionally) one or more spaces, followed by some disambiguation terms, reminders for yourself or friendly names, rooms or whatever you like. In completing-read style interface (which above function implements), you just start typing whatever including for example “living room” and the selection list will get narrowed to this and you press RETURN. The function will take the whole line and then pick off the first contiguous “word” (space separated) and send that to OpenHAB along with your command (ON/OFF or whatever you have defined/selected).

Please do let me know if you end up using this. Mostly I am wondering if I am the only Emacs user here at OpenHAB. :smile:


I assume this does not work with the OH3 authentication. I also assume you are running emacs on your TRS-80 computer?

Your one of those guys :laughing: I fondly remember the vi vs emacs battles back in the day. BTW, I’m a vi guy :grinning:

Why you have to go and ruin my fun, Bruce?! :grin:

Jokes aside, that’s probably a really good point. I am still on 2.5 as I am taking more of a “wait and see” approach because I am not in a hurry and everything still works fine for me.

Whenever I eventually get around to upgrading to 3.0, I will try and remember to update this post if/when I update my function. There are TLS libraries in Emacs so it should still work but maybe need to add some auth stuff to curl or…? I’ll cross that bridge when I come to it.

Nah. just one of first experiences with a computer back in the day, hence the moniker. I am much more of an Armbian guy when it comes to small home servers / SBC. Currently running OpenHAB on an ODROID-XU4 (previously on Cubietruck).

No u :stuck_out_tongue:

Where is the “Controlling OpenHAB from Vi” post?! :stuck_out_tongue: :laughing:


The Stable release for all of this year has been OH3. Why are you developing for something old?


You are asking this to an Emacs user? :laughing:

1 Like

Emacs is still my go-to editor. :+1:

1 Like

As a former UNIX Admin it is my :run-away" editor.

1 Like

I don’t use emacs; I am a vim guys :). But I upvoted you for the efforts. This would help other emacs users and also make the OH ecosystem even more diverse. Besides HABapp, this is the second third parties I am aware of.


Hehe I am a bit bias now as I fully commit to the HABapp framework. So have more third parties system using the REST API will ensure that it continues to have the developer’s attention ;).

But yeah emacs and vi/vim is a religious war that we shouldn’t even open. For development, a proper IDE with vim plug-in such as Eclipse or PyCharm would beat both though :).

Are you going to compete with the VS Code plugin?

Some time back, I actually played around with both:

  • writing an Emacs OpenHAB mode
  • trying to connect to LSP server

I did not get very far. But I have learned a lot more about Emacs in the meantime, and also LSP (on Emacs) have come a long way, too, in my understanding. So, maybe something I might circle back to at some point.

Although TBH java-mode is “good enough” for how seldom I seem to edit my OpenHAB config files.

Oh I’ve heard good thing about VS Code plugin, but I haven’t tried it yet. However, most professional shops for Java either use IntelliJ or Eclipse.

Most IDEs now away are quite similar, feature wise. And all of them have vim plugins. We are so much more productive now compared to 20 years ago. Relying on a pure editor is just wrong nowadays. I was doing just that with the JSR223 Python code recently with vim. Moving over to PyCharms makes the whole development so much better, and less error prone.

Just to be clear the first post and this thread are meant as humorous jokes.

1 Like