MQTT Light Switches controlling Hue and ESP devices

Hello everyone,

I’m super excited to be joining the OpenHab Community! I’ve been waiting to start creating my OpenHab setup for a few months now. I had to hold off because I wanted to finish another project I’ve been working on. (It was an automatic garden watering system that turns on a water pump based on the value of 3 soil moisture sensors dispersed around my garden).

Problem #1:
My first issue comes from the MQTT binding. After I installed the binding from the Paper UI, no configuration file was created in /etc/openhab2/services. I created one manually and named it “mqtt.cfg” and added

mosquitto.url=tcp://localhost:1883
mosquitto.clientId=openhab

Although the OpenHab log says that it started the MQTT service, it seems that it did not use the configuration file I created since it thinks it is called “broker” when in fact it is not.
18:15:30.658 [INFO ] [penhab.io.transport.mqtt.MqttService] - MQTT Service initialization completed.
18:15:30.661 [INFO ] [t.mqtt.internal.MqttBrokerConnection] - Starting MQTT broker connection ‘’

This seems to also be confirmed in the Mosquitto log file (I think the one new client connected was me, not OpenHab)

1492036018: Saving in-memory database to /var/lib/mosquitto/mosquitto.db.
1492036726: New connection from 127.0.0.1 on port 1883.
1492036726: New client connected from 127.0.0.1 as root.1492036725290 (c1, k60).
1492037819: Saving in-memory database to /var/lib/mosquitto/mosquitto.db.
1492038480: mosquitto version 1.3.4 terminating
1492038480: Saving in-memory database to /var/lib/mosquitto/mosquitto.db.

It seems like I incorrectly installed the binding. Any suggestions on how I should proceed?

Problem #2 (more of a question):

I have started to design a custom wall plate I will use to control items on the network. I am using a piece of clear plexiglass that is cut to fit into the inside of a standard wall plate cover. I then used a ring-shaped sticker I cut with a compass to stick on the back of the plexiglass so that I could spray paint the back white, then peel off the sticker and have a clear ring still exposed on the wall cover. Then on the inner part of the ring that is spray painted I have some copper foil with conductive adhesive that is connected to a capacitive touch breakout which is in turn connected to an ESP8266 (I’m using a NodeMCU board). The reason I wanted there to a ring was because I want there to be an LED that illuminates the button if the item it is controlling is on or off (whether it be a light, outlet, or whatever). Right now I just have a Phillips Hue bulb for testing purposes. The ESP will send/receive status updates with MQTT.

My problem is that I do not understand how I will have the HabPanel AND the wall plate switch to control the on/off status of the bulb. I am also confused as to how I will have the LED turn on/off according to the state of the bulb. I want to be able to turn a Hue on from the wall switch where the LED in the switch also turns on, and turn off the Hue from my phone and have the LED in the switch turn off too. Could someone please clarify???

Looking forward to more fun with OpenHab! Thanks!

Sometimes the mqtt config, while tinkering around, gets stucked. Take a look at
/userdata/config/org/openhab/mqtt.config and check if you have different entries there. If yes just delete that file and restart openHAB, the file should be created again with the correct entries from your /services/mqtt.cfg.

After deleting the other file, it is not recreated but the log file says it is still connecting to “broker”.

Instead of restarting openHAB you should then restart the server.
If that does not help post your mqtt.cfg and mosquitto.conf.

Edit: there is an easy way to test just the mosquitto part:

test mosquitto:

open two putty windows, in the first one:
pi@raspberrypi:~ $ mosquitto_sub -h 127.0.0.1 -t myTopic/#

in the second one:
pi@raspberrypi:~ $ mosquitto_pub -h 127.0.0.1 -t myTopic/Temp -m "test1"

Now you should see “test1” in the first window.

I have tested the Mosquitto server and can confirm that it is working, but I realized that I deleted the wrong mqtt.config file that was located in userdata. I have since removed the right one, but now the OH log doesn’t even mention that it tried to connect to the mqtt server. I have attached the config and log files below.

mqtt.config:

mosquitto.url=tcp://localhost:1883
mosquitto.clientId=openhab

