Platform Home Assistant "MQTT Switch”

3 minutes of reading
Proconductor: Home Assistant Community
Availability: included in thepersonale HUB Home Assistant
Category: software
Type: platform Home Assistant
Family: component "Switch” Home Assistant
Implementation difficulties: half
Card revision: 1.2

The “MQTT Switch”, daughter of the component "Switch”, serves to define of the simple ones entity switch type useful, appanointed, in managing through Home Assistant of on / off type home automation actuators that they expect to be controlled by proProtocol MQTT (for example those with firmware Sonoff-Tasmota).

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.

Regardless, it is advisable to approach the implementation of this platform alone in the presence of at least one competence basis on the topic MQTT.

Configuration

To make it as easy as possible to understand the functionnameof a controllable switch via protocollo MQTT it is necessary to know that, ideally, it has a topic of command and a telemetric topic.

The first (its presence is mandatory) is used to send commands to the actuator, the second (usually present) to receive from it the answers to these commands or in any case the status changes (maybe triggered manually via buttons or other).

Example:

  • sending by Home Assistant of the command topic to the broker addressed to the device;
  • the device, registered with the broker, receives the command assigned to it;
  • the device executes the command and sends a telemetric response topic to the broker;
  • Home Assistant, is in turn registered with the broker, receives the telemetry and effectively confirms the status to the type "MQTT Switch”From which the command started.

In the absence of telemetric topics (therefore it is impossible to indicate them in the configuration of Home Assistant), theHUB behaves in a way optimistic, ie it assumes that, when a command is implemented, it has actually been executed. Obviously any changes of state implemented directly on the device will not be received by Home Assistant.

To add an (elementary) "MQTT Switch"Below configuration of Home Assistant, the block is the following:

switch:
  - platform: mqtt
    command_topic: "cmnd/Sonoff/POWER"

Explanation of used and usable fields:

command_topic(String, mandatory) The MQTT topic to be published to Home Assistant in order to control the switch.
payload_on(String, optional) The payload to be sent together with the command indicated in command_topic in order to turn on the switch.
payload_off(String, optional) The payload to be sent together with the command indicated in command_topic in order to turn off the switch.
name
(String, optional) Describes the name of the entity. Default: MQTT Switch
icon(Icon, optional) Graphic icon for rappresite the switch, which refer to those publicly available at http://materialdesignicons.com. The syntax expects to indicate the name of the icon preceded by the prefix “mdi:
Example: "mdi:history"
state_topic
(String, optional) The MQTT topic to which to register to get the operational status changes by the controlled device.
state_on
(String, optional) The payload that, if received together with the state_topic, assumes that the switch is on. If not specified, the default is "ON"
state_off(String, optional) The payload that, if received together with the state_topic, assumes that the switch is off. If not specified, the default is "OFF"
availability_topic
(String, optional) The MQTT topic to which to register to get the status online / offline by the controlled device. Usually the topic is used LWT.
payload_available(String, optional) The payload that, if received together with the availability_topic, assumes that the switch is in service. If not specified, the default is "Online". Usually the payload is used LWT.
payload_not_available(String, optional) The payload that, if received together with the availability_topic, assumes that the switch is out of service. If not specified, the default is "Offline". Usually the topic is used LWT.
optimistic(Boolean, optional) Indicates explicitly if the switch is working in mode optimistic or not. The default is true in absence of state_topic, otherwise it is false.
qos(integer, optional) - The QoS level for the state_topic. Default: 0 (What is QoS?)
retain(Boolean, optional) Whether to set or not retain. Default: false
json_attribues_topic(String, optional) The MQTT topic to subscribe to receive a JSON payload and set a relative sensor.
device(Map, optional) Indicates any details related to the device. It only works through MQTT Discovery. More info on the platform detail page at the site Home Assistant.
USE OF TEMPLATES

In each "* _state_topic”Can be defined as a template for the extraction (parsing) of the status data. It can also be defined one for all, using the proproperty "value_template". This translates into a great comfort especially when there are payloads written in JSON notation.

Configuration example

Below is a slightly richer configuration than the base:

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

If devices with ESPEASY firmware are used, the ad hoc configuration is described here.

Guide

An implementation guide for this platform is available here.

Add an MQTT switch to the configuration Home Assistant


Home Assistant Official LogoATTENZIONE: remember that there is on our FORUM community an ad hoc section dedicated to Home Assistant, for any doubt, question, information on the specific merit of these components.

inDomus telegram channel