Home Assistant - RTurala/Sonoff-Tasmota GitHub Wiki

(This information is related to Sonoff-Tasmota version 3.9.11 and up)

Home Assistant (HA) is an open-source home automation platform running on Python 3.

Hass configuration - General

Configure HA by editing the file configuration.yaml to be found in folder .homeassistant at first installation.

After every change to the configuration file you'll need to restart HA to make it aware of the changes. On my Debian Linux system I perform the command systemctl restart home-assistant.

In the examples shown the Sonoff-Tasmota parameters are set:

  • MQTT_STATUS_OFF in user_config.h = OFF
  • MQTT_STATUS_ON in user_config.h = ON
  • SUB_PREFIX in user_config.h = cmnd
  • PUB_PREFIX in user_config.h = stat
  • PUB_PREFIX2 in user_config.h = tele
  • Mqtt = 1
  • MqttHost = domus1
  • MqttPort = 1883
  • Topic = sonoff
  • PowerRetain = 0
  • TelePeriod = 300

Hass configuration - MQTT broker

As Sonoff-Tasmota is MQTT based you will need to configure Home Assistant to connect to an MQTT broker.

Home Assistant comes with an embedded MQTT broker which is easy to set up but you may want to opt for a separate MQTT broker instead for better stability. A popular choice for this is the open-source Eclipse Mosquitto.

Configure the embedded broker

# Example configuration.yaml entry
mqtt:
  password: hello

Default username is homeassistant while port defaults to 1883.

Configure an external broker

In the following configuration example an external MQTT server is used with the hostname domus1:

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

The retain option plays an important role in persisting states over restarts and other disruptions. If you experience that states are out of sync you probably want to review your configuration.

An in depth explanation about MQTT retain is available in this video.

Add devices to Home assistant - Automatic discovery

