How to use Sonoff Tasmota in Home Assistant?

I’ve just loaded the Tasmota firmware on one of my sonoff basic units and the web interface is all good but following this tutorial I’m not able to get the switch to work at all.

mqtt:
 broker: domus1
  port: 1883
  client_id: home-assistant-1
  keepalive: 60
  username: admin
  password: admin
  protocol: 3.1
  birth_message:
    topic: "tele/hass1/LWT"
    payload: "Online"
    qos: 1
    retain: true
  will_message:
    topic: "tele/hass1/LWT"
    payload: "Offline"
    qos: 1
    retain: true

switch:

  - platform: mqtt
    name: "Phone Charger"
    state_topic: "stat/sonoff/POWER"
    command_topic: "cmnd/sonoff/power"
    qos: 0
    payload_on: "on"
    payload_off: "off"
    optimistic: false
    retain: true

If I start home Assistant with that I get the following errors and I’m not able to use any of my switches:

If I just leave mqtt blank and go straight onto switch, all my other switches work (Other switches all using different custom firmware)

mqtt:

Switch:

So I have no idea what I’m doing wrong, everything is default in the user_config.h file besides the wifi username and password I specified.

2 Likes

Is your mqtt broker named ‘domus1’, like in the example?
If you are using the HA Broker, you have to use the IP/name of HA.
BTW, don`t know if Tasmote works with the HA Broker.

Thanks!
Ok well I changed the domus1 in the config to 192.168.0.100 which is the HA IP and funny enough I can turn the switch on in HA now, but it doesn’t translate the state and it doesn’t turn off again, I have to manually turn it off on the switch.

1 Like

In the example the payloads are “ON” and “OFF”.
I think it’s case sensitive.

1 Like

You’re 100% correct, thank you so much, the other firmware was lower case :slight_smile:

Some advanced configuration for the Sonoff-Tasmota firmware. This configuration be used for the Sonoff POW:

sensor:
  - platform: mqtt
    name: "sonoff-workbench energy"
    state_topic: "tele/sonoff-workbench/ENERGY"
    value_template: "{{ value_json.Today }}"
    unit_of_measurement: "kWh"
  - platform: mqtt
    name: "sonoff-workbench power"
    state_topic: "tele/sonoff-workbench/ENERGY"
    value_template: "{{ value_json.Power }}"
    unit_of_measurement: "W"
  - platform: mqtt
    name: "sonoff-workbench voltage"
    state_topic: "tele/sonoff-workbench/ENERGY"
    value_template: "{{ value_json.Voltage }}"
    unit_of_measurement: "V"

binary_sensor:
  - platform: mqtt
    name: "sonoff-workbench lwt"
    state_topic: "tele/sonoff-workbench/LWT"
    value_template: "{{ value }}"
    payload_on: "Online"
    payload_off: "Offline"
    device_class: connectivity

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

This configuration can be used for the Sonoff TH10/TH16 or another Sonoff with sensors (bh1750, bmp280) attached:

sensor:
  - platform: mqtt
    name: "sonoff-th10 temperature"
    state_topic: "tele/sonoff-th10-3/SENSOR"
    value_template: "{{ value_json.BME280.Temperature }}"
    unit_of_measurement: "°C"
  - platform: mqtt
    name: "sonoff-th10 humidity"
    state_topic: "tele/sonoff-th10-3/SENSOR"
    value_template: "{{ value_json.BME280.Humidity }}"
    unit_of_measurement: "%"
  - platform: mqtt
    name: "sonoff-th10 pressure"
    state_topic: "tele/sonoff-th10-3/SENSOR"
    value_template: "{{ value_json.BME280.Pressure }}"
    unit_of_measurement: "hPa"
  - platform: mqtt
    name: "sonoff-th10 illuminance"
    state_topic: "tele/sonoff-th10-3/SENSOR"
    value_template: "{{ value_json.BH1750.Illuminance }}"
    unit_of_measurement: "lx"

binary_sensor:
  - platform: mqtt
    name: "sonoff-th10 lwt"
    state_topic: "tele/sonoff-th10-3/LWT"
    value_template: "{{ value }}"
    payload_on: "Online"
    payload_off: "Offline"
    device_class: connectivity

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

Have fun!

9 Likes

Hello all.

Just starting with Home Assistant. Now testing Mqtt and Sonoff devices (with tasmota firmware).

I tested mqtt works ok with hello world messages, with to putty terminals or putty and home assistant interface.

Now i do this kind settings one my sonoff switch.

When i toggle switch ON my browser. Sonoff switch goes ON just like i want, BUT after couple seconds HA browser shows switch goes back OFF state. Sonoff leave that ON state. If i try manually off sonoff that goes back to ON state after couple seconds. Only way i get that “permanent ON” state off, i need to stop mosquitto service, delete percident file and start mosquitto service again.

I am very thankfull if someone can help me with this problem.

edit: I try this too, but not helped:

  • platform: mqtt
    name: “Sonoff”
    state_topic: “stat/s20-001/POWER”
    command_topic: “cmnd/s20-001/POWER”
    optimistic: false
    payload_on: “ON”
    payload_off: “OFF”
    qos: 1
    retain: true

This occurs because HA did not receive the state_topic: message indicating that the light had turned on. In your example, it should receive a message with topic stat/s20-001/POWER and payload ON (or OFF when you turn the light off).

Not sure, but maybe this helps?

Hi try that link instructions but not helped. Now i try mosquitto -v command. That gives me this, still that sonoff goes ON state ok, but after that not work.

pi@hassbian:~ $ mosquitto -v
1504805954: mosquitto version 1.4.14 (build date Mon, 10 Jul 2017 23:48:43 +0100) starting
1504805954: Using default config.
1504805954: Opening ipv4 listen socket on port 1883.
1504805954: Error: Address already in use

Or if that normal? Little scared how this mqtt start workings and quite much problems to get this working, if compare how easy everything seems many tutorials :slight_smile:

I think you need different Client ID’s for HA and the Sonoff.

Open up a terminal on the system you have mosquitto installed and at the prompt type:

mosquitto_sub -u $USER -P $PASSWORD -t "#" -v

Where $USER = your mosquitto user and $PASSWORD = your mosquitto password.

the output should look similar to this:

tele/sonoff/STATE {"Time":"2017-09-07T20:16:46", "Uptime":38, "Vcc":3.161, "POWER":"OFF", "Wifi":{"AP":1, "SSID":"The Promised Lan", "RSSI":100, "APMac":"B8:A3:86:BD:0C:FB"}}
tele/sonoff4/STATE {"Time":"2017-09-07T20:18:00", "Uptime":50, "Vcc":3.204, "POWER":"OFF", "Wifi":{"AP":1, "SSID":"The Promised Lan", "RSSI":38, "APMac":"B8:A3:86:BD:0C:FB"}}
tele/sonoff4/SENSOR {"Time":"2017-09-07T20:18:01", "AM2301":{"Temperature":23.4, "Humidity":43.5}, "TempUnit":"C"}

Note sonoff4 is a TH10 which also includes an AM2301 temp and humidity sensor.

You have the client id the same for both the sonoff and the config of the broker in HA, i fell for the same thing. Change it in the sonoff to something unique

1 Like

So, just got my Sonoff POW loaded with Tasmota 5.11 and have it online. I am trying to get some of the power numbers out of the unit and am not having much luck…

Here is the output from MQTTfx:

tele/sonoff-basement/SENSOR

Results:

{"Time":"2018-02-04T19:21:11","ENERGY":{"Total":0.000,"Yesterday":0.000,"Today":0.000,"Period":0,"Power":0,"Factor":0.00,"Voltage":118,"Current":0.000}}

My MQTT sensor to read the voltage value:

- platform: mqtt
  name: "sonoff-basement voltage"
  state_topic: "tele/sonoff-basement/SENSOR"
  value_template: "{{ value_json.Voltage }}"
  unit_of_measurement: "V"

This is resulting in the sensor

sensor.sonoffbasement_voltage

With no value contained.

What am I doing wrong?

This setup works for me.

- platform: mqtt
  state_topic: "tele/sonoff/SENSOR"
  name: "Voltage"
  unit_of_measurement: "kWh"
  value_template: "{{ value_json['ENERGY'].Voltage }}"

I hope it works for you.

Thanks… Worked! I knew it was something wrong in reading the json fields.

Is it normal for the POW not to have a wifi led on if the switch is turned off? The unit is still connected but does not display the blue led when it is logically off. I am running Tasmota 5.11.1

No it should show the blue light, maybe you changed it in the code of Tasmota before you wrote the firmware to the device?

I ota’d with sonota.exe and it loaded the latest version which I had pulled down.

This Tastmota/Home Assistant guide sorted it for me:

2 Likes