415 lines
No EOL
18 KiB
HTML
415 lines
No EOL
18 KiB
HTML
<!doctype html>
|
||
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
|
||
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
|
||
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
|
||
<!--[if gt IE 8]><!--> <html> <!--<![endif]-->
|
||
|
||
<head>
|
||
<meta charset="utf-8">
|
||
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
|
||
<title>MQTT - Home Assistant</title>
|
||
<meta name="author" content="Paulus Schoutsen">
|
||
<meta name="description" content="Instructions how to setup MQTT within Home Assistant.">
|
||
|
||
<meta name="viewport" content="width=device-width">
|
||
<link rel="canonical" href="https://home-assistant.io/components/mqtt/">
|
||
|
||
<meta property="fb:app_id" content="338291289691179">
|
||
<meta property="og:title" content="MQTT">
|
||
<meta property="og:site_name" content="Home Assistant">
|
||
<meta property="og:url" content="https://home-assistant.io/components/mqtt/">
|
||
<meta property="og:type" content="article">
|
||
<meta property="og:description" content="Instructions how to setup MQTT within Home Assistant.">
|
||
<meta property="og:image" content="https://home-assistant.io/images/home-assistant-logo-2164x2164.png">
|
||
|
||
<meta name="twitter:card" content="summary">
|
||
<meta name="twitter:site" content="@home_assistant">
|
||
|
||
<meta name="twitter:title" content="MQTT">
|
||
<meta name="twitter:description" content="Instructions how to setup MQTT within Home Assistant.">
|
||
<meta name="twitter:image" content="https://home-assistant.io/images/home-assistant-logo-2164x2164.png">
|
||
|
||
<link href="/stylesheets/screen.css" media="screen, projection" rel="stylesheet">
|
||
<link href="/atom.xml" rel="alternate" title="Home Assistant" type="application/atom+xml">
|
||
<link rel='shortcut icon' href='/images/favicon.ico' />
|
||
<link rel='icon' type='image/png' href='/images/favicon-192x192.png' sizes='192x192' />
|
||
</head>
|
||
|
||
<body >
|
||
|
||
<header>
|
||
<div class="grid-wrapper">
|
||
<div class="grid">
|
||
|
||
<div class="grid__item three-tenths lap-two-sixths palm-one-whole ha-title">
|
||
<a href="/" class="site-title">
|
||
<img width='40' src='/images/favicon-192x192.png'>
|
||
<span>Home Assistant</span>
|
||
</a>
|
||
</div>
|
||
|
||
<div class="grid__item seven-tenths lap-four-sixths palm-one-whole">
|
||
<nav>
|
||
<input type="checkbox" id="toggle">
|
||
<label for="toggle" class="toggle" data-open="Main Menu" data-close="Close Menu"></label>
|
||
<ul class="menu pull-right">
|
||
<li>
|
||
<a href="/getting-started/">Getting started</a>
|
||
<ul>
|
||
<li><a href='/getting-started/'>Installing Home Assistant</a></li>
|
||
<li><a href='/getting-started/configuration/'>Configuration basics</a></li>
|
||
<li><a href='/getting-started/devices/'>Adding devices</a></li>
|
||
<li><a href='/getting-started/presence-detection/'>Presence detection</a></li>
|
||
<li><a href='/getting-started/automation/'>Automation</a></li>
|
||
<li><a href='/getting-started/templating/'>Templating</a></li>
|
||
<li><a href='/cookbook'>Configuration cookbook</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a href='/components/'>Components</a></li>
|
||
<li>
|
||
<a href="/developers/">Developers</a>
|
||
<ul>
|
||
<li><a href="/developers/architecture/">Architecture</a></li>
|
||
<li><a href="/developers/frontend/">Frontend development</a></li>
|
||
<li><a href="/developers/creating_components/">
|
||
Creating components
|
||
</a></li>
|
||
<li><a href="/developers/add_new_platform/">
|
||
Adding platform support
|
||
</a></li>
|
||
<li><a href="/developers/api/">API</a></li>
|
||
<li><a href="/developers/credits/">Credits</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a href="/blog/">Blog</a></li>
|
||
<li><a href="/help/">Need help?</a></li>
|
||
</ul>
|
||
</nav>
|
||
</div>
|
||
|
||
</div>
|
||
</div>
|
||
</header>
|
||
|
||
|
||
|
||
<div class="grid-wrapper">
|
||
<div class="grid grid-center">
|
||
|
||
<div class="grid__item two-thirds lap-one-whole palm-one-whole">
|
||
|
||
|
||
<article class="page">
|
||
<header>
|
||
<h1 class="title indent">
|
||
MQTT
|
||
</h1>
|
||
</header>
|
||
<hr class="divider">
|
||
|
||
<p>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.</p>
|
||
|
||
<p>To integrate MQTT into Home Assistant, add the following section to your <code>configuration.yaml</code> file:</p>
|
||
|
||
<div class="highlighter-coderay"><div class="CodeRay">
|
||
<div class="code"><pre><span class="comment"># Example configuration.yaml entry</span>
|
||
<span class="key">mqtt</span>:
|
||
<span class="key">broker</span>: <span class="string"><span class="content">IP_ADDRESS_BROKER</span></span>
|
||
<span class="key">port</span>: <span class="string"><span class="content">1883</span></span>
|
||
<span class="key">client_id</span>: <span class="string"><span class="content">home-assistant-1</span></span>
|
||
<span class="key">keepalive</span>: <span class="string"><span class="content">60</span></span>
|
||
<span class="key">username</span>: <span class="string"><span class="content">USERNAME</span></span>
|
||
<span class="key">password</span>: <span class="string"><span class="content">PASSWORD</span></span>
|
||
<span class="key">certificate</span>: <span class="string"><span class="content">/home/paulus/dev/addtrustexternalcaroot.crt</span></span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
|
||
<p>Configuration variables:</p>
|
||
|
||
<ul>
|
||
<li><strong>broker</strong> (<em>Required</em>): The IP address of your MQTT broker, e.g. 192.168.1.32.</li>
|
||
<li><strong>port</strong> (<em>Optional</em>): The network port to connect to. Default is 1883.</li>
|
||
<li><strong>client_id</strong> (<em>Optional</em>): Client ID that Home Assistant will use. Has to be unique on the server. Default is a random generated one.</li>
|
||
<li><strong>keepalive</strong> (<em>Optional</em>): The keep alive in seconds for this client. Default is 60.</li>
|
||
<li><strong>username</strong> (<em>Optional</em>): The username to use with your MQTT broker.</li>
|
||
<li><strong>password</strong> (<em>Optional</em>): The corresponding password for the username to use with your MQTT broker.</li>
|
||
<li><strong>certificate</strong> (<em>Optional</em>): Certificate to use to encrypt communication with the broker.</li>
|
||
</ul>
|
||
|
||
<h2><a class="title-link" name="picking-a-broker" href="#picking-a-broker"></a> Picking a broker</h2>
|
||
|
||
<p>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.</p>
|
||
|
||
<h4><a class="title-link" name="run-your-own" href="#run-your-own"></a> Run your own</h4>
|
||
|
||
<p>Most private option but requires a bit more work. There are two free and open-source brokers to pick from: <a href="http://mosquitto.org/">Mosquitto</a> and <a href="http://www.mosca.io/">Mosca</a>.</p>
|
||
|
||
<div class="highlighter-coderay"><div class="CodeRay">
|
||
<div class="code"><pre><span class="comment"># Example configuration.yaml entry</span>
|
||
<span class="key">mqtt</span>:
|
||
<span class="key">broker</span>: <span class="string"><span class="content">192.168.1.100</span></span>
|
||
<span class="key">port</span>: <span class="string"><span class="content">1883</span></span>
|
||
<span class="key">client_id</span>: <span class="string"><span class="content">home-assistant-1</span></span>
|
||
<span class="key">keepalive</span>: <span class="string"><span class="content">60</span></span>
|
||
<span class="key">username</span>: <span class="string"><span class="content">USERNAME</span></span>
|
||
<span class="key">password</span>: <span class="string"><span class="content">PASSWORD</span></span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
|
||
<h4><a class="title-link" name="public-mqtt" href="#public-mqtt"></a> Public MQTT</h4>
|
||
|
||
<p>The Mosquitto project runs a <a href="http://test.mosquitto.org">public broker</a>. 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.</p>
|
||
|
||
<div class="highlighter-coderay"><div class="CodeRay">
|
||
<div class="code"><pre><span class="key">mqtt</span>:
|
||
<span class="key">broker</span>: <span class="string"><span class="content">test.mosquitto.org</span></span>
|
||
<span class="key">port</span>: <span class="string"><span class="content">1883</span></span>
|
||
|
||
<span class="comment"># Optional, if you want encryption</span>
|
||
<span class="comment"># (doesn't really matter because broker is public)</span>
|
||
<span class="key">port</span>: <span class="string"><span class="content">8883</span></span>
|
||
<span class="comment"># Download certificate from http://test.mosquitto.org/ssl/mosquitto.org.crt</span>
|
||
<span class="key">certificate</span>: <span class="string"><span class="content">/home/paulus/downloads/mosquitto.org.crt</span></span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
|
||
<h4><a class="title-link" name="cloudmqtt" href="#cloudmqtt"></a> CloudMQTT</h4>
|
||
|
||
<p><a href="https://www.cloudmqtt.com">CloudMQTT</a> is a hosted private MQTT instance that is free up to 10 connected devices. This is enough to get started with for example <a href="/components/device_tracker.owntracks/">OwnTracks</a> and give you a taste of what is possible.</p>
|
||
|
||
<p class="note">
|
||
Home Assistant is not affiliated with CloudMQTT nor will receive any kickbacks.
|
||
</p>
|
||
|
||
<ol>
|
||
<li><a href="https://customer.cloudmqtt.com/login">Create an account</a> (no payment details needed)</li>
|
||
<li><a href="https://customer.cloudmqtt.com/subscription/create">Create a new CloudMQTT instance</a><br />
|
||
(Cute Cat is the free plan)</li>
|
||
<li>From the control panel, click on the <em>Details</em> button.</li>
|
||
<li>Create unique users for Home Assistant and each phone to connect<br />(CloudMQTT does not allow two<br />
|
||
connections from the same user)<br />
|
||
a. Under manage users, fill in username, password and click add<br />
|
||
b. Under ACLs, select user, topic <code>#</code>, check ‘read access’ and ‘write access’</li>
|
||
<li>Copy the instance info to your configuration.yaml:</li>
|
||
</ol>
|
||
|
||
<div class="highlighter-coderay"><div class="CodeRay">
|
||
<div class="code"><pre><span class="key">mqtt</span>:
|
||
<span class="key">broker</span>: <span class="string"><span class="content"><Server></span></span>
|
||
<span class="key">port</span>: <span class="string"><span class="content"><SSL Port></span></span>
|
||
<span class="key">username</span>: <span class="string"><span class="content"><User></span></span>
|
||
<span class="key">password</span>: <span class="string"><span class="content"><Password></span></span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
|
||
<p class="note">
|
||
Home Assistant will automatically load the correct certificate if you connect to an encrypted channel of CloudMQTT (port range 20 000 - 30 000).
|
||
</p>
|
||
|
||
<h2><a class="title-link" name="building-on-top-of-mqtt" href="#building-on-top-of-mqtt"></a> Building on top of MQTT</h2>
|
||
|
||
<ul>
|
||
<li><a href="/components/sensor.mqtt/">MQTT Sensor</a></li>
|
||
<li><a href="/components/switch.mqtt/">MQTT Switch</a></li>
|
||
<li><a href="/components/device_tracker.mqtt/">MQTT Device Tracker</a></li>
|
||
<li><a href="/components/device_tracker.owntracks/">OwnTracks Device Tracker</a></li>
|
||
<li><a href="/components/automation/#mqtt-based-automation">MQTT automation rule</a></li>
|
||
<li><a href="/components/alarm_control_panel.mqtt/">MQTT alarm</a></li>
|
||
<li>Integrating it into own component. See the <a href="https://github.com/balloob/home-assistant/blob/dev/config/custom_components/mqtt_example.py">MQTT example component</a> how to do this.</li>
|
||
</ul>
|
||
|
||
<h2><a class="title-link" name="testing-your-setup" href="#testing-your-setup"></a> Testing your setup</h2>
|
||
|
||
<p>For debugging purposes <code>mosquitto</code> is shipping commandline tools to send and recieve MQTT messages. For sending test messages to a broker running on localhost:</p>
|
||
|
||
<div class="highlighter-coderay"><div class="CodeRay">
|
||
<div class="code"><pre>$ mosquitto_pub -h 127.0.0.1 -t home-assistant/switch/1/on -m "Switch is ON"
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
|
||
<p>Another way to send MQTT messages by hand is to use the “Developer Tools” in the Frontend. Choose “Call Service” and then <code>mqtt/mqtt_send</code> under “Available Services”. Enter something similar to the example below into the “Service Data” field.</p>
|
||
|
||
<div class="highlighter-coderay"><div class="CodeRay">
|
||
<div class="code"><pre>{
|
||
<span class="key"><span class="delimiter">"</span><span class="content">topic</span><span class="delimiter">"</span></span>:<span class="string"><span class="delimiter">"</span><span class="content">home-assistant/switch/1/on</span><span class="delimiter">"</span></span>,
|
||
<span class="key"><span class="delimiter">"</span><span class="content">payload</span><span class="delimiter">"</span></span>:<span class="string"><span class="delimiter">"</span><span class="content">Switch is ON</span><span class="delimiter">"</span></span>
|
||
}
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
|
||
<p>The message should appear on the bus:</p>
|
||
|
||
<div class="highlighter-coderay"><div class="CodeRay">
|
||
<div class="code"><pre>... [homeassistant] Bus:Handling <Event MQTT_MESSAGE_RECEIVED[L]: topic=home-assistant/switch/1/on, qos=0, payload=Switch is ON>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
|
||
<p>For reading all messages sent on the topic <code>home-assistant</code> to a broker running on localhost:</p>
|
||
|
||
<div class="highlighter-coderay"><div class="CodeRay">
|
||
<div class="code"><pre>$ mosquitto_sub -h 127.0.0.1 -v -t "home-assistant/#"
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
|
||
<h2><a class="title-link" name="processing-json" href="#processing-json"></a> Processing JSON</h2>
|
||
|
||
<p>The MQTT switch and sensor platforms support processing JSON over MQTT messages and parse 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 <code>100</code>.</p>
|
||
|
||
<table>
|
||
<thead>
|
||
<tr>
|
||
<th>JSONPath query</th>
|
||
<th>JSON</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr>
|
||
<td><code>somekey</code></td>
|
||
<td><code>{ 'somekey': 100 }</code></td>
|
||
</tr>
|
||
<tr>
|
||
<td><code>somekey[0]</code></td>
|
||
<td><code>{ 'somekey': [100] }</code></td>
|
||
</tr>
|
||
<tr>
|
||
<td><code>somekey[0].value</code></td>
|
||
<td><code>{ 'somekey': [ { value: 100 } ] }</code></td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
|
||
<p>To use this, add the following key to your <code>configuration.yaml</code>:</p>
|
||
|
||
<div class="highlighter-coderay"><div class="CodeRay">
|
||
<div class="code"><pre><span class="key">switch</span>:
|
||
<span class="key">platform</span>: <span class="string"><span class="content">mqtt</span></span>
|
||
<span class="key">state_format</span>: <span class="string"><span class="content">'json:somekey[0].value'</span></span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
|
||
<p>More information about the full JSONPath syntax can be found <a href="https://github.com/kennknowles/python-jsonpath-rw#jsonpath-syntax">here</a>.</p>
|
||
|
||
|
||
</article>
|
||
|
||
|
||
</div>
|
||
|
||
|
||
<aside id="sidebar" class="grid__item one-third lap-one-whole palm-one-whole">
|
||
<div class="grid">
|
||
|
||
|
||
<section class="aside-module grid__item one-whole lap-one-half">
|
||
<div class='brand-logo-container section'>
|
||
<img src='/images/supported_brands/mqtt.png' />
|
||
</div>
|
||
<div class='section'>
|
||
<h1 class='title delta'>Related components</h1>
|
||
<ul class='divided'>
|
||
<li><a href='/components/alarm_control_panel.mqtt/'>
|
||
MQTT Alarm Control Panel
|
||
</a></li>
|
||
<li><a href='/components/rollershutter.mqtt/'>
|
||
MQTT Rollershutter
|
||
</a></li>
|
||
<li><a href='/components/binary_sensor.mqtt/'>
|
||
MQTT binary sensor
|
||
</a></li>
|
||
<li><a href='/components/device_tracker.mqtt/'>
|
||
MQTT device tracker
|
||
</a></li>
|
||
<li><a href='/components/light.mqtt/'>
|
||
MQTT light
|
||
</a></li>
|
||
<li><a href='/components/sensor.mqtt/'>
|
||
MQTT sensor
|
||
</a></li>
|
||
<li><a href='/components/switch.mqtt/'>
|
||
MQTT switch
|
||
</a></li>
|
||
</ul>
|
||
</div>
|
||
<div class='section'>
|
||
<h1 class="title delta">Category Hub</h1>
|
||
<ul class='divided'>
|
||
<li>
|
||
<a href='/components/ecobee/'>Ecobee</a>
|
||
</li>
|
||
<li>
|
||
<a href='/components/isy994/'>ISY994 Controller</a>
|
||
</li>
|
||
<li>
|
||
MQTT
|
||
</li>
|
||
<li>
|
||
<a href='/components/modbus/'>Modbus</a>
|
||
</li>
|
||
<li>
|
||
<a href='/components/rfxtrx/'>RFXtrx</a>
|
||
</li>
|
||
<li>
|
||
<a href='/components/tellstick/'>TellStick</a>
|
||
</li>
|
||
<li>
|
||
<a href='/components/vera/'>Vera</a>
|
||
</li>
|
||
<li>
|
||
<a href='/components/verisure/'>Verisure</a>
|
||
</li>
|
||
<li>
|
||
<a href='/components/wink/'>Wink</a>
|
||
</li>
|
||
<li>
|
||
<a href='/components/zwave/'>Z-Wave</a>
|
||
</li>
|
||
</ul>
|
||
</div>
|
||
</section>
|
||
|
||
</div>
|
||
</aside>
|
||
|
||
</div>
|
||
</div>
|
||
|
||
<footer>
|
||
<div class="grid-wrapper">
|
||
<div class="grid">
|
||
<div class="grid__item">
|
||
<div class="copyright">
|
||
<a rel="me" href='https://twitter.com/home_assistant'><i class="icon-twitter"></i></a>
|
||
<a rel="me" href='https://github.com/balloob/home-assistant'><i class="icon-github"></i></a>
|
||
|
||
<div class="credit">
|
||
Website powered by <a href='http://jekyllrb.com/'>Jekyll</a> and the <a href='https://github.com/coogie/oscailte'>Oscalite theme</a>.<br />
|
||
Hosted by <a href='https://pages.github.com/'>GitHub</a> and served by <a href='https://cloudflare.com'>CloudFlare</a>.
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</footer>
|
||
|
||
<!--[if lt IE 7]>
|
||
<p class="chromeframe">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> or <a href="http://www.google.com/chromeframe/?redirect=true">activate Google Chrome Frame</a> to improve your experience.</p>
|
||
<![endif]-->
|
||
|
||
<script>
|
||
var _gaq=[['_setAccount','UA-57927901-1'],['_trackPageview']];
|
||
(function(d,t){var g=d.createElement(t),s=d.getElementsByTagName(t)[0];
|
||
g.src=('https:'==location.protocol?'//ssl':'//www')+'.google-analytics.com/ga.js';
|
||
s.parentNode.insertBefore(g,s)}(document,'script'));
|
||
</script>
|
||
|
||
</body>
|
||
</html> |