Greetings, I am new to home assistant and have come off using SmartThings (which has its own world of problems). Anyway, I hope somebody out there can help me out.
Here is the situation, I have a couple of Sonoff devices that have Tasmota running on them. I have configured them with the MQTT settings in accordance with the multiple videos I have watched on the subject. The screenshot below shows the setup (I am using a real IP address don’t worry)
I have installed Mosquitto broker and I have set a username and password. I have put the following parameters in the configuration.yaml file.
mqtt:
broker: core-mosquitto
username: test
password: 1234
so far so good…
I have also added the following parameters to the configuration.yaml file again based on the multiple videos I have watched.
so this should all work. BUT!!! It doesn’t. What is happening is if the Sonoff is off and I turn it on with Home assistant. Then it turns on HAPPY DAYS!!! But after a few seconds home assistant decides that the switch is off and then turns the virtual switch (in the GUI) off again…BUT here’s the thing… the Sonoff doesn’t turn off.
It’s like as if the off command is not being sent. I thought I would try testing MQTT to see if messages are being sent correctly, but I can’t work out how to do that. I have seen a couple of videos which show how you can set a command line to test if messages are being sent and received correctly from the broker, but if I try to use the commands using the SSH shell (Putty) I get an error message “Error: Address not available” when I try to initiate the command “mosquitto_sub -d -u test -P 1234 -t “dev/test””
I was going to try using MQTT lenses but the learning curve is currently to steep for me. Once I have this working I will hopefully be able to get on to building the automation I want to use. Any help would be greatly appreciated.
I’m by no means an expert… just starting out with HASS myself… I am also struggling with a similar problem right now, where my mqtt broker is running, but hass can’t connect.
That being said, your mosquitto command looks right, but I would also specify a -h flag which will tell mosquitto_sub which host to connect to. You can see all available flags by running:
mosquitto_sub --help
Have you done any configuration to your mosquitto.conf file other than providing passwords file?
Everything else on the Sonoff is standard and it just works. Which version of mosquito are you on?
Also, for the topic you want to use a different one for each sonoff, or else they will all listen to the same topic. I make the client name and topic name the same for simplicity.
thanks for the suggestion. but I am not sure what you mean by “I would also specify a -h flag” i dont know where to put this flag, is there a command line or something? if there is how do I use it?
below is the mosquitto.conf. it is unchanged from the original with the exception of a username and password.
{
“plain”: true,
“ssl”: false,
“anonymous”: true,
“logins”: [
{
“username”: “test”,
“password”: “1234”
}
],
“customize”: {
“active”: false,
“folder”: “mosquitto”
},
“certfile”: “fullchain.pem”,
“keyfile”: “privkey.pem”
}
below is the mosquitto log. it shows that the MQTT broker is connecting to the Sonoff, and when I change the client or topic name you can see that the broker gets a Socket error when the Sonoff reboots.
1524620516: New connection from 10.110.20.98 on port 1883.
1524620516: New client connected from 10.110.20.98 as lens_t7ZZUwoAyITay6WmpslQ2NZlMm2 (c1, k120, u’test’).
1524620639: Client eceb69f5-07e7-4b59-988e-ba77fdd7dcbd disconnected.
1524620671: New connection from 172.30.32.1 on port 1883.
1524620671: New client connected from 172.30.32.1 as 99a752b4-fac8-46a0-8f3f-42748a41e143 (c1, k60, u’test’).
1524620735: New connection from 10.110.20.97 on port 1883.
1524620735: New client connected from 10.110.20.97 as sonoff (c1, k15, u’test’).
1524620736: Socket error on client Sonoff_48B653, disconnecting.
1524620855: Socket error on client sonoff, disconnecting.
1524620855: New connection from 10.110.20.97 on port 1883.
1524620855: New client connected from 10.110.20.97 as sonoff (c1, k15, u’test’).
1524621285: Socket error on client sonoff, disconnecting.
1524621285: New connection from 10.110.20.97 on port 1883.
1524621285: New client connected from 10.110.20.97 as sonoff (c1, k15, u’test’).
if I click on the switch in the overview to open the logbook for the device when I turn on the switch MQTT is still seeing the switch state as off as shown in the screenshot below.
Hi, thanks for this. I pasted it into my configuration and changed the name of the Sonoff to match sonoff13. this had not effect on the issue. when I turn on the device in HA the device turns on but HA is not getting the message so it thinks its still of. as a result you cant turn it off again.
this is relay doing my had in and I am about to go and try using Domoticz
hmmm… I am not sure what to say…
with regards to -h flag, I meant: mosquitto_sub -d -u test -P 1234 -t “dev/test” -h 192.168.1.7 or whatever your MQTT broker IP is.
My guess would be that your issue is sonoff and not mqtt… what will happen if you were to deploy another MQTT broker on your non-hass machine and reconfigure your sonoff to connect to it? If you are familiar with docker, you can run this:
If you are still having problems connecting your sonoff to your brand new mqtt deployment, I would blame your sonoff and would try to reflash… Maybe you haven’t changed some setting in tasmota configs?
So this may be nothing but I had a similar issue when I first started with Hass.io, Sonoffs and Tasmota v12. lots of random MQTT behaviour after loads of searching I found that disabling mDNS in the Tasmota build stopped all that and not my Sonoffs work great and are rock solid
thanks Mark. great suggestion, do you mind telling me where I find that setting. my Arduino config is default accross the board because the latest build of Tasmota dosnt require any configuration because it can be done at runtime
it’s in user_config.h I commented out the following before I flashed. Not sure if you can disable it using a console command
// – mDNS ---------------------------------------- #define USE_DISCOVERY // Enable mDNS for the following services (+8k code, +0.3k mem) #define WEBSERVER_ADVERTISE // Provide access to webserver by name .local/ #define MQTT_HOST_DISCOVERY // Find MQTT host server (overrides MQTT_HOST if found)
I fixed it…and it was something relay stupid. So to help others here is what I discovered.
First of all the documentation and multiple videos about how to set up a Sonoff with MQTT lack a pretty critical piece of information. It will only work as described based on the assumption that the Sonoff is only configured with one switch. But that isn’t, in fact, the reality, because even the Sonoff basic can be set up with 2 relays. This is exactly how I have configured the Sonoff.
Therefore when you attempt to send a message to the Sonoff with the assumption that there is only one power switch then the command topic and State topic will look something like this command_topic: “cmnd/sonoff/POWER”, state_topic: “stat/sonoff/POWER”
But because I am new to this, and I don’t know any different I incorrectly believed that the POWER in the phrase referred to the Power relay and if I was using GPIO14 as a second relay then this would in fact use a command similar to “cmnd/sonoff/GPI14”. But this assumption on my part was wrong.
So how I discovered the issues is by using the console on the Sonoff, I noticed that when I initiated a command from HA the Sonoff received the command as follows.
HA didn’t have a placeholder for POWER1 so as far as it new the device never actually turned on. Because HA believed the devices was still off then of cause it stands to reason that you can’t turn it off.
So to solve the issue what I had to do is modify the MQTT switch command to specify the separate power switches.
As a side note, it helps to troubleshoot mqtt outside of HA. Use your PC or phone to send and receive messages and verify working before add HA into the mix.
Prevents hours spent debugging config files that have no problem.