home-assistant.github.io/topics/service_calls/index.html
2016-03-13 00:06:00 +00:00

248 lines
No EOL
10 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>Service Calls - Home Assistant</title>
<meta name="author" content="Paulus Schoutsen">
<meta name="description" content="Instructions how to call services in Home Assistant.">
<meta name="viewport" content="width=device-width">
<link rel="canonical" href="https://home-assistant.io/topics/service_calls/">
<meta property="fb:app_id" content="338291289691179">
<meta property="og:title" content="Service Calls">
<meta property="og:site_name" content="Home Assistant">
<meta property="og:url" content="https://home-assistant.io/topics/service_calls/">
<meta property="og:type" content="article">
<meta property="og:description" content="Instructions how to call services in 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="Service Calls">
<meta name="twitter:description" content="Instructions how to call services in 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">
<div class='edit-github'><a href='https://github.com/balloob/home-assistant.io/tree/master/source/_topics/service_calls.markdown'>Edit this page on GitHub</a></div>
<header>
<h1 class="title indent">
Service Calls
</h1>
</header>
<hr class="divider">
<p>Various components allow calling services when a certain event occurs. The most common one is calling a service when an automation trigger happens. But a service can also be called from a script or via the Amazon Echo.</p>
<p>The configuration options to call a config are the same between all components and are described on this page.</p>
<p>Examples on this page will be given as part of an automation component configuration but different approaches can be used for other components too.</p>
<p class="note">
Use the service developer tool in the frontend to discover available services.
</p>
<h3><a class="title-link" name="the-basics" href="#the-basics"></a> The basics</h3>
<p>Call the service <code>homeassistant.turn_on</code> on the entity <code>group.living_room</code>. This will turn all members of <code>group.living_room</code> on. You can also omit <code>entity_id</code> and it will turn on all possible entities.</p>
<div class="highlighter-coderay"><div class="CodeRay">
<div class="code"><pre><span class="key">automation</span>:
- <span class="string"><span class="content">trigger:</span><span class="content">
platform: sun
event: sunset</span></span>
<span class="key">action</span>:
<span class="key">service</span>: <span class="string"><span class="content">homeassistant.turn_on</span></span>
<span class="key">entity_id</span>: <span class="string"><span class="content">group.living_room</span></span>
</pre></div>
</div>
</div>
<h3><a class="title-link" name="passing-data-to-the-service-call" href="#passing-data-to-the-service-call"></a> Passing data to the service call</h3>
<p>You can also specify other parameters beside the entity to target. For example, the light turn on service allows specifying the brightness.</p>
<div class="highlighter-coderay"><div class="CodeRay">
<div class="code"><pre><span class="key">automation</span>:
- <span class="string"><span class="content">trigger:</span><span class="content">
platform: sun
event: sunset</span></span>
<span class="key">action</span>:
<span class="key">service</span>: <span class="string"><span class="content">light.turn_on</span></span>
<span class="key">entity_id</span>: <span class="string"><span class="content">group.living_room</span></span>
<span class="key">data</span>:
<span class="key">brightness</span>: <span class="string"><span class="content">120</span></span>
<span class="key">rgb_color</span>: <span class="string"><span class="content">[255, 0, 0]</span></span>
</pre></div>
</div>
</div>
<h3><a class="title-link" name="use-templates-to-decide-which-service-to-call" href="#use-templates-to-decide-which-service-to-call"></a> Use templates to decide which service to call</h3>
<p>You can use <a href="/topics/templating/">templating</a> support to dynamically choose which service to call. For example, you can call a certain service based on if a light is on.</p>
<div class="highlighter-coderay"><div class="CodeRay">
<div class="code"><pre><span class="key">automation</span>:
- <span class="string"><span class="content">trigger:</span><span class="content">
platform: sun
event: sunset</span></span>
<span class="key">action</span>:
<span class="key">service_template</span>: <span class="string"><span class="delimiter">&gt;</span><span class="content">
{% if states.sensor.temperature | float &gt; 15 %}
switch.turn_on
{% else %}
switch.turn_off
{% endif %}</span></span>
<span class="key">entity_id</span>: <span class="string"><span class="content">switch.ac</span></span>
</pre></div>
</div>
</div>
<h3><a class="title-link" name="use-templates-to-determine-the-attributes" href="#use-templates-to-determine-the-attributes"></a> Use templates to determine the attributes</h3>
<p>Templates can also be used for the data that you pass to the service call.</p>
<div class="highlighter-coderay"><div class="CodeRay">
<div class="code"><pre><span class="key">automation</span>:
- <span class="string"><span class="content">trigger:</span><span class="content">
platform: sun
event: sunset</span></span>
<span class="key">action</span>:
<span class="key">service_template</span>: <span class="string"><span class="content">thermostat.set_temperature</span></span>
<span class="key">data_template</span>:
<span class="key">entity_id</span>: <span class="string"><span class="delimiter">&gt;</span><span class="content">
{% if is_state('device_tracker.paulus', 'home') %}
thermostat.upstairs
{% else %}
thermostat.downstairs
{% endif %}</span></span>
<span class="key">temperature</span>: <span class="string"><span class="content">{{ 22 - distance(states.device_tracker.paulus) }}</span></span>
</pre></div>
</div>
</div>
</article>
</div>
</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>