Mosquitto.conf:

# Place your local configuration in /etc/mosquitto/conf.d/
#
# A full description of the configuration file is at
# /usr/share/doc/mosquitto/examples/mosquitto.conf.example

pid_file /var/run/mosquitto.pid

persistence true
persistence_location /var/lib/mosquitto/

log_dest file /var/log/mosquitto/mosquitto.log

include_dir /etc/mosquitto/conf.d

You mixed it up somehow :slight_smile:
In normal operation you don’t have to touch mqtt.config at all, only if you see an error, for example connecting to the wrong broker.
So delete the file /userdata/config/org/openhab/mqtt.config again.
Then go to /conf/services/mqtt.cfg (mqtt.cfg) and make the necessary changes.
Actually only
mosquitto.url=tcp://localhost:1883
is needed to make the binding working.
Then restart your server and see what happens.
A second option to verify the data coming in from your sensors through the mosquitto broker is mqtt.fx or mqtt.spy.

A successful connection and data transfer looks like this in your mosquitto.log:

1492230320: New client connected from 192.168.2.184 as ESPClient4 (c1, k15).
1492230320: Sending CONNACK to ESPClient4 (0, 0)
1492230320: Received SUBSCRIBE from ESPClient4
1492230320: 	/esp8266four/# (QoS 0)
1492230320: ESPClient4 0 /esp8266four/#
1492230320: Sending SUBACK to ESPClient4
1492230320: Sending PUBLISH to ESPClient4 (d0, q0, r1, m0, '/esp8266four/light/intensity', ... (2 bytes))
1492230320: Sending PUBLISH to ESPClient4 (d0, q0, r1, m0, '/esp8266four/temperature/sun', ... (3 bytes))
1492230320: Sending PUBLISH to ESPClient4 (d0, q0, r1, m0, '/esp8266four/temperature/shadow', ... (3 bytes))

If you still see those messages in your log, your mosquitto is not running properly. And your version of mosquitto is very old, current one is 1.4.11.
You can find several tutorials how to install the current version on Raspbian, one is here:

