Site updated at 2017-09-23 00:58:45 UTC

This commit is contained in:
Travis CI 2017-09-23 00:58:45 +00:00
parent 68f6da8d25
commit a675f41451
95 changed files with 698 additions and 440 deletions

View file

@ -8,7 +8,7 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Template Light - Home Assistant</title>
<meta name="author" content="Home Assistant">
<meta name="description" content="Instructions how to integrate Template lights into Home Assistant.">
<meta name="description" content="Instructions how to integrate Template Lights into Home Assistant.">
<meta name="viewport" content="width=device-width">
<link rel="canonical" href="https://home-assistant.io/components/light.template/">
<meta property="fb:app_id" content="338291289691179">
@ -16,12 +16,12 @@
<meta property="og:site_name" content="Home Assistant">
<meta property="og:url" content="https://home-assistant.io/components/light.template/">
<meta property="og:type" content="article">
<meta property="og:description" content="Instructions how to integrate Template lights into Home Assistant.">
<meta property="og:description" content="Instructions how to integrate Template Lights into Home Assistant.">
<meta property="og:image" content="https://home-assistant.io/images/default-social.png">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:site" content="@home_assistant">
<meta name="twitter:title" content="Template Light">
<meta name="twitter:description" content="Instructions how to integrate Template lights into Home Assistant.">
<meta name="twitter:description" content="Instructions how to integrate Template Lights into Home Assistant.">
<meta name="twitter:image" content="https://home-assistant.io/images/default-social.png">
<link href="/stylesheets/screen.css" media="screen, projection" rel="stylesheet">
<link href="/atom.xml" rel="alternate" title="Home Assistant" type="application/atom+xml">
@ -74,15 +74,19 @@
</h1>
</header>
<hr class="divider">
<p>The <code class="highlighter-rouge">template</code> platform creates lights that combine components and provides the ability to run scripts or invoke services for each of the on, off, and brightness commands of a light.</p>
<p>To enable Template lights in your installation, add the following to your <code class="highlighter-rouge">configuration.yaml</code> file:</p>
<p>The <code class="highlighter-rouge">template</code> platform creates lights that combine components and provides the
ability to run scripts or invoke services for each of the on, off, and
brightness commands of a light.</p>
<p>To enable Template Lights in your installation, add the following to your
<code class="highlighter-rouge">configuration.yaml</code> file:</p>
<div class="language-yaml highlighter-rouge"><pre class="highlight"><code><span class="c1"># Example configuration.yaml entry</span>
<span class="s">light</span><span class="pi">:</span>
<span class="pi">-</span> <span class="s">platform</span><span class="pi">:</span> <span class="s">template</span>
<span class="s">lights</span><span class="pi">:</span>
<span class="s">theater_lights</span><span class="pi">:</span>
<span class="s">friendly_name</span><span class="pi">:</span> <span class="s2">"</span><span class="s">Theater</span><span class="nv"> </span><span class="s">Lights"</span>
<span class="s">value_template</span><span class="pi">:</span> <span class="s2">"</span><span class="s">{{is_state('sensor.theater_brightness.attributes.lux</span><span class="nv"> </span><span class="s">&gt;</span><span class="nv"> </span><span class="s">0'}}"</span>
<span class="s">level_template</span><span class="pi">:</span> <span class="s2">"</span><span class="s">{{</span><span class="nv"> </span><span class="s">sensor.theater_brightness.attributes.lux|int</span><span class="nv"> </span><span class="s">}}"</span>
<span class="s">value_template</span><span class="pi">:</span> <span class="s2">"</span><span class="s">{{</span><span class="nv"> </span><span class="s">sensor.theater_brightness.attributes.lux|int</span><span class="nv"> </span><span class="s">&gt;</span><span class="nv"> </span><span class="s">0</span><span class="nv"> </span><span class="s">}}"</span>
<span class="s">turn_on</span><span class="pi">:</span>
<span class="s">service</span><span class="pi">:</span> <span class="s">script.theater_lights_on</span>
<span class="s">turn_off</span><span class="pi">:</span>
@ -90,48 +94,85 @@
<span class="s">set_level</span><span class="pi">:</span>
<span class="s">service</span><span class="pi">:</span> <span class="s">script.theater_lights_level</span>
<span class="s">data_template</span><span class="pi">:</span>
<span class="s">brightness</span><span class="pi">:</span> <span class="s2">"</span><span class="s">{{brightness}}"</span>
<span class="s">level_template</span><span class="pi">:</span> <span class="s2">"</span><span class="s">{{is_state('sensor.theater_brightness.attributes.lux'}}"</span>
<span class="s">brightness</span><span class="pi">:</span> <span class="s2">"</span><span class="s">{{</span><span class="nv"> </span><span class="s">brightness</span><span class="nv"> </span><span class="s">}}"</span>
</code></pre>
</div>
<p>Configuration variables:</p>
<ul>
<li><strong>lights</strong> array (<em>Required</em>): List of your lights.
<ul>
<li><strong>friendly_name</strong> (<em>Optional</em>): Name to use in the Frontend.</li>
<li><strong>value_template</strong> (<em>Optional</em>): Defines a <a href="/topics/templating/">template</a> to get the state of the light. If not provided the component defaults to optimisitc state determination.</li>
<li><strong>turn_on</strong> (<em>Required</em>): Defines an <a href="/getting-started/automation/">action</a> to run when the light is turned on.</li>
<li><strong>turn_off</strong> (<em>Required</em>): Defines an <a href="/getting-started/automation/">action</a> to run when the light is turned off.</li>
<li><strong>set_level</strong> (<em>Optional</em>): Defines an <a href="/getting-started/automation/">action</a> to run when the light is given a brightness command.</li>
<li><strong>level_template</strong> (*Optional): Defines a <a href="/topics/templating/">template</a> to get the brightness of the light. If not provided the component defaults to optimisitc brightness determination.</li>
<li><strong>entity_id</strong> (<em>Optional</em>): Add a list of entity IDs so the switch only reacts to state changes of these entities. This will reduce the number of times the light will try to update its state.</li>
</ul>
</li>
</ul>
<div class="config-vars">
<h3><a class="title-link" name="configuration-variables" href="#configuration-variables"></a> Configuration Variables</h3>
<dl class="">
<dt><a class="title-link" name="switches" href="#switches"></a> switches</dt>
<dd>
<p class="desc"><span class="type">(<span class="map">map</span>)</span><span class="required">(Required)</span><span class="description">List of your lights.</span></p>
</dd>
<dd>
<dl class="nested">
<dt><a class="title-link" name="friendly_name" href="#friendly_name"></a> friendly_name</dt>
<dd>
<p class="desc"><span class="type">(<span class="string">string</span>)</span><span class="required">(Optional)</span><span class="description">Name to use in the frontend.</span></p>
</dd>
<dt><a class="title-link" name="entity_id" href="#entity_id"></a> entity_id</dt>
<dd>
<p class="desc"><span class="type">(<span class="string list">string | list</span>)</span><span class="required">(Optional)</span><span class="description">Add a list of entity IDs so the switch only reacts to state changes of these entities. This will reduce the number of times the light will try to update its state.</span></p>
</dd>
<dt><a class="title-link" name="value_template" href="#value_template"></a> value_template</dt>
<dd>
<p class="desc"><span class="type">(<span class="template"><a href="/docs/configuration/templating/">template</a></span>)</span><span class="required">(Optional)</span><span class="description">Defines a template to get the state of the light.</span></p>
<p class="default">Default value: optimistic</p>
</dd>
<dt><a class="title-link" name="level_template" href="#level_template"></a> level_template</dt>
<dd>
<p class="desc"><span class="type">(<span class="template"><a href="/docs/configuration/templating/">template</a></span>)</span><span class="required">(Optional)</span><span class="description">Defines a template to get the brightness of the light.</span></p>
<p class="default">Default value: optimistic</p>
</dd>
<dt><a class="title-link" name="turn_on" href="#turn_on"></a> turn_on</dt>
<dd>
<p class="desc"><span class="type">(<span class="action"><a href="/docs/scripts/">action</a></span>)</span><span class="required">(Required)</span><span class="description">Defines an action to run when the light is turned on.</span></p>
</dd>
<dt><a class="title-link" name="turn_off" href="#turn_off"></a> turn_off</dt>
<dd>
<p class="desc"><span class="type">(<span class="action"><a href="/docs/scripts/">action</a></span>)</span><span class="required">(Required)</span><span class="description">Defines an action to run when the light is turned off.</span></p>
</dd>
<dt><a class="title-link" name="set_level" href="#set_level"></a> set_level</dt>
<dd>
<p class="desc"><span class="type">(<span class="action"><a href="/docs/scripts/">action</a></span>)</span><span class="required">(Optional)</span><span class="description">Defines an action to run when the light is given a brightness command.</span></p>
</dd>
</dl>
</dd>
</dl>
</div>
<h2><a class="title-link" name="considerations" href="#considerations"></a> Considerations</h2>
<p>If you are using the state of a platform that takes extra time to load, the template light may get an unknown state during startup. This results in error messages in your log file until that platform has completed loading. If you use is_state() function in your template, you can avoid this situation. For example, you would replace {{ states.switch.source.state }} with this equivalent that returns true/false and never gives an unknown result:
{{ is_state(switch.source, on) }}</p>
<p>If you are using the state of a platform that takes extra time to load, the
Template Light may get an <code class="highlighter-rouge">unknown</code> state during startup. This results
in error messages in your log file until that platform has completed loading.
If you use <code class="highlighter-rouge">is_state()</code> function in your template, you can avoid this situation.
For example, you would replace
<code class="highlighter-rouge"><span class="p">{</span><span class="err">{</span><span class="w"> </span><span class="err">states.switch.source.state</span><span class="w"> </span><span class="err">==</span><span class="w"> </span><span class="err">'on'</span><span class="w"> </span><span class="p">}</span><span class="err">}</span></code>
with this equivalent that returns <code class="highlighter-rouge">true</code>/<code class="highlighter-rouge">false</code> and never gives an unknown
result:
<code class="highlighter-rouge"><span class="p">{</span><span class="err">{</span><span class="w"> </span><span class="err">is_state('switch.source',</span><span class="w"> </span><span class="err">'on')</span><span class="w"> </span><span class="p">}</span><span class="err">}</span></code></p>
<h2><a class="title-link" name="examples" href="#examples"></a> Examples</h2>
<p>In this section you will find some real life examples of how to use this light.</p>
<h3><a class="title-link" name="theater-volume-control" href="#theater-volume-control"></a> Theater Volume Control</h3>
<p>This example shows a light that is actually a home theaters volume. This component gives you the flexibility to provide whatever youd like to send as the payload to the consumer including any scale conversions you may need to make; the media_player component needs a floating point percentage value 0.0-1.0</p>
<p>This example shows a light that is actually a home theaters volume. This
component gives you the flexibility to provide whatever youd like to send as
the payload to the consumer including any scale conversions you may need to
make; the <a href="/components/media_player/">Media Player component</a> needs a floating
point percentage value from <code class="highlighter-rouge">0.0</code> to <code class="highlighter-rouge">1.0</code>.</p>
<div class="language-yaml highlighter-rouge"><pre class="highlight"><code><span class="s">light</span><span class="pi">:</span>
<span class="pi">-</span> <span class="s">platform</span><span class="pi">:</span> <span class="s">template</span>
<span class="s">lights</span><span class="pi">:</span>
<span class="s">theater_volume</span><span class="pi">:</span>
<span class="s">friendly_name</span><span class="pi">:</span> <span class="s1">'</span><span class="s">Receiver</span><span class="nv"> </span><span class="s">Volume'</span>
<span class="s">friendly_name</span><span class="pi">:</span> <span class="s2">"</span><span class="s">Receiver</span><span class="nv"> </span><span class="s">Volume"</span>
<span class="s">value_template</span><span class="pi">:</span> <span class="pi">&gt;-</span>
<span class="no"> </span>
<span class="no">{%- if is_state("media_player.receiver", "on") -%}</span>
<span class="no">{%- if states.media_player.receiver.attributes.is_volume_muted -%}</span>
<span class="no">off</span>
<span class="no">{%- else -%}</span>
<span class="no">on</span>
<span class="no">{%- endif -%}</span>
<span class="no">{%- else -%}</span>
<span class="no">{% if is_state('media_player.receiver', 'on') %}</span>
<span class="no">{% if states.media_player.receiver.attributes.is_volume_muted %}</span>
<span class="no">off</span>
<span class="no">{% else %}</span>
<span class="no">on</span>
<span class="no">{% endif %}</span>
<span class="no">{% else %}</span>
<span class="no">off</span>
<span class="no">{%- endif -%}</span>
<span class="no"> </span>
<span class="no">{% endif %}</span>
<span class="no">turn_on:</span>
<span class="no">service: media_player.volume_mute</span>
<span class="no">data:</span>
@ -144,18 +185,15 @@
<span class="no">is_volume_muted: true</span>
<span class="no">set_level:</span>
<span class="no">service: media_player.volume_set</span>
<span class="no">data:</span>
<span class="no">entity_id: media_player.receiver</span>
<span class="no">data_template:</span>
<span class="no">volume_level: '{{((brightness / 255 * 100) | int)/100}}'</span>
<span class="no">entity_id: media_player.receiver</span>
<span class="no">volume_level: "{{ (brightness / 255 * 100)|int / 100 }}"</span>
<span class="no">level_template: &gt;-</span>
<span class="no"> </span>
<span class="no">{%- if is_state("media_player.receiver", "on") -%}</span>
<span class="no">{{(255 * states.media_player.receiver.attributes.volume_level) | int}}</span>
<span class="no">{%- else -%}</span>
<span class="no">{% if is_state('media_player.receiver', 'on') %}</span>
<span class="no">{{ (states.media_player.receiver.attributes.volume_level|float * 255)|int }}</span>
<span class="no">{% else %}</span>
<span class="no">0</span>
<span class="no">{%- endif -%}</span>
<span class="no"> </span>
<span class="no">{% endif %}</span>
</code></pre>
</div>
</article>
@ -190,7 +228,7 @@
Template Sensor
</a></li>
<li><a href='/components/switch.template/'>
Template switch
Template Switch
</a></li>
</ul>
</div>