Components Home Assistant "MQTT"

4 minutes of reading
Proconductor: Home Assistant Community
Availability: included in theHUB Home Assistant
Category: software
Type: components Home Assistant
Implementation difficulties: low
Card revision: 1.3

The "MQTT" component di Home Assistant serves to configure the basic functionality of the proMQTT tocollo under this specification personale HUB. It enables configuration of a certain number of platforms connected to it, suitable for the management / control of elements such as:

  • Alarm control panels
  • Sensorthe tracks
  • IP Camera
  • Dark for finestre
  • GPS tracking
  • Fans
  • Air conditioning
  • Luci
  • Locks
  • Notifications
  • Sensorcustomizable generics
  • Switches
  • Vacuum cleaner

All pants these accessories (and related platforms for implementation) depend on the configuration, upstream, of the MQTT component.

To add this component to Home Assistant it is sufficient that in the configuration file configuration.yaml this line is present:

# Example configuration.yaml entry
mqtt:

Where nothing else is indicated, Home Assistant prosees you start an MQTT broker in autonomy and directly uses that. For more stability the developers of Home Assistant specify that it is preferable to use an external broker (Eg. Eclipse Mosquitto).

To configure an external broker, you need to set up a configuration like the following:

# MQTT
mqtt:
  broker: 127.0.0.1
  username: NOME_UTENTE
  password: PASSWORD
  port: 1883
  client_id: home-assistant
  keepalive: 60

where in the broker field we will insert the IP of the broker MQTT (in this case the IP is indicated 127.0.0.1 because the broker is running on the same macChina in which it is running Home Assistant). The fields username e password they must be indicated and personalized only if the broker being used foresees an authentication.

To activate MQTT on Home Assistant, enough of the above.

Nb MQTT is not difficult to implement, but to do this you need to have some concepts clearly in mind. To this proposito reading is recommended careful was originally the guide dedicated to the configuration of the MQTT components in the profirst home automation.

Additional features

Use of certificates

Home Assistant allows to specify in the configuration the use of cryptographic certificates for communication to and from the broker MQTT.

For this purpose, the configuration is as follows:

mqtt:
  certificate: /home/pi/dev/addtrustexternalcaroot.crt

where /home/pi/dev/addtrustexternalcaroot.crt is the path / file name of the certificate.

Configuration variables:

certificateOptional. 'auto' or the path / file name of the certificate to be used. If a file is specified, it must contain the root certificate of the certifying body that signed the certificate. The file can contain multiple certificates.
Example: /home/user/identrust-root.pem
client_keyOptional. Path / file name of the client's encryption key.
Example: /home/user/owntracks/cookie.key 
client_certOptional. Path / file name of the client's cryptographic certificate.
Example: /home/user/owntracks/cookie.crt 

Discovery

The "discovery" function of the MQTT component at Home Assistant it's extremely convenient, because it allows to avoid the punctual configuration of every device present in home automation (risking duplicates and misconfigurations). Once activated, that prosee the census and the entry in the configuration of the following types of accessories (where they display MQTT topics, of course):

  • Sensorthe tracks
  • IP Camera
  • dark
  • Fans
  • Luci
  • Sensorcustom generics
  • Switches

To enable discovery, it is sufficient to indicate on the configuration file configuration.yaml of Home Assistant:

mqtt:
  discovery: true
  discovery_prefix: homeassistant

Configuration variables:

discoveryOptional. Enable or disable discovery.
Example: 'true'O'false'
discovery_prefixOptional. Indicates the prefix for the client's MQTT topic Home Assistant.
The default is'homeassistant'. The prefix must follow a specific format:

<discovery_prefix>/<component>/[<node_id>/]<object_id>/<>

Where:

<Discovery_prefix> One of the supported components, eg. 'binary_sensor'
<NODE_ID>Optional. The ID of the node that provides the topic.
<Object_id>The device ID. It is used to distinguish the device in the topic
<>The command, the action to follow. g. 'ON'O'OFF'.

Firmware for IoT components based on ESP8266, ESP32 and devices ITEAD Sonoff which offer the MQTT auto-discovery service are the following:

Further information and examples of use on the Discovery are present on the dedicated page of the MQTT component on site Home Assistant.

Publish Service

As you can imagine, through the MQTT implementation of Home Bridge You can also publish topics.
There are two methods to publish a payload: by explaining its value or by using a template:

{
"topic": "home-assistant/light/1/command",
"payload": "on"
}
{
"topic": "home-assistant/light/1/state",
"payload_template": "{{ states('device_tracker.paulus') }}"
}

Birth and Last will

MQTT supports the so-called wills of first and last wishes, appgreasy LWT (Last Will Testament). In essence they are commands to be executed when an MQTT device "is born", that is it turns on and introduces itself to the broker (and therefore to anyone who is subscribed to those topics on the broker) or when "he dies", or unexpectedly exits the network (I physically turn off a device).

Also Home Assistant allows the publication of LWT type topics, both for the "birth" and for the "will":

mqtt:
  birth_message:
    topic: 'hass/status'
    payload: 'online'
  will_message:
   topic: 'hass/status'
   payload: 'offline'

Log

The "log" component of Home Assistant allows to track MQTT messages. Simply add the following block to the configuration:

logger:
  default: warning
  logs:
    homeassistant.components.mqtt: debug

Procease JSON content

The components "Switch" e "Sensor" di Home Assistant support the processing JSON via MQTT messages and interpret them via JSONPath. JSONPath allows us to specify where, within a JSON content, a certain value resides.
The following examples always return the value "100"

JSONPath queryJSON
somekey{ 'somekey': 100 }
somekey [0]{ 'somekey': [100] }
somekey [0] .value{ 'somekey': [ { value: 100 } ] }

To use it within the configuration of Home Assistant, a type block is inserted:

switch:
  platform: mqtt
  state_format: 'json:somekey[0].value'

It is also possible to extract JSON values ​​using a value template:

switch:
  platform: mqtt
  value_template: '{{ value_json.somekey[0].value }}'

More syntax information is available on the JSONPath repository.

More information on testing of MQTT configurations for Home Assistant are available on site 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