Add an MQTT switch to the configuration Home Assistant

4 minutes of reading
AIMS OF THE GUIDE
  • Add an MQTT switch to the configuration of Home Assistant
  • Difficulty level: bass
CONCEPTS FACED:
  • Software installation and configuration
SOFTWARE COMPONENTS USED:
Prerequisites
PHYSICAL DEVICES USED:
  • The device on which it resides Home Assistant
  • An intelligent switch that offers an MQTT interface
GUIDE more indicated for:

All environments

Notes and disclaimer
  • qualsiasi modifica all'impianto elettrico dev'essere progettata ed effettuata da personale qualificato;
  • any changes implemented in probefore is a propersonal responsibility as well as a profirst risk and danger (the contents of the present page are purely educational);
  • any changes implemented in proprior to a device it voids the guarantee, quality approvals and certifications.
Driving review: 1.4

Abstract

Home Assistant provides native support from the earliest versions al proMQTT tocollo through a called component, for theappanointed, "MQTT". If you have an intelligent switch equipped with MQTT support (eg. a Sonoff riproprogrammed Sonoff-Tasmota), it is possible to integrate the control in a very simple way on Home Assistant.

On this page we will see how to do this using the component "Switch"Of Home Assistant, specifically the platform (sub-component) “MQTT Switch".

In order for the configuration to do that appwe remain to carry out functions, it is necessary that the MQTT component di Home Assistant is already configured. We recommend reading this of this guide.


We strongly recommend that you read the following guide before reviewing this guide:

How to add switches and sensors Sonoff a Home Assistant

Installation

No need to install anything: just add to the configuration of Home Assistant the entry for the switch you want to add.

Nb To use this platform it is necessary that the "MQTT" component of Home Assistant has been previously configured (see first part of the card dedicated to the "MQTT" component). We also recommend to read carefully the guide dedicated to the theme of configuration of the MQTT components in the profirst home automation.

Configuration

So let's see how to do it.
The minimal set configuration to add to the file "configuration.yaml"Of Home Assistant is the following:

switch:
  - platform: mqtt
    name: "Ventilatore",
    command_topic: "cmnd/Ventilatore/POWER"

Once saved the configuration and relaunched / updated Home Assistant, appa new switch will appear (as in the example configuration):

Home Assistant Switch

Configuration variables:

name

(string)(Optional) The name to attribute to the switch
Default value: MQTT Switch

icon

(string)(Optional) Switch icon (eg. mdi: radiator).

state_topic
(string)(Required) The MQTT topic to be signed by Home Assistant to receive the switch status (on / off). Obviously the state topic changes based to the MQTT configuration of the device.
command_topic(string)(Obligatory) The MQTT topic to be published by Home Assistant to change the status of the switch. Obviously the command topic changes based to the MQTT configuration of the device.
availability_topic(string)(Optional) The MQTT topic to be signed by Home Assistant to receive the status (online / offline) of the switch. Obviously the availability topic changes based to the MQTT configuration of the device. Usually the topic is used LWT.
payload_on

(string)(Optional) The payload that rappshows access (sent together with command_topic).
Default value: true

payload_off

(string)(Optional) The payload that rappresets the shutdown (sent together with command_topic).
Default value: false

payload_available

(string)(Optional) The payload that interprets the status as "online"
Default value: "Online". Usually the payload is used LWT.

payload_not_available

(string)(Optional) The payload that interprets the status as "offline"
Default value: "Offline". Usually the payload is used LWT.

optimistic

(Boolean)(Optional) Flag that defines whether the switch works in optimistic mode.
Default value: true if noessun state_topic is defined, otherwise false.

qos

(integer) (Optional) Indicates the quality-of-service to be used. Default value: 0 (What is QoS?)

retain

(Boolean) (Optional) Indicates whether the published MQTT messages for this switch should use retain.
Default value: false

value_template

(string) (Oational) Defines whether or not to use a template to extract a certain value in the payload of state.

In light of these parameters, a type switch appare in the configuration as follows:

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

It is possible to integrate the same type of configuration not just to introduce up Home Assistant of the switches but also of the lights. Let's consider the case where you are using a Sonoff Basic (modified Tasmota) as a switch on a bedside lamp: on the home automation defined on Home Assistant we would like that switch appas a light point, rather than as a switch. The configuration block will be the same as described above, but indicated under "light:"

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

Status Census at the start of Home Assistant

At the start of Home Assistant that environment does not know the status of the MQTT switches possibly included in the configuration. To ensure that this status is detected immediately (and not after a few minutes, with the othernameautomatic switch on the part of the various switches) it is necessary to insert in the configuration an automatism that you publish on the broker a command topic to the switch (s) to be updated which results in a telemetry:

automation:
  alias: "Censimento stati MQTT"
  trigger:
    platform: homeassistant
    event: start
  condition: []
  action:
    service: mqtt.publish
    data:
      topic: "cmnd/Ventilatore/POWER"
      payload: ""
      retain: false

Under the block "action:"It will be possible to indicate which MQTT topics to indicate (in this case" cmnd / Fan / POWER ") in order to force a response from the MQTT components to get the status.

How to add switches and sensors Sonoff a Home Assistant


ITEAD Sonoff BasicATTENZIONE: remember that there is on our FORUM community an ad hoc section dedicated to Sonoff (and more generally to devices ESP8266), for any doubt, question, information on the specific merit of these components.


Please comment below