Need help in setting up sonoff switch in Home Assistant

Finally I flashed my sonoff basic with Tasmota. But I can’t find any perfect guide for setting up in Home Assistant :sob:

Can you guys help me out!!

try this:

Not sure what I’m missing. I can see the switch in Lovelace UI. (I added through entities). Tried turning on/off but it’s not working

I have a sonoff mini and have integrated into HA using ESPHOME. (addon for Hassio).

Once you have Tasmota on the Sonoff, use ESPHOME to compile new firmware and upload OTA.

Works like a charm. No need for MQTT, HA sees it as an integration.

My code

  name: sonoff_mini
  platform: ESP8266
#  board: esp01_1m
  board: esp8285
    priority: 600
    - switch.turn_off: 
        id: relay
  ssid: !secret ssid_iot
  password: !secret password_iot
  # Enable fallback hotspot (captive portal) in case wifi connection fails
    ssid: "Sonoff Mini Fallback Hotspot"
    password: "OWNYWRWjLNZJ"


# Enable logging

# Enable Home Assistant API
  password: !secret esp_password
  password: !secret esp_password
  port: 80
#  tasmota template: {"NAME":"Sonoff Mini","GPIO":[17,0,0,0,9,0,0,0,21,56,0,0,255],"FLAG":0,"BASE":1}
#  template decoded: GPIO: GPIO00 17 Button1, GPIO04 9 Switch1, GPIO12 21 Relay1, GPIO13 56 Led1i, GPIO16 255 userdef; BASE: Sonoff Basic

    number: GPIO13
    inverted: true

  - platform: gpio
      number: GPIO4
      mode: INPUT_PULLUP
      inverted: True
    name: "Stair Lights Switch"
      - switch.turn_on: relay
      - switch.turn_off: relay    
  - platform: gpio
    name: "Stair Lights Relay"
    pin: GPIO12
    id: relay
#    restore_mode: ALWAYS_OFF
I am running HA on a Pi4 in Docker.

1 Like

I’m keep getting this error in Sonoff Console.

04:53:15 MQT: Attempting connection...
04:53:15 MQT: Connect failed to, rc 5. Retry in 10 sec

an you show your config and broker settings? Are these just switches?

