5.4 KiB
layout | title | description | date | sidebar | comments | sharing | footer |
---|---|---|---|---|---|---|---|
page | MQTT | Instructions how to setup MQTT within Home Assistant. | 2015-08-07 18:00 | false | false | true | true |

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
Configuration variables:
- broker (Required): The IP address of your MQTT broker, e.g. 192.168.1.32.
- port (Optional): The network port to connect to. Default is 1883.
- client_id (Optional): Client ID that Home Assistant will use. Has to be unique on the server. Default is a random generated one.
- keepalive (Optional): The keep alive in seconds 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): Certificate to use to encrypt communication with the broker.
{% linkable_title Picking a broker %}
The MQTT component needs you to run an MQTT broker for Home Assistant to connect to.
There are three options, each with various degrees of ease of setup and privacy.
{% linkable_title Run your own %}
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
{% linkable_title Public MQTT %}
The Mosquitto project runs a public broker. Easiest to setup but there is 0 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, 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 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) a. Under manage users, fill in username, password and click add b. 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 %}
- MQTT Sensor
- MQTT Switch
- MQTT Device Tracker
- OwnTracks Device Tracker
- MQTT automation rule
- MQTT alarm
- Integrating it into a component. See the MQTT example component how to do this.
{% linkable_title Testing your setup %}
For debugging purposes mosquitto
is shipping commandline tools to send and recieve MQTT messages. For sending test messages to a broker running on localhost:
mosquitto_pub -h 127.0.0.1 -t home-assistant/switch/1/on -m "Switch is ON"
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/#"