What?
A friend of mine wants to start with this whole automation lark. He had a simple question: “How the beep do I make a switch turn on and off?”
It is such a simple question and, while openHAB is such a powerful thing, I feel there is no easy answer that a noob can follow. I remember I could find no such thing.
A comment was made a while back in a post where there was someone struggling to understand how items relate to HABPanel; the response was “Have you read the documentation? All of the documentation?”
While well intentioned, I am sure it was detrimental to that user continuing.
- I will attempt to explain a starter setup for an individual who is not to unfamiliar with Raspberry Pi, Linux basics and basic code.
- It will not be the most efficient way of doing it and it won’t be the most recent of everything (looking at you mqtt binding 2.4!)
Hopefully it will give a starting point an a juicy bit help someone understand.
Assumptions
- You have a working RPi (RPi3B+ onwards is good)
- Said Pi is up to date on everything (apt-get update pkg-get upgrade
- You can follow tutorials linked off from here.
- We will use PaperUI (Used for installing bindings and basic stuff)
- We will use HABPanel (Used for displaying and interacting with items)
- We will write minimum code
- You’ve shared your openhab config using samba
How it works
- A smart plug publishes (talks to) your mqtt service running on your PI
- openHAB (running on your PI) subscribes to messages on mqtt
- You have an item (which is a switch). The switch, uses the mqtt binding to listen to the messages published and acts on them (ON/OFF)
- You display that switch on HABPanel as a clicking do-da
- You lose the next 6 months to adding more stuff to the system and your wife leaves you
Items and actions needed
Install MQTT
- SSH into your pi and run the following command
sudo apt-get install mosquitto
You now have mosquitto installed
Download MQTT Spy to test (You can do this on your Windows machine)
-
Create a new connection within mqtt spy and use your Pi’s name. It should connect.
-
Subscribe to a new topic. Let’s call it “home/myswitch”
-
Configure your smart switch to publish to the same topic. This will vary from device to device so there is no point in me trying to explain it. I can show what I see and hopefully you can work it out.
Key is though, you can use mqttSpy to “debug” where your messages are landing.
Here I can see that it’s publishing to a topic called home/lights/kitchen/LED_KITCHEN_LANTERN
(The identifier is a keyword in Espurna. You don’t have to use it.)
In mqttSpy you’re want to subscribe to this and include a /# at the end. This is a wild card in mqtt-land
You should not see this:
And, as in my case, you now should see all the messages chatting back and forth.
If you see this you’ve successfully got your smart switch talking to mqtt.
Next
Install the mqtt 1.x binding
-
Open a browser and go to your openhab installation (http://openhab) might work for you…
-
Click on Addons -> Bindings and type in mqtt.
-
Be sure to install the 1.x binding. The 2.4 binding is a) buggy (For me it is ) and far more intense to understand than the 1.x
-
You should now see a file in your Services folder called mqtt.cfg
Edit it to have the URL of the mqtt server (which should just be localhost)
All should now be well.
Setup yout item
- In your Items folder, create a new file called test.items.
- Open it in your favourite editor (Notepad++ or VSCode)
- Paste the following
Switch MySwitch "This is my first switch and it will work!" {
mqtt="
>[mqtt:home/lights/hallway/lamp/SON_HALL_LAMP_2/relay/0/set:command:ON:1],
>[mqtt:home/lights/hallway/lamp/SON_HALL_LAMP_2/relay/0/set:command:OFF:0],
<[mqtt:home/lights/hallway/lamp/SON_HALL_LAMP_2/relay/0:state:default]
" }
Make sure you change your paths to match what you are seeing in mqttSpy.
Next steps
- Finally! Head of to HABPanel and create a new dashboard.
- On your blank dashboard add a new widget, a Switch widget would do nicely.
- Edit the switch widget and, where it says “openHAB item name” find or type your item MySwitch as above.
- Click save and Run.
Now, all being well, you’re done. Clicking the switch sends a message to openHAB which sends a message to the binding which sends a message to mosquitto which gets noticed by your smar switch which turns on.
caveat emptor
I’ve done this all from my head. There might be some steps not as clear as they could. Let me know and I can edit the post.
The mqtt binding is marked as obsolete and will be replaced at some point int he future. While the new binding will be better, it has a whole bunch you need to wrap your head around which I think it a bit over the top for a noob.
Getting to understand how this all hangs together is a very steep learning curve. The community is very helpful if a) some effort is shown on your part to solve the problem b) you provide enough information to let them help you.
Hopefully this will help someone (like my friend)
Feedback always welcome