(go to section "Installing the MQTT mosquitto”). → assuming you are on a Raspberry Pi …

I have updated Mosquitto to 1.4.11 and deleted the mqtt.config file, but I am still not having any luck. I still see that the Mosquitto log file says:

1492267423: mosquitto version 1.3.4 terminating

but I believe that to be from the older installation as it does not have the new version on there (1.4.11). I tested the Mosquitto server again using the link that you sent me, and the output matches exactly how it’s listed on the tutorial so I assume that it is working. Any other suggestions?

Nope … but you have to get rid of that old mosquitto version.

Oops! I’m still getting an error though…

1492271868: mosquitto version 1.4.11 (build date Mon, 20 Feb 2017 22:47:27 +0000) starting
1492271868: Config loaded from /etc/mosquitto/mosquitto.conf.
1492271868: Opening ipv4 listen socket on port 1883.
1492271868: Opening ipv6 listen socket on port 1883.
1492271888: Error in poll: Interrupted system call.
1492271888: mosquitto version 1.4.11 terminating
1492271974: mosquitto version 1.4.11 (build date Mon, 20 Feb 2017 22:47:27 +0000) starting
1492271974: Config loaded from /etc/mosquitto/mosquitto.conf.
1492271974: Opening ipv4 listen socket on port 1883.
1492271974: Opening ipv6 listen socket on port 1883.
1492273784: Saving in-memory database to /var/lib/mosquitto/mosquitto.db.
1492275585: Saving in-memory database to /var/lib/mosquitto/mosquitto.db.

IMHO you’re going to find OpenHAB not very helpful for your project. It is a work very much in progress and for OpenHAB nerds. If you are going to be interfacing to something that is itself a work in progress I would suggest you go with Node-Red. It talks quite nicely to MQTT and will also talk to Hue bulbs as well as many other devices. The logical flows make understanding of the functionality easy and you’ll find that the Dashboard ability knocks spots of the basic OpenHAB GUI.

That is not to say I don’t like OpenHAB, mine is just currently broken after what should have been a trivial upgrade.

If you do it right, it works without any problems. I’m using openHAB with MQTT and other bindings since two years and did not have any major problems.

I seem to be having trouble with this. :joy:

The steps I did on my RPi3:

curl -O http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key
sudo apt-key add mosquitto-repo.gpg.key
rm mosquitto-repo.gpg.key
cd /etc/apt/sources.list.d/
sudo curl -O http://repo.mosquitto.org/debian/mosquitto-$(awk -F"[)(]+" '/VERSION=/ {print $2}' /etc/os-release).list
sudo apt-get update
sudo apt-get install mosquitto mosquitto-clients

make sure you have
listener 1883
in your mosquitto.conf

restart Pi

Install MQTT binding, create mqtt.cfg with following config line:

mosquitto.url=tcp://localhost:1883

Example for ESP 8266 Number item, in this case temperature from an DS18B20:

Number Temperature "Temperatur [%.1f °C]" (gInfluxdb) { mqtt="<[mosquitto:/esp8266four/temperature/shadow:state:default]" }

Okay, I decided to do a clean install on my Pi which has definitley solved several issues. The only problem I have remaining is that when I click the switch on the sitemap, the mqtt publish does not occur. I subscribed to all topics to ensure that I didn’t sub to the wrong one with

mosquitto_sub -v -t /#

and I have included my .sitemap and .items files below.

Sitemap:

sitemap home label="Home" {
    Switch item=switch1 label="Test Switch" icon="light"
}

Items:

Switch switch1 "Test Switch" <light> { mqtt="<[openhab:/esp8266four/temperature/shadow:state:default]" }

(P.S. I changed the Mosquitto server id to openhab so that’s why I have this:
{mqtt="<[openhab:/esp8266four/temperature/shadow:state:default]"

Any suggestions? :grinning:

Typo?
You’ve included my mqtt topics! That won’t work with your switch. You need to find out to what topic your switch is publishing to ,

Hahaha, oops! I currently do not have my switches configured yet, so I just want to see of just OH can publish to Mosquitto. I’m assuming it’s done like so:

Switch switch1 "Test Switch" <light> {mqtt=">[openhab:/esp8266four/temperature/shadow:command:ON:1],>
[openhab:/esp8266four/temperature/shadow:command:OFF:0]" }

If this is correct (that’s a big if ;)), then I am still not seeing any messages being published on any topics.

Actually I don’t know what you are doing. :slight_smile:
What do you expect to see in openHAB if no switch is connected to your hardware?

You could do it the other way around: create a virtual switch in openHAB, bind it to a virtual topic, put the switch on your sitemap, switch that virtual switch and you should see the status changing on the topic (if you have subscribed to that topic or did a wildcard subscription).
I tried this with a virtual switch just now and it worked fine:

Switch TestMQTT {mqtt=">[mosquitto:/esp8266four/switch/test:command:ON:1],>[mosquitto:/esp8266four/switch/test:command:OFF:0]" }

But this is not the way to go. First make sure your broker is running (and not terminating), then install the binding, do the standard config (don’t change the standard names as you did above), connect a switch to your hardware, connect the switch to openhab MQTT topic, that’s it.

Sorry, I don’t know what is going on in your system. Maybe openHABian is an option for you.

Ahhh victory!

What I was trying to do was just have a virtual switch to see if I could connect to the MQTT server. I knew that once I had accomplished this I would be able to apply my knowledge towards an actual hardware switch that I will setup later. Speaking of which, how would you recommend that I do this. As I mentioned in my first post I want to be able to control my lights with hardware AND software so that I can walk into a room and easily turn on a light if I don’t have access to the web server. I thought about using variables to store the status of all of my items in the rules file, but I feel like there’s a more effective way to do this…

Persistence is one way to go: Persistence · openhab/openhab1-addons Wiki · GitHub

You mean the virtual switch? Just bind the “virtual” switch item in your items file to a “virtual” topic on your mosquitto broker through the mqtt binding, put it on your sitemap, subscribe to the choosen topic and see if you get a result.
(Still does not make sense to me, it is more work than connecting the actual hardware switch to your openHAB).

Have fun and happy switching :slight_smile: