9.7 KiB
layout | title | description | date | sidebar | comments | sharing | footer | logo | ha_category | featured | ha_release | ha_iot_class |
---|---|---|---|---|---|---|---|---|---|---|---|---|
page | MQTT | Instructions how to setup MQTT within Home Assistant. | 2015-08-07 18:00 | true | false | true | true | mqtt.png | Hub | true | pre 0.7 | depends |
MQTT (aka MQ Telemetry Transport) is a machine-to-machine or "Internet of Things" connectivity protocol on top of TCP/IP. It allows extremely lightweight publish/subscribe messaging transport.
To integrate MQTT into Home Assistant, add the following section to your configuration.yaml
file:
# Example configuration.yaml entry
mqtt:
broker: IP_ADDRESS_BROKER
port: 1883
client_id: home-assistant-1
keepalive: 60
username: USERNAME
password: PASSWORD
certificate: /home/paulus/dev/addtrustexternalcaroot.crt
protocol: 3.1
Configuration variables:
- broker (Required): The IP address or hostname of your MQTT broker, e.g. 192.168.1.32.
- port (Optional): The network port to connect to. Default is 1883.
- client_id (Optional): The client ID that Home Assistant will use. Has to be unique on the server. Default is a randomly generated one.
- keepalive (Optional): The time in seconds between sending keep alive messages for this client. Default is 60.
- username (Optional): The username to use with your MQTT broker.
- password (Optional): The corresponding password for the username to use with your MQTT broker.
- certificate (Optional): The certificate authority certificate file that is to be treated as trusted by this client. This file should contain the root certificate of the certificate authority that signed your broker's certificate, but may contain multiple certificates. Example:
/home/user/identrust-root.pem
- client_key (Optional): Client key (example:
/home/user/owntracks/cookie.key
) - client_cert (Optional): Client certificate (example:
/home/user/owntracks/cookie.crt
) - protocol (Optional): Protocol to use: 3.1 or 3.1.1. By default it connects with 3.1.1 and falls back to 3.1 if server does not support 3.1.
{% linkable_title Picking a broker %}
The MQTT component needs you to run an MQTT broker for Home Assistant to connect to. There are four options, each with various degrees of ease of setup and privacy.
{% linkable_title Embedded broker %}
Home Assistant contains an embedded MQTT broker. If no broker configuration is given, the HBMQTT broker is started and Home Assistant connects to it. Embedded broker default configuration:
Setting | Value |
---|---|
Host | localhost |
Port | 1883 |
Protocol | 3.1.1 |
User | homeassistant |
Password | Your API password |
Websocket port | 8080 |
This broker does not currently work with OwnTracks because of a protocol version issue.
If you want to customize the settings of the embedded broker, use embedded:
and the values shown in the HBMQTT Broker configuration. This will replace the default configuration.
# Example configuration.yaml entry
mqtt:
embedded:
# Your HBMQTT config here. Example at:
# http://hbmqtt.readthedocs.org/en/latest/references/broker.html#broker-configuration
{% linkable_title Run your own %}
This is the most private option but requires a bit more work. There are two free and open-source brokers to pick from: Mosquitto and Mosca.
# Example configuration.yaml entry
mqtt:
broker: 192.168.1.100
port: 1883
client_id: home-assistant-1
keepalive: 60
username: USERNAME
password: PASSWORD
There is an issue with the Mosquitto package included in Ubuntu 14.04 LTS. Specify `protocol: 3.1` in your MQTT configuration to work around this issue.
{% linkable_title Public broker %}
The Mosquitto project runs a public broker. This is the easiest to set up, but there is no privacy as all messages are public. Use this only for testing purposes and not for real tracking of your devices.
mqtt:
broker: test.mosquitto.org
port: 1883
# Optional, replace port 1883 with following if you want encryption
# (doesn't really matter because broker is public)
port: 8883
# Download certificate from http://test.mosquitto.org/ssl/mosquitto.org.crt
certificate: /home/paulus/downloads/mosquitto.org.crt
{% linkable_title CloudMQTT %}
CloudMQTT is a hosted private MQTT instance that is free for up to 10 connected devices. This is enough to get started with for example OwnTracks and give you a taste of what is possible.
Home Assistant is not affiliated with CloudMQTT nor will receive any kickbacks.
- Create an account (no payment details needed)
- Create a new CloudMQTT instance (Cute Cat is the free plan)
- From the control panel, click on the Details button.
- Create unique users for Home Assistant and each phone to connect
(CloudMQTT does not allow two connections from the same user)- Under manage users, fill in username, password and click add
- Under ACLs, select user, topic
#
, check 'read access' and 'write access'
- Copy the instance info to your configuration.yaml:
mqtt:
broker: <Server>
port: <SSL Port>
username: <User>
password: <Password>
Home Assistant will automatically load the correct certificate if you connect to an encrypted channel of CloudMQTT (port range 20 000 - 30 000).
{% linkable_title Building on top of MQTT %}
-
Integrating it into own component. See the MQTT example component how to do this.
{% linkable_title Publish service %}
The MQTT component will register the service publish
which allows publishing messages to MQTT topics. There are two ways of specifying your payload. You can either use payload
to hard-code a payload or use payload_template
to specify a template that will be rendered to generate the payload.
{
"topic": "home-assistant/light/1/command",
"payload": "on"
}
{
"topic": "home-assistant/light/1/state",
"payload_template": "{% raw %}{{ states('device_tracker.paulus') }}{% endraw %}"
}
{% linkable_title Logging %}
The logger component allow the logging of received MQTT messages.
# Example configuration.yaml entry
logger:
default: warning
logs:
homeassistant.components.device_tracker.mqtt: debug
{% linkable_title Testing your setup %}
The mosquitto
broker package ships commandline tools to send and recieve MQTT messages. As an alternative have a look at hbmqtt_pub and hbmqtt_sub which are provied by HBMQTT. For sending test messages to a broker running on localhost check the example below:
$ mosquitto_pub -h 127.0.0.1 -t home-assistant/switch/1/on -m "Switch is ON"
If you are using the embedded MQTT broker, the command looks a little different because you need to add the MQTT protocol version.
$ mosquitto_pub -V mqttv311 -t "hello" -m world
or if you are using a API password:
$ mosquitto_pub -V mqttv311 -u homeassistant -P <your api password> -t "hello" -m world
Another way to send MQTT messages by hand is to use the "Developer Tools" in the Frontend. Choose "Call Service" and then mqtt/mqtt_send
under "Available Services". Enter something similar to the example below into the "Service Data" field.
{
"topic":"home-assistant/switch/1/on",
"payload":"Switch is ON"
}
The message should appear on the bus:
... [homeassistant] Bus:Handling <Event MQTT_MESSAGE_RECEIVED[L]: topic=home-assistant/switch/1/on, qos=0, payload=Switch is ON>
For reading all messages sent on the topic home-assistant
to a broker running on localhost:
$ mosquitto_sub -h 127.0.0.1 -v -t "home-assistant/#"
For the embedded MQTT broker the command looks like:
$ mosquitto_sub -v -V mqttv311 -t "#"
Add the username homeassistant
and your API password if needed.
{% linkable_title Processing JSON %}
The MQTT switch and sensor platforms support processing JSON over MQTT messages and parsing them using JSONPath. JSONPath allows you to specify where in the JSON the value resides that you want to use. The following examples will always return the value 100
.
JSONPath query | JSON |
---|---|
somekey |
{ 'somekey': 100 } |
somekey[0] |
{ 'somekey': [100] } |
somekey[0].value |
{ 'somekey': [ { value: 100 } ] } |
To use this, add the following key to your configuration.yaml
:
switch:
platform: mqtt
state_format: 'json:somekey[0].value'
More information about the full JSONPath syntax can be found here.