home-assistant.github.io/getting-started/automation-condition/index.html
2016-04-24 07:02:24 +00:00

309 lines
No EOL
15 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>Automation Conditions - Home Assistant</title>
<meta name="author" content="Paulus Schoutsen">
<meta name="description" content="Automations can test conditions when invoked.">
<meta name="viewport" content="width=device-width">
<link rel="canonical" href="https://home-assistant.io/getting-started/automation-condition/">
<meta property="fb:app_id" content="338291289691179">
<meta property="og:title" content="Automation Conditions">
<meta property="og:site_name" content="Home Assistant">
<meta property="og:url" content="https://home-assistant.io/getting-started/automation-condition/">
<meta property="og:type" content="website">
<meta property="og:description" content="Automations can test conditions when invoked.">
<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="Automation Conditions">
<meta name="twitter:description" content="Automations can test conditions when invoked.">
<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 href='/getting-started/'>Getting started</a></li>
<li><a href='/components/'>Components</a></li>
<li><a href='/cookbook/'>Examples</a></li>
<li><a href="/developers/">Developers</a></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">
Automation Conditions
</h1>
</header>
<hr class="divider">
<p>Conditions are an optional part of an automation rule and be used to prevent an action from happening when triggered. Conditions look very similar to triggers but are very different. A trigger will look at events happening in the system while a condition only looks at how the system looks right now. A trigger can observe that a switch is being turned on. A condition can only see if a switch is currently on or off.</p>
<p>An automation rule can have multiple conditions. By default the action will only fire if all conditions pass. An optional key <code>condition_type: 'or'</code> can be set on the automation rule to fire action if any condition matches. In the example below, the automation would trigger if the time is before 05:00 <em>OR</em> after 20:00.</p>
<div class="highlighter-coderay"><div class="CodeRay">
<div class="code"><pre><span class="key">automation</span>:
<span class="key">condition_type</span>: <span class="string"><span class="content">or</span></span>
<span class="key">condition</span>:
- <span class="string"><span class="content">platform: time</span></span>
<span class="key">before</span>: <span class="string"><span class="content">'05:00'</span></span>
- <span class="string"><span class="content">platform: time</span></span>
<span class="key">after</span>: <span class="string"><span class="content">'20:00'</span></span>
</pre></div>
</div>
</div>
<p>If your triggers and conditions are exactly the same, you can use a shortcut to specify conditions. In this case, triggers that are not valid conditions will be ignored.</p>
<div class="highlighter-coderay"><div class="CodeRay">
<div class="code"><pre><span class="key">automation</span>:
<span class="key">condition</span>: <span class="string"><span class="content">use_trigger_values</span></span>
</pre></div>
</div>
</div>
<h4><a class="title-link" name="numeric-state-condition" href="#numeric-state-condition"></a> Numeric state condition</h4>
<p>This type of condition attempts to parse the state of specified entity as a number and triggers if the value matches all of the above or below thresholds.<br />
Either <code>above</code> or <code>below</code>, or both need to be specified. If both are used, the condition is true when the value is &gt;= <code>before</code> *and** &lt; <code>after</code>.<br />
You can optionally use a <code>value_template</code> to make the value of the entity the same type of value as the condition.</p>
<div class="highlighter-coderay"><div class="CodeRay">
<div class="code"><pre><span class="key">automation</span>:
<span class="key">condition</span>:
<span class="key">platform</span>: <span class="string"><span class="content">numeric_state</span></span>
<span class="key">entity_id</span>: <span class="string"><span class="content">sensor.temperature</span></span>
<span class="key">above</span>: <span class="string"><span class="content">17</span></span>
<span class="key">below</span>: <span class="string"><span class="content">25</span></span>
</pre></div>
</div>
</div>
<h4><a class="title-link" name="state-condition" href="#state-condition"></a> State condition</h4>
<p>Tests if an entity is a specified state.</p>
<div class="highlighter-coderay"><div class="CodeRay">
<div class="code"><pre><span class="key">automation</span>:
<span class="key">condition</span>:
<span class="key">platform</span>: <span class="string"><span class="content">state</span></span>
<span class="key">entity_id</span>: <span class="string"><span class="content">device_tracker.paulus</span></span>
<span class="key">state</span>: <span class="string"><span class="content">not_home</span></span>
<span class="comment"># optional: trigger only if state was this for last X time.</span>
<span class="key">for</span>:
<span class="key">hours</span>: <span class="string"><span class="content">1</span></span>
<span class="key">minutes</span>: <span class="string"><span class="content">10</span></span>
<span class="key">seconds</span>: <span class="string"><span class="content">5</span></span>
</pre></div>
</div>
</div>
<h4><a class="title-link" name="sun-condition" href="#sun-condition"></a> Sun condition</h4>
<p>The sun condition can test if the sun has already set or risen when a trigger occurs. The <code>before</code> and <code>after</code> keys can only be set to <code>sunset</code> or <code>sunrise</code>. They have a corresponding optional offset value (<code>before_offset</code>, <code>after_offset</code>) that can be added, similar to the <a href="#sun-trigger">sun trigger</a>.</p>
<div class="highlighter-coderay"><div class="CodeRay">
<div class="code"><pre><span class="key">automation</span>:
<span class="key">condition</span>:
<span class="key">platform</span>: <span class="string"><span class="content">sun</span></span>
<span class="key">after</span>: <span class="string"><span class="content">sunset</span></span>
<span class="comment"># Optional offset value</span>
<span class="key">after_offset</span>: <span class="string"><span class="delimiter">&quot;</span><span class="content">-1:00:00</span><span class="delimiter">&quot;</span></span>
</pre></div>
</div>
</div>
<h4><a class="title-link" name="template-condition" href="#template-condition"></a> Template condition</h4>
<p>The template condition will test if the [given template][template] renders a value equal to true. This is achieved by having the template result in a true boolean expression or by having the template render true.</p>
<div class="highlighter-coderay"><div class="CodeRay">
<div class="code"><pre><span class="key">automation</span>:
<span class="key">condition</span>:
<span class="key">platform</span>: <span class="string"><span class="content">template</span></span>
<span class="key">value_template</span>: <span class="string"><span class="content">'{{ state.attributes.battery &gt; 50 }}'</span></span>
<span class="comment"># Or value_template could be:</span>
<span class="comment"># {% if state.attributes.battery &gt; 50 %}true{% else %}false{% endif %}</span>
</pre></div>
</div>
</div>
<h4><a class="title-link" name="time-condition" href="#time-condition"></a> Time condition</h4>
<p>The time condition can test if it is after a specified time, before a specified time or if it is a certain day of the week</p>
<div class="highlighter-coderay"><div class="CodeRay">
<div class="code"><pre><span class="key">automation</span>:
<span class="key">condition</span>:
<span class="key">platform</span>: <span class="string"><span class="content">time</span></span>
<span class="comment"># At least one of the following is required.</span>
<span class="key">after</span>: <span class="string"><span class="content">'15:00:00'</span></span>
<span class="key">before</span>: <span class="string"><span class="content">'23:00:00'</span></span>
<span class="key">weekday</span>:
- <span class="string"><span class="content">mon</span></span>
- <span class="string"><span class="content">wed</span></span>
- <span class="string"><span class="content">fri</span></span>
</pre></div>
</div>
</div>
<p>Valid values for <code>weekday</code> are (<code>sun</code>, <code>mon</code>, <code>tue</code>, <code>wed</code>, <code>thu</code>, <code>fri</code> &amp; <code>sat</code>)</p>
<h4><a class="title-link" name="zone-condition" href="#zone-condition"></a> Zone condition</h4>
<p>Zone conditions test if an entity is in a certain zone. For zone automation to work, you need to have setup a device tracker platform that supports reporting GPS coordinates. Currently this is limited to the <a href="/components/device_tracker.owntracks/">OwnTracks platform</a> and the <a href="/components/device_tracker.icloud/">iCloud platform</a>.</p>
<div class="highlighter-coderay"><div class="CodeRay">
<div class="code"><pre><span class="key">automation</span>:
<span class="key">condition</span>:
<span class="key">platform</span>: <span class="string"><span class="content">zone</span></span>
<span class="key">entity_id</span>: <span class="string"><span class="content">device_tracker.paulus</span></span>
<span class="key">zone</span>: <span class="string"><span class="content">zone.home</span></span>
</pre></div>
</div>
</div>
</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/home-assistant/home-assistant.io/tree/master/source/getting-started/automation-condition.markdown'>Edit this page on GitHub</a></div>
<div class='section'>
<h1 class="title delta">Getting Started Guide</h1>
<ul class='divided sidebar-menu'>
<li>
<a href='/getting-started/'>Installation </a>
<ul>
<li><a href='/getting-started/installation-raspberry-pi/'>Raspberry Pi </a></li>
<li><a href='/getting-started/installation-docker/'>Docker </a></li>
<li><a href='/getting-started/installation-synology/'>Synology NAS </a></li>
<li><a href='/getting-started/installation-virtualenv/'>VirtualEnv Linux Instructions </a></li>
<li><a href='/getting-started/troubleshooting/'>Troubleshooting </a></li>
</ul>
</li>
<li>
Configuration
<ul>
<li><a href='/getting-started/configuration/'>Configuration.yaml </a></li>
<li><a href='/getting-started/devices/'>Setting up devices </a></li>
<li><a href='/getting-started/customizing-devices/'>Customizing devices and services </a></li>
<li><a href='/getting-started/presence-detection/'>Presence Detection </a></li>
<li><a href='/getting-started/troubleshooting-configuration/'>Troubleshooting configuration.yaml </a></li>
</ul>
</li>
<li>
<a href='/getting-started/automation/'>Automation </a>
<ul>
<li><a href='/getting-started/automation-create-first/'>Tutorial </a></li>
<li><a href='/getting-started/automation-trigger/'>Triggers </a></li>
<li><a class='active' href='/getting-started/automation-condition/'>Conditions </a></li>
<li><a href='/getting-started/automation-action/'>Actions </a></li>
</ul>
</li>
<li>
<a href='/getting-started/autostart/'>Auto-start Home Assistant </a>
<ul>
<li><a href='/getting-started/autostart-systemd/'>Linux - SystemD </a></li>
<li><a href='/getting-started/autostart-upstart/'>Linux - Upstart </a></li>
<li><a href='/getting-started/autostart-macos/'>macOS </a></li>
<li><a href='/getting-started/autostart-synology/'>Synology NAS </a></li>
</ul>
</li>
<li><a href='/topics/'>Advanced Topics</a></li>
<li><a href='/getting-started/android/'>Add to Android Homescreen </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/home-assistant/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>
<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>