home-assistant.github.io/components/mqtt/index.html
2016-03-27 16:54:26 +00:00

567 lines
No EOL
24 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!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='/demo/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>Getting started <i class="icon icon-caret-down"></i></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='/topics/'>Advanced Topics</a></li>
</ul>
</li>
<li><a href='/components/'>Components</a></li>
<li><a href='/cookbook'>Examples</a></li>
<li>
<a>Developers <i class="icon icon-caret-down"></i></a>
<ul>
<li><a href="/developers/">Setup Development</a></li>
<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>
<span class="key">protocol</span>: <span class="string"><span class="content">3.1</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>
<li><strong>protocol</strong> (<em>Optional</em>): 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.</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 four options, each with various degrees of ease of setup and privacy.</p>
<h4><a class="title-link" name="use-the-embedded-broker" href="#use-the-embedded-broker"></a> Use the embedded broker</h4>
<p>Home Assistant contains an embedded MQTT broker. If no broker configuration is given, the <a href="https://pypi.python.org/pypi/hbmqtt">HBMQTT broker</a> is started and Home Asssistant connects to it. Embedded broker default configuration:</p>
<table>
<thead>
<tr>
<th>Setting</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td>Host</td>
<td>localhost</td>
</tr>
<tr>
<td>Port</td>
<td>1883</td>
</tr>
<tr>
<td>Protocol</td>
<td>3.1.1</td>
</tr>
<tr>
<td>User</td>
<td>homeassistant</td>
</tr>
<tr>
<td>Password</td>
<td>Your API password</td>
</tr>
<tr>
<td>Websocket port</td>
<td>8080</td>
</tr>
</tbody>
</table>
<p class="note">
This broker does not currently work with OwnTracks because of a protocol version issue.
</p>
<p>If you want to customize the settings of the embedded broker, use <code>embedded:</code> and the values shown in the <a href="http://hbmqtt.readthedocs.org/en/latest/references/broker.html#broker-configuration">HBMQTT Broker configuration</a>. This will replace the default configuration.</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">embedded</span>:
<span class="comment"># Your HBMQTT config here. Example at:</span>
<span class="comment"># http://hbmqtt.readthedocs.org/en/latest/references/broker.html#broker-configuration</span>
</pre></div>
</div>
</div>
<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>
<p class="note warning">
There is an issue with the Mosquitto package included in Ubuntu 14.04 LTS. Specify <code>protocol: 3.1</code> in your MQTT configuration to work around this issue.
</p>
<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, replace port 1883 with following 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">&lt;Server&gt;</span></span>
<span class="key">port</span>: <span class="string"><span class="content">&lt;SSL Port&gt;</span></span>
<span class="key">username</span>: <span class="string"><span class="content">&lt;User&gt;</span></span>
<span class="key">password</span>: <span class="string"><span class="content">&lt;Password&gt;</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/alarm_control_panel.mqtt/">MQTT Alarm control panel</a></li>
<li><a href="/components/binary_sensor.mqtt/">MQTT Binary sensor</a></li>
<li><a href="/components/sensor.mqtt/">MQTT Sensor</a></li>
<li><a href="/components/switch.mqtt/">MQTT Switch</a></li>
<li><a href="/components/light.mqtt/">MQTT Light</a></li>
<li><a href="/components/lock.mqtt/">MQTT Lock</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>
<p><a href="/components/automation/#mqtt-based-automation">MQTT automation rule</a></p>
</li>
<li>Integrating it into own component. See the <a href="/cookbook/python_component_mqtt_basic/">MQTT example component</a> how to do this.</li>
</ul>
<h3><a class="title-link" name="publish-service" href="#publish-service"></a> Publish service</h3>
<p>The MQTT component will register the service <code>publish</code> which allows publishing messages to MQTT topics. There are two ways of specifiying your payload. You can either use <code>payload</code> to hard-code a payload or use <code>payload_template</code> to specify a <a href="/topics/templating/">template</a> that will be rendered to generate the payload.</p>
<div class="highlighter-coderay"><div class="CodeRay">
<div class="code"><pre>{
<span class="key"><span class="delimiter">&quot;</span><span class="content">topic</span><span class="delimiter">&quot;</span></span>: <span class="string"><span class="delimiter">&quot;</span><span class="content">home-assistant/light/1/command</span><span class="delimiter">&quot;</span></span>,
<span class="key"><span class="delimiter">&quot;</span><span class="content">payload</span><span class="delimiter">&quot;</span></span>: <span class="string"><span class="delimiter">&quot;</span><span class="content">on</span><span class="delimiter">&quot;</span></span>
}
</pre></div>
</div>
</div>
<div class="highlighter-coderay"><div class="CodeRay">
<div class="code"><pre>{
<span class="key"><span class="delimiter">&quot;</span><span class="content">topic</span><span class="delimiter">&quot;</span></span>: <span class="string"><span class="delimiter">&quot;</span><span class="content">home-assistant/light/1/state</span><span class="delimiter">&quot;</span></span>,
<span class="key"><span class="delimiter">&quot;</span><span class="content">payload_template</span><span class="delimiter">&quot;</span></span>: <span class="string"><span class="delimiter">&quot;</span><span class="content">{{ states('device_tracker.paulus') }}</span><span class="delimiter">&quot;</span></span>
}
</pre></div>
</div>
</div>
<h2><a class="title-link" name="testing-your-setup" href="#testing-your-setup"></a> Testing your setup</h2>
<p>The <code>mosquitto</code> broker package is shipping commandline tools to send and recieve MQTT messages. As an alternative have a look at <a href="http://hbmqtt.readthedocs.org/en/latest/references/hbmqtt_pub.html">hbmqtt_pub</a> and <a href="http://hbmqtt.readthedocs.org/en/latest/references/hbmqtt_sub.html">hbmqtt_sub</a> which are provied by HBMQTT. For sending test messages to a broker running on localhost check the example below:</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 &quot;Switch is ON&quot;
</pre></div>
</div>
</div>
<p>If you are using the embeeded MQTT broker, the command looks a little different because you need to add the MQTT protocol version.</p>
<div class="highlighter-coderay"><div class="CodeRay">
<div class="code"><pre>$ mosquitto_pub -V mqttv311 -t hello -m world
</pre></div>
</div>
</div>
<p>or if you are using a API password.</p>
<div class="highlighter-coderay"><div class="CodeRay">
<div class="code"><pre>$ mosquitto_pub -V mqttv311 -u homeassistant -P &lt;your api password&gt; -t &quot;hello&quot; -m world
</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">&quot;</span><span class="content">topic</span><span class="delimiter">&quot;</span></span>:<span class="string"><span class="delimiter">&quot;</span><span class="content">home-assistant/switch/1/on</span><span class="delimiter">&quot;</span></span>,
<span class="key"><span class="delimiter">&quot;</span><span class="content">payload</span><span class="delimiter">&quot;</span></span>:<span class="string"><span class="delimiter">&quot;</span><span class="content">Switch is ON</span><span class="delimiter">&quot;</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 &lt;Event MQTT_MESSAGE_RECEIVED[L]: topic=home-assistant/switch/1/on, qos=0, payload=Switch is ON&gt;
</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 &quot;home-assistant/#&quot;
</pre></div>
</div>
</div>
<p>For the embeeded MQTT broker the command looks like the sample below.</p>
<div class="highlighter-coderay"><div class="CodeRay">
<div class="code"><pre>$ mosquitto_sub -V mqttv311 -t # -v
</pre></div>
</div>
</div>
<p>Add the username <code>homeassistant</code> and your API password if needed.</p>
<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='edit-github'><a href='https://github.com/balloob/home-assistant.io/tree/master/source/_components/mqtt.markdown'>Edit this page on GitHub</a></div>
<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/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/lock.mqtt/'>
MQTT Lock
</a></li>
<li><a href='/components/notify.mqtt/'>
MQTT Notifications
</a></li>
<li><a href='/components/rollershutter.mqtt/'>
MQTT Rollershutter
</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/apcupsd/'>APCUPSd</a>
</li>
<li>
<a href='/components/wemo/'>Belkin WeMo devices</a>
</li>
<li>
<a href='/components/bloomsky/'>BloomSky</a>
</li>
<li>
<a href='/components/ecobee/'>Ecobee</a>
</li>
<li>
<a href='/components/isy994/'>ISY994 Controller</a>
</li>
<li>
<a href='/components/insteon_hub/'>Insteon Hub</a>
</li>
<li>
MQTT
</li>
<li>
<a href='/components/modbus/'>Modbus</a>
</li>
<li>
<a href='/components/mysensors/'>MySensors</a>
</li>
<li>
<a href='/components/nest/'>Nest</a>
</li>
<li>
<a href='/components/rfxtrx/'>RFXtrx</a>
</li>
<li>
<a href='/components/scsgate/'>SCSGate</a>
</li>
<li>
<a href='/components/tellstick/'>TellStick</a>
</li>
<li>
<a href='/components/tellduslive/'>Telldus Live</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>