Home Assistant

pascalsaul edited this page Jul 21, 2018 · 55 revisions

Home
Theo's Tasmota Tips
What's New

Getting Started

Upload tools

Usage

Devices - Additional Info

Home Automation Integration

Other Integrations

Further Topics

ESP32

Clone this wiki locally

(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.

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 = 1
  • TelePeriod = 300

MQTT broker

As Sonoff-Tasmota is MQTT based you will need to configure Home Assistant to connect to an MQTT broker. You can use the following configuration to an MQTT server 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: true
  will_message:
    topic: "tele/hass1/LWT"
    payload: "Offline"
    qos: 1
    retain: true

Switch

As HA is non persistent it is important to configure Sonoff-Tasmota for sending retained power status messages to the broker. This is accomplished with the command PowerRetain On or cmnd/sonoff/PowerRetain On.

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

switch:
  - platform: mqtt
    name: "Sonoff power"
    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

Update the configure of switch NECESSARY ONLY with v.5.9.1

- platform: mqtt
  name: "On/Off Sonoff"
  state_topic: "stat/sonoff/RESULT"
  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.

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/

Power state on start-up without retain option

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/power1"
        payload: ""
    - service: mqtt.publish
      data:
        topic: "cmnd/sonoffs/power2"
        payload: ""
    - service: mqtt.publish
      data:
        topic: "cmnd/sonoffs/Power3"
        payload: "" 
    - service: mqtt.publish
      data:
        topic: "cmnd/sonoffs/power4"
        payload: ""

Sync auto discovered devices including power state

When you use a partially dynamic configured environment without a persistant broker you have the risk that the data is running out out of sync when services are restarted. Use this to get all your (auto discovered) devices in sync including power state again after a reboot of one of service .

- 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: "" 

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"
  - platform: mqtt
    name: "Tele Humidity"
    state_topic: "tele/sonoff/SENSOR"
    value_template: "{{ value_json['DHT22'].Humidity }}"
    unit_of_measurement: "%"

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'

HTU and BMP I2C sensors

HTU21 and BMP280 sensors connected to sonoff2 send messages like:

16:16:43 MQTT: 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"

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 }}"

MagicHome Led Controller

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

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/Color"
  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"

Sonoff B1

Configure the module as "Sonoff B1".

light:
  - platform: mqtt
    name: 'Living room lamp'
    state_topic: "stat/lr_lamp/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: true

Additional example for Sonoff B1

light: 
  - platform: mqtt
    name: "B1"
    command_topic: "cmnd/DEVb1/power"
    state_topic: "stat/DEVb1/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: true

AiLight

Configure the module as "AiLight".

light:
  - platform: mqtt
    name: 'Family room lamp'
    state_topic: "stat/fr_lamp/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: true

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/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/Dimmer"
      brightness_command_topic: "cmnd/led/Dimmer"
      brightness_value_template: "{{ value_json.Dimmer }}"
      brightness_scale: 100      
      rgb_state_topic: "stat/led/Color"
      rgb_command_topic: "cmnd/led/Color2"
      rgb_command_mode: hex
      rgb_command_template: "{{ '%02x%02x%02x' | format(red, green, blue)}}"
      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

Get the IP address of a newly flash 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/#'
    value_template: "{{ value_json.IPAddress1.split(' ')[1].replace('(','').replace(')','') }}"

Put it into a group

  sonoff:
    name: "Sonoff Tasmota"
    control: hidden
    entities:
      - sensor.sonoff_ip
      - script.get_gonoff_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.

I run this on wemos and sonoff flash with tasmota and it has not let me down.

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 }}'

iFan02

From @kbickar in Support for Ifan02 #2839`

fan:
  platform: mqtt  
  name: "Ceiling Fan"  
  state_topic: "stat/ceiling_fan/RESULT"
  speed_state_topic: "stat/ceiling_fan/RESULT"
  state_value_template: "{% if value_json.FanSpeed == 0 -%}0{%- elif value_json.FanSpeed > 0 -%}2{%- endif %}" 
  speed_value_template: "{{ value_json.FanSpeed }}"
  availability_topic: tele/ceiling_fan/LWT
  payload_available: Online
  payload_not_available: Offline
  speed_command_topic: "cmnd/ceiling_fan/FanSpeed"
  payload_low_speed: "1"
  payload_medium_speed: "2"
  payload_high_speed: "3"
  command_topic: "cmnd/ceiling_fan/FanSpeed"
  payload_off: "0"
  payload_on: "2"
  speeds:
    - low
    - medium
    - high