Configuring MQTT on Hass.io

The obvious answer would be HA isn’t receiving the state message back again but don’t know why.

Is your console example just from turning it on and off from HA as mine doesn’t show any CMD messages, this is one turn on and then off from the HA front end on mine…

10:22:54 MQT: stat/sonoff2/RESULT = {"POWER":"ON"}
10:22:54 MQT: stat/sonoff2/POWER = ON (retained)
10:22:58 MQT: stat/sonoff2/RESULT = {"POWER":"OFF"}
10:22:58 MQT: stat/sonoff2/POWER = OFF (retained)

Yeah I can’t see why it’s not getting the state back.
HA’s log isn’t showing anything related to MQTT responses…

I can’t really turn it off from HA because the switch keeps jumping back to the off position. The cmnd entries you see are my manual entries in the console to turn it off

(Edited, can’t see why, not “can”)

Found another thread describing my issue exactly: Sonoff with embedded mqtt

My switch config in case it helps. My sonoff switches are working now (my trouble now is in automations…).
switch:

  • platform: mqtt
    name: “Sonoff switch1”
    command_topic: “cmnd/sonoff1/power”
    state_topic: “stat/sonoff1/POWER”
    qos: 1
    payload_on: “ON”
    payload_off: “OFF”
    retain: true

Also the first MQTT broker config I tried was wrong (at face value).
mqtt:
broker: core-mosquitto
username: yourusername
password: yourpassword

Worked when I set Broker: to my Pi ip and ignored passwords (for now).
mqtt:
broker: 192.168.0.50
client_id: home-assistant-pi

When I configure Hassio like this, the log of the mosquito broker says

starting version 3.2.2
1512735275: mosquitto version 1.4.12 (build date 2017-06-01 13:03:48+0000) starting
1512735275: Config loaded from /etc/mosquitto.conf.
1512735275: Opening ipv4 listen socket on port 1883.
1512735275: Opening ipv6 listen socket on port 1883.
1512735275: Warning: Mosquitto should not be run as root/administrator.
1512735461: New connection from 172.30.32.1 on port 1883.
1512735461: Socket error on client , disconnecting.
1512735830: New connection from 172.30.32.1 on port 1883.
1512735830: Socket error on client , disconnecting.

What do I do wrong?

When I had that “socket error on client” this week (while learning stuff) it was when I had a user/password mismatch between sonoff and MQTT broker…

Interesting, core-mosquitto is the recommended, and working for most people, broker using the hass.io add on rather than the built in mqtt component.

Be sure you’re using the username/password you set up in the broker not an overall HA one.

As @mr.sneezy did, try removing them from the equation altogether to be sure everything then works and it’s not misdirecting you away from a different fault.

Without a username and pass it works good.
when i put a username and password, in the config.yaml is changes the used password for a long string automatically.
Maybe that’s the problem.
BTW, I’m new to Hassio and raspberry pi and programming.
Love Hassio though.
Even my wife gets exited about home automation

Edit: got it working now.
thnx for the help

1 Like

Oké: I flashed a sonoff, started MQTT and in my MQTT log it says : New client connected from 192.168.192.84 as DVES_0780D2.

How to make this client work like a switch?
Got stuck here.

Thnx in advance

How did you fixed it?

How did you get it working? Having same issue with disconnect.

Sorry I’m another stuck.
In the Broker window I get this:
[INFO] found davidtja on local database
1543575809: New client connected from 192.168.0.56 as DVES_3A4FB9 (c1, k10, u’davidtja’).
So assume my MQTT side is set up correctly.
I have added to my config:

switch:  
  - platform: mqtt
    name: "Sonoff"
    command_topic: "cmnd/sonoff/power"
    state_topic: "stat/sonoff/POWER"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    retain: true

However I see no devices on my home page or any matches in states.

Can someone please help?

First thing to check would be to open the webpage of the sonoff and goto configuration/configure MQTT check the topic is the same as your command topic and state topic in your config.yaml entry

To see the switch on your homepage you need to add it to a group via it’s entity ID which will be something like switch.sonoff etc.

Really appreciate your response.
I don’t have it in my devices/entities.
In my configuration validation it says:
“Component not found: Switch”
My full topic is “%prefix%/%topic%/” (that’s as it was by default).
My topics in the config file are:
state_topic: “stat/sonoff/POWER”
command_topic: “cmnd/sonoff/POWER”",
Whereas on the actual Sonoff webpage its “Sonoff” - Perhaps that my mistake perhaps?

Sorted - My entry in config had a Capitalized “Switch” rather than “switch”

Thanks for providing the code basics. I have for days tried to get Tasmota/sonfoff working to no avail. I just pasted the switch config and there it is! A 2017 response I find helpful in 2019. LOL!

1 Like