MQTT device discovery To ease Home Assistant (and Domoticz #1731 and newer) configuration a feature called MQTT device discovery is made available for Tasmota switches and lights.

Automatic discovery is currently supported - and recommended - for:

  • Relay (Switch)
  • Light (LED dimmer)
  • Sensors
  • Button

By executing command SetOption19 On this feature is enabled and a retained MQTT message starting with topic "homeassistant..", as defined in user_config.h HOME_ASSISTANT_DISCOVERY_PREFIX, is sent containing parameters used by Home Assistant to automatically configure a device. This means ni user interaction is needed to add the device to Home Assistant.

To disable this feature and get rid of the retained message execute command SetOption19 Off and the homeassistant topic is removed from the MQTT server. This will also remove the device from Home Assistant.

Tip: Sync auto discovered devices including power state

When you use a partially dynamic configured environment without a persistent broker you have the risk that the device state is of sync when Home Assistant is restarted. Use this automation to get all your (auto discovered) devices in sync, including power state, when Home Assistant is started.

- alias: "Power state on HA start-up"
  trigger:
    platform: homeassistant
    event: start
  action:
    - service: mqtt.publish
      data:
        topic: "cmnd/sonoffs/SetOption19"
        payload: "1"
    - service: mqtt.publish
      data:
        topic: "cmnd/sonoffs/state"
        payload: "" 

Add devices to Home assistant - Manual

Instead of relying on automatic discovery, devices can be manually added to Home Assistant. The following section gives example for how to configure some typical device types.

Tip: Refresh state on Home Assistant start-up without retain option

An automation can be used to send status command. One example is an automation at Home Assistant startup which you can also easily trigger again via the GUI manually.

Example

Edit automations.yaml and add:

- alias: "Power state on HA start-up"
  trigger:
    platform: homeassistant
    event: start
  action:
    - service: mqtt.publish
      data:
        topic: "cmnd/sonoffs/state"
        payload: ""

This automation posts to the default "sonoffs" group topic. Each device will send back their status message which contains relay power and light status.

Tip: View the firmware version number of a Tasmota device

Add a sensor like below in your sensor section for each Tasmota device in one card in an order that makes sense for you. Change the state_topic and availability_topic to the unique topic of each device.

sensor:
  - platform: mqtt
    name: "S20_Rock"
    state_topic: "stat/S20Beta/STATUS2"
    value_template: "{{value_json['StatusFWR'].Version }}"
    qos: 0
    availability_topic: "tele/S20Beta/LWT"
    payload_available: "Online"
    payload_not_available: "Offline"

Tip: Get the IP address of a newly flashed Sonoff (ESP8266)

Here is some code that will display the IP address of you newly flashed ESP8266 if you have to change PROJECT name in user_config.h you must also change the script topic has to match it.

The script

 get_sonoff_ip:	
  alias: Get Sonoff New IP (sonoff)	
  sequence:	
  - data:	
      topic: cmnd/sonoff/ipaddress	
    service: mqtt.publish	

topic: cmnd/the PROJECT NAME/ipaddress now the sensor

  - platform: mqtt
    name: "SonOff IP"
    state_topic: 'stat/sonoff/RESULT'
    value_template: "{{ value_json.IPAddress1.split(' ')[1].replace('(','').replace(')','') }}"

Put it into a group

  sonoff:
    name: "Sonoff Tasmota"
    control: hidden
    entities:
      - script.get_sonoff_ip
      - sensor.sonoff_ip

restart HA plug in your newly flashed ESP8266 Click EXECUTE (in the new group) and It will display the IP address in the "Sonoff IP" sensor, don't forget to change the topic name in "Configure MQTT" in the Configuration Menu.

Tip: WiFI RSSI Signal Strength

Display the WiFi signal strength published from each telemetry message (default time is every 300 seconds). Change the two topics below to your Tasmota device name. It may not immediately show on your Home Assistant panel until the next telemetry message is published.

  - platform: mqtt
    state_topic: "tele/SNF-Washer/STATE"
    name: "Washer Signal"
    unit_of_measurement: "%"
    value_template: "{{value_json['Wifi'].RSSI }}"
    availability_topic: "tele/SNF-Washer/LWT"
    payload_available: "Online"
    payload_not_available: "Offline"

Manually add Switch

Add the device as a switch to HA by updating the configuration file.

switch:
  - platform: mqtt
    name: "Sonoff power"
    state_topic: "stat/sonoff/RESULT"
    state_value_template: "{{ value_json.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

If you are using your Sonoff to control a light, you may want to use the light component. Simply replace switch with light in the above configuration. All other settings remain the same.

If you are using a localized version (eg. de-DE) be sure to check the correct spelling and cases for the defines:

  • 'D_ONLINE' for 'payload_available'
  • 'D_OFFLINE' for 'payload_not_available'
  • 'D_ON' for 'payload_on'
  • 'D_OFF' for 'payload_off'

**Since Tasmota v6 Home Assistant Discovery defaults to "switch", to change this to "light" go to the console and type the following command: SetOption30 1 **

Further documentation on the Home Assistant switch and light components can be found here:

https://home-assistant.io/components/switch.mqtt/

https://home-assistant.io/components/light.mqtt/

Manually add DHT22 sensor

Periodical updates

A DHT22 Temperature and Humidity sensor connected to a Sonoff TH10 will send at TelePeriod intervals the following information to the MQTT broker:

tele/sonoff/SENSOR = {"Time":"2017-02-12T16:11:12", "DHT22":{"Temperature":23.9, "Humidity":34.1}}

To make the information visible in HA add the following lines to the configuration file.

sensor:
  - platform: mqtt
    name: "Tele Temperature"
    state_topic: "tele/sonoff/SENSOR"
    value_template: "{{ value_json['DHT22'].Temperature }}"
    unit_of_measurement: "°C"
    availability_topic: "tele/sonoff/LWT"
    payload_available: "Online"
    payload_not_available: "Offline"
  - platform: mqtt
    name: "Tele Humidity"
    state_topic: "tele/sonoff/SENSOR"
    value_template: "{{ value_json['DHT22'].Humidity }}"
    unit_of_measurement: "%"
    availability_topic: "tele/sonoff/LWT"
    payload_available: "Online"
    payload_not_available: "Offline"

This periodic interval can be changed using the TelePeriod command (see the wiki for the MQTT commands).

Manual updates

Another means of sensor information retrieval from Sonoff-Tasmota is using the status command Status 10 or cmnd/sonoff/status 10. This would result in a message like:

stat/sonoff/STATUS10 {"StatusSNS":{"Time":"2017-02-11T18:06:05", "DHT22":{"Temperature":"21.8", "Humidity":"48.0"}}}

The HA configuration would then look like this:

sensor:
  - platform: mqtt
    name: "Stat Temperature"
    state_topic: "stat/sonoff/STATUS10"
    value_template: "{{ value_json.StatusSNS.DHT22.Temperature }}"
    unit_of_measurement: "°C"
  - platform: mqtt
    name: "Stat Humidity"
    state_topic: "stat/sonoff/STATUS10"
    value_template: "{{ value_json.StatusSNS.DHT22.Humidity }}"
    unit_of_measurement: "%"

The Sonoff-Tasmota command could be initiated by a mosquitto mqtt pub command on mosquitto_pub -h localhost -t 'cmnd/sonoff/status' -m '10'

Manually add HTU and BMP I2C sensors

HTU21 and BMP280 sensors connected to sonoff2 send messages like:

tele/sonoff2/SENSOR = {"Time":"2017-02-12T16:16:43", "HTU21":{"Temperature":24.0, "Humidity":34.0}, "BMP280":{"Temperature":24.9, "Pressure":1032.5}}

Where the Pressure information would be made available to HA with

sensor:
  - platform: mqtt
    name: "Tele Pressure"
    state_topic: "tele/sonoff2/SENSOR"
    value_template: "{{ value_json.BMP280.Pressure }}"
    unit_of_measurement: "hPa"

Manually add Sonoff Pow Energy sensors

Periodical updates

A Sonoff Pow device called pow1 will periodically send the following message:

tele/pow1/SENSOR = {"Time":"2018-02-14T21:51:31","ENERGY":{"Total":0.984,"Yesterday":0.000,"Today":0.984,"Period":12,"Power":145,"Factor":0.90,"Voltage":220,"Current":0.731}}

The HA configuration for Energy, Power, Voltage and Current would be:

sensor:
  - platform: mqtt
    name: "Energy"
    state_topic: "tele/pow1/SENSOR"
    value_template: '{{ value_json["ENERGY"]["Today"] }}'
    unit_of_measurement: "kWh"
  - platform: mqtt
    name: "Power"
    state_topic: "tele/pow1/SENSOR"
    value_template: '{{ value_json["ENERGY"]["Power"] }}'
    unit_of_measurement: "W"
  - platform: mqtt
    name: "Voltage"
    state_topic: "tele/pow1/SENSOR"
    value_template: '{{ value_json["ENERGY"]["Voltage"] }}'
    unit_of_measurement: "V"
  - platform: mqtt
    name: "Current"
    state_topic: "tele/pow1/SENSOR"
    value_template: '{{ value_json["ENERGY"]["Current"] }}'
    unit_of_measurement: "A"

Update the configure of sensor NECESSARY ONLY with v.5.9.1

- platform: mqtt
  name: "Energy"
  state_topic: "tele/pow1/ENERGY"
  value_template: '{{ value_json["Today"] }}'
  unit_of_measurement: "kWh"
- platform: mqtt
  name: "Power"
  state_topic: "tele/pow1/ENERGY"
  value_template: '{{ value_json["Power"] }}'
  unit_of_measurement: "W"
- platform: mqtt
  name: "Voltage"
  state_topic: "tele/pow1/ENERGY"
  value_template: '{{ value_json["Voltage"] }}'
  unit_of_measurement: "V"
- platform: mqtt
  name: "Current"
  state_topic: "tele/pow1/ENERGY"
  value_template: '{{ value_json["Current"] }}'
  unit_of_measurement: "A"

Manual updates

The manual message retrieved with command Status 8 or cmnd/pow1/status 8 will show:

stat/pow1/STATUS8 = {"StatusPWR":{"Yesterday":0.002, "Today":0.002, "Power":4, "Factor":0.37, "Voltage":227, "Current":0.056}}

The HA configuration for Power Factor would then be:

sensor:
  - platform: mqtt
    name: "Power Factor"
    state_topic: "stat/pow1/STATUS8"
    value_template: "{{ value_json.StatusPWR.Factor }}"

Manually add Dimmer / PWM LED

Change the brightness template entities to match your entity name.

configuration.yaml

- platform: mqtt
  name: "TuyaDimTest"
  state_topic: "stat/TuyaDimTest/RESULT"
  state_value_template: "{{ value_json.POWER }}"
  command_topic: "cmnd/TuyaDimTest/POWER"
  availability_topic: "tele/TuyaDimTest/LWT"
  brightness_state_topic: "stat/TuyaDimTest/RESULT"
  brightness_command_topic: "cmnd/TuyaDimTest/Dimmer"
  brightness_scale: 100
  brightness_value_template: >
    {% if value_json.Dimmer is defined %}
      {{ value_json.Dimmer }}
    {% else %}
      {% if state_attr('light.tuyadimtest','brightness') == none %}
        0
      {% else %}
        {{ state_attr('light.tuyadimtest','brightness') / 255 * 100 }}
      {% endif %}
    {% endif %}
  qos: 1
  payload_on: "ON"
  payload_off: "OFF"
  payload_available: "Online"
  payload_not_available: "Offline"
  retain: false

Manually add RGB Lights

AiLight

Configure the module as "AiLight".

light:
  - platform: mqtt
    name: 'Family room lamp'
    state_topic: "stat/fr_lamp/RESULT"
    state_value_template: "{{ value_json.POWER }}"
    command_topic: "cmnd/fr_lamp/POWER"
    brightness_state_topic: "stat/fr_lamp/DIMMER"
    brightness_command_topic: "cmnd/fr_lamp/DIMMER"
    brightness_value_template: "{{ value_json.brightness }}"
    brightness_scale: 100
    rgb_state_topic: "stat/fr_lamp/COLOR"
    rgb_command_topic: "cmnd/fr_lamp/COLOR"
    rgb_command_template: "{{ '%02x%02x%02x00' | format(red, green, blue) }}"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    retain: false

Manually add Led WS2812B

Configure any of the pins of the module as "WS2812B" and on the Console run SetOption15 1.

light:
# LED WS2812b
    - platform: mqtt
      name: "TiraLed"
      retain: false
      state_topic: "stat/led/RESULT"
      state_value_template: "{{ value_json.POWER }}"
      command_topic: "cmnd/led/POWER"
      availability_topic: "tele/led/LWT"
      qos: 1
      payload_on: "ON"
      payload_off: "OFF"
      payload_available: "Online"
      payload_not_available: "Offline"
      optimistic: false
      brightness_state_topic: "stat/led/RESULT"
      brightness_command_topic: "cmnd/led/Dimmer"
      brightness_value_template: "{{ value_json.Dimmer }}"
      brightness_scale: 100      
      rgb_state_topic: "stat/led/RESULT"
      rgb_value_template: "{{(value_json.Channel[0]*2.55)|int}},{{(value_json.Channel[1]*2.55)|int}},{{(value_json.Channel[2]*2.55)|int}}"
      rgb_command_topic: "cmnd/led/Color2"
      rgb_command_mode: hex
      rgb_command_template: "{{ '%02x%02x%02x' | format(red, green, blue)}}"
      hs_state_topic: "stat/led/RESULT"
      hs_command_topic: "cmnd/led/HsbColor"
      hs_value_template: "{{ value_json.HSBColor.rsplit(',', 1)[0] }}"
      effect_state_topic: "stat/led/Scheme"
      effect_command_topic: "cmnd/led/Scheme"
      effect_value_template: "{{ value_json.Scheme }}"
      effect_list:
          - 0
          - 1
          - 2
          - 3
          - 4
          - 5
          - 6
          - 7
          - 8
          - 9
          - 10
          - 11
          - 12

Manually add RGBW Lights

MagicHome Led Controller

Configure the module as "34 MagicHome" and from the Console run SetOption15 1.

Arilux LC02

Configure the module as "18 Generic module" and from the Console run SetOption15 1. More info how to configure the GPIO Arilux LC02

Configuration entry

light:
- platform: mqtt
  name: magic
  state_topic: "stat/sonoff/RESULT"
  command_topic: "cmnd/sonoff/POWER"
  state_value_template: "{{ value_json.POWER }}"
  brightness_state_topic: "stat/sonoff/RESULT"
  brightness_command_topic: "cmnd/sonoff/Dimmer"
  brightness_scale: 100
  brightness_value_template: "{{ value_json.Dimmer }}"
  white_value_state_topic: "stat/sonoff/RESULT"
  white_value_command_topic: "cmnd/sonoff/Channel4"
  white_value_scale: 100
  white_value_template: "{{ value_json.Channel[3] }}"
  rgb_command_topic: "cmnd/sonoff/Color2"
  rgb_command_template: "{{ '%02x%02x%02x' | format(red, green, blue)}}"
  rgb_state_topic: "stat/sonoff/RESULT"
  rgb_value_template: "{{(value_json.Channel[0]*2.55)|int}},{{(value_json.Channel[1]*2.55)|int}},{{(value_json.Channel[2]*2.55)|int}}"
  effect_state_topic: "stat/sonoff/RESULT"
  effect_command_topic: "cmnd/sonoff/Scheme"
  effect_list:
    - 0
    - 1
    - 2
    - 3
    - 4
  retain: false
  qos: 1
  payload_on: "ON"
  payload_off: "OFF"

Manually add RGBWW Lights

Sonoff B1

https://community.home-assistant.io/t/sonoff-b1-led-wifi-bulb-template-error-in-log/28690/9

You can set the following using the sonoff web interface - Console or by sending it MQTT commands

Fade on (optional, but makes the transitions slower)
Speed 5 (optional, but makes the transitions slower)
light:
  - platform: mqtt
    name: "Lounge lamp"
    state_topic: "stat/sonoffb1/RESULT"
    state_value_template: "{{ value_json.POWER }}"
    state_value_template: "{{ value }}"
    command_topic: "cmnd/sonoffb1/POWER"
    brightness_state_topic: "stat/sonoffb1/DIMMER"
    brightness_command_topic: "cmnd/sonoffb1/Dimmer"
    brightness_value_template: "{{ value_json.Dimmer }}"
    brightness_scale: 100
    rgb_state_topic: "stat/sonoffb1/COLOR"
    rgb_command_topic: "cmnd/sonoffb1/Color"
    rgb_command_template: "{{ '%02x%02x%02x0000' | format(red, green, blue) }}"
    rgb_value_template: "{{ value_json.Color[0:2]|int(base=16) }},{{ value_json.Color[2:4]|int(base=16) }},{{ value_json.Color[4:6]|int(base=16) }}"
    color_temp_state_topic: "stat/sonoffb1/CT"
    color_temp_command_topic: "cmnd/sonoffb1/CT"
    color_temp_value_template: "{{ value_json.CT }}"
    effect_state_topic: "stat/sonoffb1/SCHEME"
    effect_command_topic: "cmnd/sonoffb1/Scheme"
    effect_value_template: "{{ value_json.Scheme }}"
    effect_list:
      - 0
      - 1
      - 2
      - 3
      - 4
    availability_topic: "tele/sonoffb1/LWT"
    payload_available: "Online"
    payload_not_available: "Offline"
    qos: 0
    payload_on: "ON"
    payload_off: "OFF"
    optimistic: false
    retain: false

Configure the module as "Sonoff B1".

light:
  - platform: mqtt
    name: 'Living room lamp'
    state_topic: "stat/lr_lamp/RESULT"
    state_value_template: "{{ value_json.POWER }}"
    command_topic: "cmnd/lr_lamp/POWER"
    rgb_state_topic: "stat/lr_lamp/COLOR"
    rgb_command_topic: "cmnd/lr_lamp/COLOR"
    rgb_command_template: "{{ '%02x%02x%02x0000' | format(red, green, blue) }}"
    brightness_state_topic: "stat/lr_lamp/DIMMER"
    brightness_value_template: "{{ value_json.brightness }}"
    brightness_command_topic: "cmnd/lr_lamp/DIMMER"
    brightness_scale: 100
    color_temp_command_topic: "cmnd/lr_lamp/CT"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    retain: false

Additional example for Sonoff B1

light: 
  - platform: mqtt
    name: "B1"
    command_topic: "cmnd/DEVb1/power"
    state_topic: "stat/DEVb1/RESULT"
    state_value_template: "{{ value_json.POWER }}"
    rgb_state_topic: "stat/DEVb1/color"
    rgb_command_topic: "cmnd/DEVb1/color"
    brightness_state_topic: "stat/DEVb1/dimmer"
    brightness_command_topic: "cmnd/DEVb1/dimmer"
    color_temp_state_topic: "stat/DEVb1/CT"
    color_temp_command_topic: "cmnd/DEVb1/CT"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    retain: false

RF Codes from Sonoff-Bridge

binary_sensor:
  - platform: mqtt
    name: "Test RF bridge rfkey 1"
    payload_on: "1"
    payload_off: "0"
    device_class: opening
    state_topic: "tele/sonoff_bridge/RESULT"
    value_template: '{{ value_json.RfReceived.RfKey }}'

Manually add iFan02 - Example 1

From @kbickar in Support for Ifan02 #2839`

Modified by @finity69x2 in Support for Ifan02 #2839`

- platform: mqtt  
    name: "Sonoff Fan"
    command_topic: "cmnd/sonoff_fan/FanSpeed"
    speed_command_topic: "cmnd/sonoff_fan/FanSpeed"    
    state_topic: "stat/sonoff_fan/RESULT"
    speed_state_topic: "stat/sonoff_fan/RESULT"
    #state_value_template: "{% if value_json.FanSpeed == 0 -%}0{%- elif value_json.FanSpeed > 0 -%}4{%- endif %}"
    state_value_template: >
      {% if value_json.FanSpeed is defined %}
        {% if value_json.FanSpeed == 0 -%}0{%- elif value_json.FanSpeed > 0 -%}4{%- endif %}
      {% else %}
        {% if states.fan.sonoff_fan.state == 'off' -%}0{%- elif states.fan.sonoff_fan.state == 'on' -%}4{%- endif %}
      {% endif %}
    speed_value_template: "{{ value_json.FanSpeed }}"
    availability_topic: tele/sonoff_fan/LWT
    payload_off: "0"
    payload_on: "4"
    payload_low_speed: "1"
    payload_medium_speed: "2"
    payload_high_speed: "3"
    payload_available: Online
    payload_not_available: Offline
    speeds:
      - off
      - low
      - medium
      - high

Manually add S31

configuration.yaml

switch:
  - platform: mqtt
    name: "s31_02"
    state_topic: "stat/s31_02/RESULT"
    state_value_template: "{{ value_json.POWER }}"
    command_topic: "cmnd/s31_02/POWER"
    availability_topic: "tele/s31_02/LWT"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    payload_available: "Online"
    payload_not_available: "Offline"
    retain: false

sensor:
  - platform: mqtt
    name: "s31_02 Voltage"
    state_topic: "tele/s31_02/SENSOR"
    value_template: "{{ value_json['ENERGY'].Voltage }}"
    unit_of_measurement: "V"
    availability_topic: "tele/s31_02/LWT"
    qos: 1
    payload_available: "Online"
    payload_not_available: "Offline"
  - platform: mqtt
    name: "s31_02 Current"
    state_topic: "tele/s31_02/SENSOR"
    value_template: "{{ value_json['ENERGY'].Current | round(2) }}"
    unit_of_measurement: "A"
    availability_topic: "tele/s31_02/LWT"
    qos: 1
    payload_available: "Online"
    payload_not_available: "Offline"
  - platform: mqtt
    name: "s31_02 Power"
    state_topic: "tele/s31_02/SENSOR"
    value_template: "{{ value_json['ENERGY'].Power }}"
    unit_of_measurement: "W"
    availability_topic: "tele/s31_02/LWT"
    qos: 1
    payload_available: "Online"
    payload_not_available: "Offline"
  - platform: mqtt
    name: "s31_02 Power Factor"
    state_topic: "tele/s31_02/SENSOR"
    value_template: "{{ value_json['ENERGY'].Factor }}"
    availability_topic: "tele/s31_02/LWT"
    qos: 1
    payload_available: "Online"
    payload_not_available: "Offline"
  - platform: mqtt
    name: "s31_02 Energy Today"
    state_topic: "tele/s31_02/SENSOR"
    value_template: "{{ value_json['ENERGY'].Today }}"
    unit_of_measurement: "kWh"
    availability_topic: "tele/s31_02/LWT"
    qos: 1
    payload_available: "Online"
    payload_not_available: "Offline"
  - platform: mqtt
    name: "s31_02 Energy Yesterday"
    state_topic: "tele/s31_02/SENSOR"
    value_template: "{{ value_json['ENERGY'].Yesterday }}"
    unit_of_measurement: "kWh"
    availability_topic: "tele/s31_02/LWT"
    qos: 1
    payload_available: "Online"
    payload_not_available: "Offline"
  - platform: mqtt
    name: "s31_02 Energy Total"
    state_topic: "tele/s31_02/SENSOR"
    value_template: "{{ value_json['ENERGY'].Total }}"
    unit_of_measurement: "kWh"
    availability_topic: "tele/s31_02/LWT"
    qos: 1
    payload_available: "Online"
    payload_not_available: "Offline"

groups.yaml

Sonoff S31_02:
  - switch.s31_02
  - sensor.s31_02_voltage
  - sensor.s31_02_current
  - sensor.s31_02_power
  - sensor.s31_02_power_factor
  - sensor.s31_02_energy_today
  - sensor.s31_02_energy_yesterday
  - sensor.s31_02_energy_total

Manually add iFan02 - Example 2

Combination of configs found in the support thread: Support for Ifan02 #2839 and Home Assistant forum: Sonoff IFan02 (Tasmota) MQTT Fan

configuration.yaml

fan:
  - platform: mqtt  
    name: "Pat Ceiling Fan"  
    state_topic: "stat/ifan02_1/RESULT"
    speed_state_topic: "stat/ifan02_1/RESULT"
    state_value_template: >
        {% if value_json.FanSpeed is defined %}
          {% if value_json.FanSpeed == 0 -%}0{%- elif value_json.FanSpeed > 0 -%}2{%- endif %}
        {% else %}
          {% if states.fan.pat_ceiling_fan.state == 'off' -%}0{%- elif states.fan.pat_ceiling_fan.state == 'on' -%}2{%- endif %}
        {% endif %}
    speed_value_template: "{{ value_json.FanSpeed }}"
    availability_topic: tele/ifan02_1/LWT
    payload_available: Online
    payload_not_available: Offline
    speed_command_topic: "cmnd/ifan02_1/FanSpeed"
    payload_low_speed: "1"
    payload_medium_speed: "2"
    payload_high_speed: "3"
    command_topic: "cmnd/ifan02_1/FanSpeed"
    payload_off: "0"
    payload_on: "2"
    qos: 1
    retain: false
    speeds:
      - low
      - medium
      - high
light:
  - platform: mqtt
    name: "Pat Ceiling Light"
    command_topic: "cmnd/ifan02_1/power1"
    state_topic: "stat/ifan02_1/POWER1"
    availability_topic: tele/ifan02_1/LWT
    payload_available: Online
    payload_not_available: Offline
    qos: 1
    payload_off: "OFF"
    payload_on: "ON"
    retain: false

groups.yaml

Pat Ceiling Fan:
  - fan.pat_ceiling_fan
  - light.pat_ceiling_light