Here is what’s inside configuration

  "logins": [],
  "anonymous": false,
  "customize": {
    "active": false,
    "folder": "mosquitto"
  "certfile": "fullchain.pem",
  "keyfile": "privkey.pem",
  "require_certificate": false

Here is sonoff mqtt configuration

Here is configuration.yaml

  - platform: mqtt
    name: "Sonoff"
    state_topic: "stat/sonoff/POWER"
    command_topic: "cmnd/sonoff/POWER"
    availability_topic: "tele/sonoff/LWT"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    payload_available: "Online"
    payload_not_available: "Offline"
    retain: false

OK you CANNOT use a user called Homeassistant
Create a new user in HA for MQTT and configure devices for that. Your broker config will work.
Change the client id back to default as well.
I’d also suggest removing all config from config and using discovery (SetOption19 On)

Not sure about what I’m missing.

Changed like this buddy.

broker: core-mosquitto
username: mqtt
password: 123456
  - platform: mqtt
    name: "Sonoff"
    state_topic: "stat/sonoff/POWER"
    command_topic: "cmnd/sonoff/POWER"
    availability_topic: "tele/sonoff/LWT"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    payload_available: "Online"
    payload_not_available: "Offline"
    retain: false

hassio? if so delete everything mqtt: section and all from config also delete the switch and use discovery: SetOption19 On in Tasmota console.

SetOption19 On in Tasmota console returns this…

00:00:00 Project sonoff Sonoff (Topic sonoff, Fallback DVES_B0E649, GroupTopic sonoffs) Version 6.4.0(sonoff)-2_4_2
00:00:00 WIF: Connecting to AP1 Xiaomi Router 3C in mode 11N as sonoff-1609...
00:00:05 WIF: Connected
00:00:05 DNS: Initialized
00:00:05 HTP: Web server active on sonoff-1609.local with IP address
05:35:36 MQT: Attempting connection...
05:35:51 MQT: Connect failed to, rc -4. Retry in 10 sec
05:35:57 CMD: SetOption19 On
05:35:57 RSL: homeassistant/light/B0E649_LI_1/config = 
05:35:57 RSL: homeassistant/switch/B0E649_RL_1/config = {"name":"Sonoff","command_topic":"sonoff/cmnd/POWER","state_topic":"sonoff/tele/STATE","value_template":"{{value_json.POWER}}","payload_off":"OFF","payload_on":"ON","availability_topic":"sonoff/tele/LWT","payload_available":"Online","payload_not_available":"Offline"}
05:35:57 RSL: homeassistant/light/B0E649_LI_2/config = 
05:35:57 RSL: homeassistant/switch/B0E649_RL_2/config = 
05:35:57 RSL: homeassistant/light/B0E649_LI_3/config = 
05:35:57 RSL: homeassistant/switch/B0E649_RL_3/config = 
05:35:57 RSL: homeassistant/light/B0E649_LI_4/config = 
05:35:57 RSL: homeassistant/switch/B0E649_RL_4/config = 
05:35:57 RSL: homeassistant/light/B0E649_LI_5/config = 
05:35:57 RSL: homeassistant/switch/B0E649_RL_5/config = 
05:35:57 RSL: homeassistant/light/B0E649_LI_6/config = 
05:35:57 RSL: homeassistant/switch/B0E649_RL_6/config = 
05:35:57 RSL: homeassistant/light/B0E649_LI_7/config = 
05:35:57 RSL: homeassistant/switch/B0E649_RL_7/config = 
05:35:57 RSL: homeassistant/light/B0E649_LI_8/config = 
05:35:57 RSL: homeassistant/switch/B0E649_RL_8/config = 
05:35:57 RSL: sonoff/stat/RESULT = {"SetOption19":"ON"}
05:36:01 MQT: Attempting connection...
05:36:16 MQT: Connect failed to, rc -4. Retry in 10 sec
05:36:27 MQT: Attempting connection...
05:36:42 MQT: Connect failed to, rc -4. Retry in 10 sec

FYI, I removed the Entity from Lovelace UI and Configuration.yml is like this below now.

# Configure a default setup of Home Assistant (frontend, api, etc)

# Uncomment this if you are using SSL/TLS, running in Docker container, etc.
# http:
#   base_url:

# Text to speech
  - platform: google_translate

group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

  discovery: true
  discovery_prefix: homeassistant

So that’s pretty old firmware. Which binary did you flash? Some of them don’t include discovery.
You don’t need ANY mqtt: section at all if you use hassio and the broker is on the same machine. You use the integration

I flashed Tasmota 6.4.0. And yes. Hassio and Broker running on same machina. i.e RaspberryPi Docker

yes but which binary specifically - they have minimal, etc like about 10 varieties… some don’t support discovery.
Can you update to a later version?

I flashed the latest tasmota.

Here is what I’m getting in the console

05:51:05 MQT: Attempting connection...
05:51:20 MQT: Connect failed to, rc -4. Retry in 10 sec
05:51:26 CMD: SetOption19 On
05:51:26 RSL: sonoff/stat/RESULT = {"SetOption19":"ON"}
05:51:27 RSL: homeassistant/light/B0E649_LI_1/config = 
05:51:27 RSL: homeassistant/switch/B0E649_RL_1/config = {"name":"Sonoff","cmd_t":"~cmnd/POWER","stat_t":"~tele/STATE","val_tpl":"{{value_json.POWER}}","pl_off":"OFF","pl_on":"ON","avty_t":"~tele/LWT","pl_avail":"Online","pl_not_avail":"Offline","uniq_id":"B0E649_RL_1","device":{"identifiers":["B0E649"],"connections":[["mac","84:F3:EB:B0:E6:49"]]},"~":"sonoff/"}
05:51:27 RSL: homeassistant/light/B0E649_LI_2/config = 
05:51:27 RSL: homeassistant/switch/B0E649_RL_2/config = 
05:51:27 RSL: homeassistant/light/B0E649_LI_3/config = 
05:51:27 RSL: homeassistant/switch/B0E649_RL_3/config = 
05:51:27 RSL: homeassistant/light/B0E649_LI_4/config = 
05:51:27 RSL: homeassistant/switch/B0E649_RL_4/config = 
05:51:27 RSL: homeassistant/light/B0E649_LI_5/config = 
05:51:27 RSL: homeassistant/switch/B0E649_RL_5/config = 
05:51:27 RSL: homeassistant/light/B0E649_LI_6/config = 
05:51:27 RSL: homeassistant/switch/B0E649_RL_6/config = 
05:51:27 RSL: homeassistant/light/B0E649_LI_7/config = 
05:51:27 RSL: homeassistant/switch/B0E649_RL_7/config = 
05:51:27 RSL: homeassistant/light/B0E649_LI_8/config = 
05:51:27 RSL: homeassistant/switch/B0E649_RL_8/config = 
05:51:27 RSL: homeassistant/binary_sensor/B0E649_BTN_1/config = {"name":"Sonoff Button1","stat_t":"~stat/BUTTON1","avty_t":"~tele/LWT","pl_avail":"Online","pl_not_avail":"Offline","uniq_id":"B0E649_BTN_1","device":{"identifiers":["B0E649"],"connections":[["mac","84:F3:EB:B0:E6:49"]]},"~":"sonoff/","value_template":"{{value_json.STATE}}","pl_on":"TOGGLE","off_delay":1}
05:51:27 RSL: homeassistant/binary_sensor/B0E649_BTN_2/config = 
05:51:27 RSL: homeassistant/binary_sensor/B0E649_BTN_3/config = 
05:51:27 RSL: homeassistant/binary_sensor/B0E649_BTN_4/config = 
05:51:27 RSL: homeassistant/binary_sensor/B0E649_SW_1/config = {"name":"Sonoff Switch1","stat_t":"~stat/SWITCH1","avty_t":"~tele/LWT","pl_avail":"Online","pl_not_avail":"Offline","uniq_id":"B0E649_SW_1","device":{"identifiers":["B0E649"],"connections":[["mac","84:F3:EB:B0:E6:49"]]},"~":"sonoff/","value_template":"{%if is_state(entity_id,\"on\")-%}OFF{%-else-%}ON{%-endif%}"}
05:51:27 RSL: homeassistant/binary_sensor/B0E649_SW_2/config = 
05:51:27 RSL: homeassistant/binary_sensor/B0E649_SW_3/config = 
05:51:27 RSL: homeassistant/binary_sensor/B0E649_SW_4/config = 
05:51:27 RSL: homeassistant/binary_sensor/B0E649_SW_5/config = 
05:51:27 RSL: homeassistant/binary_sensor/B0E649_SW_6/config = 
05:51:27 RSL: homeassistant/binary_sensor/B0E649_SW_7/config = 
05:51:27 RSL: homeassistant/binary_sensor/B0E649_SW_8/config = 
05:51:27 RSL: homeassistant/sensor/B0E649_status/config = {"name":"Sonoff status","stat_t":"~HASS_STATE","avty_t":"~LWT","frc_upd":true,"pl_avail":"Online","pl_not_avail":"Offline","json_attributes_topic":"~HASS_STATE","unit_of_meas":" ","val_tpl":"{{value_json['RSSI']}}","ic":"mdi:information-outline","uniq_id":"B0E649_status","device":{"identifiers":["B0E649"],"connections":[["mac","84:F3:EB:B0:E6:49"]],"name":"Sonoff","model":"Sonoff Basic","sw_version":"8.1.0(tasmota)","manufacturer":"Tasmota"},"~":"sonoff/tele/"}

So configure>integrations… do you see a MQTT integration?
Do you see any switch entities?
Do you see anything in the log in Home Assistant for the Broker? The console log seems to indicate it’s not connecting to the broker
You created a Home Assistant User called mqtt right?
Mine is called MQTT-User

Yes. I see MQTT Mosquito Integration inside Configure -> Integrations

Nope. I don’t see any switch entities

This is inside log

Log Details (WARNING)
Fri Jan 31 2020 10:03:20 GMT+0530 (India Standard Time)

Config entry for mqtt not ready yet. Retrying in 5 seconds.

Yes. I created User named mqtt

I mean the log for the broker… Go to hassio side panel and click on MQTT - look in the log at the bottom. I assume you started the broker?

There should be no ‘config entry for mqtt’! Did you restart HA after you removed it from configuration.yaml?

Here is the log of MQTT Broker.

10:45:55] INFO: Setup mosquitto configuration
[10:45:55] WARNING: SSL not enabled - No valid certs found!
[10:45:55] INFO: No local user available
[10:45:55] INFO: Initialize system configuration.
[10:45:55] INFO: Initialize Add-on services
[10:45:55] INFO: Initialize Home Assistant discovery
[10:45:55] INFO: Start Mosquitto daemon
1580447755: mosquitto version 1.6.3 starting
1580447755: Config loaded from /etc/mosquitto.conf.
1580447755: Loading plugin: /usr/share/mosquitto/
1580447755: |-- *** auth-plug: startup
1580447755:  ├── Username/password checking enabled.
1580447755:  ├── TLS-PSK checking enabled.
1580447755:  └── Extended authentication not enabled.
1580447755: Opening ipv4 listen socket on port 1883.
1580447755: Opening ipv6 listen socket on port 1883.
1580447755: Opening websockets listen socket on port 1884.
1580447755: Warning: Mosquitto should not be run as root/administrator.
1580447805: New connection from on port 1883.
[INFO] found mqtt on Home Assistant
1580447807: New client connected from as DVES_B0E649 (p2, c1, k30, u'mqtt').

I configured it buddy.

Here is what I did.

Uninstalled MQTT Broker. Restarted Host Machine. Then Installed MQTT with discovery enabled.

Then went to configuration -> Integration.
Found MQTT Device. Added to Lovelace.

Thank you so much for your time and help.

Is it possible to enable my Google Home with Tasmota?