Automation docs moved to getting started and split up

This commit is contained in:
Paulus Schoutsen 2016-04-24 09:00:24 +02:00
parent 2cea0e718f
commit f74237f582
39 changed files with 532 additions and 475 deletions

View file

@ -0,0 +1,36 @@
---
layout: page
title: "Automation Actions"
description: "Automations result in action."
date: 2016-04-24 08:30 +0100
sidebar: true
comments: false
sharing: true
footer: true
---
When an automation rule fires, it calls a service. For this service you can specify the entity_id that it should apply to and optional service parameters (to specify for example the brightness).
```yaml
automation:
# Change the light in the kitchen and living room to 150 brightness and color red.
action:
service: homeassistant.turn_on
entity_id:
- light.kitchen
- light.living_room
data:
brightness: 150
rgb_color: [255, 0, 0]
```
```yaml
automation:
# Notify me on my mobile phone of an event
action:
service: notify.notify
data:
message: Something just happened, better take a look!
```
If you want to specify multiple services to be called, or to include a delay, have a look at the [script component](/components/script/). If you want to describe the desired state of certain entities, check out the [scene component](/components/scene/).

View file

@ -0,0 +1,121 @@
---
layout: page
title: "Automation Conditions"
description: "Automations can test conditions when invoked."
date: 2016-04-24 08:30 +0100
sidebar: true
comments: false
sharing: true
footer: true
---
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.
An automation rule can have multiple conditions. By default the action will only fire if all conditions pass. An optional key `condition_type: 'or'` 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 _OR_ after 20:00.
```yaml
automation:
condition_type: or
condition:
- platform: time
before: '05:00'
- platform: time
after: '20:00'
```
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.
```yaml
automation:
condition: use_trigger_values
```
#### {% linkable_title Numeric state condition %}
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.
Either `above` or `below`, or both need to be specified. If both are used, the condition is true when the value is >= `before` *and** < `after`.
You can optionally use a `value_template` to make the value of the entity the same type of value as the condition.
```yaml
automation:
condition:
platform: numeric_state
entity_id: sensor.temperature
above: 17
below: 25
```
#### {% linkable_title State condition %}
Tests if an entity is a specified state.
```yaml
automation:
condition:
platform: state
entity_id: device_tracker.paulus
state: not_home
# optional: trigger only if state was this for last X time.
for:
hours: 1
minutes: 10
seconds: 5
```
#### {% linkable_title Sun condition %}
The sun condition can test if the sun has already set or risen when a trigger occurs. The `before` and `after` keys can only be set to `sunset` or `sunrise`. They have a corresponding optional offset value (`before_offset`, `after_offset`) that can be added, similar to the [sun trigger](#sun-trigger).
```yaml
automation:
condition:
platform: sun
after: sunset
# Optional offset value
after_offset: "-1:00:00"
```
#### {% linkable_title Template condition %}
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'.
```yaml
automation:
condition:
platform: template
value_template: '{% raw %}{{ state.attributes.battery > 50 }}{% endraw %}'
# Or value_template could be:
# {% raw %}{% if state.attributes.battery > 50 %}true{% else %}false{% endif %}{% endraw %}
```
#### {% linkable_title Time condition %}
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
```yaml
automation:
condition:
platform: time
# At least one of the following is required.
after: '15:00:00'
before: '23:00:00'
weekday:
- mon
- wed
- fri
```
Valid values for `weekday` are (`sun`, `mon`, `tue`, `wed`, `thu`, `fri` & `sat`)
#### {% linkable_title Zone condition %}
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 [OwnTracks platform](/components/device_tracker.owntracks/) and the [iCloud platform](/components/device_tracker.icloud/).
```yaml
automation:
condition:
platform: zone
entity_id: device_tracker.paulus
zone: zone.home
```

View file

@ -0,0 +1,100 @@
---
layout: page
title: "Your First Automation"
description: "Step by step guiding through making your first automation."
date: 2016-04-24 08:30 +0100
sidebar: true
comments: false
sharing: true
footer: true
---
Before we dive deeper into what every piece of automation _can_ do, let's look at a simple automation rule: **Turn on the lights when the sun sets**
In this example, we are defining a trigger to track the sunset and tell it to fire when the sun is setting. When this event is triggered, the service `light.turn_on` is called without any parameters. Because we specify no parameters, it will turn on all the lights.
```yaml
# Example configuration.yaml entry
automation:
alias: Turn on light when sun sets
trigger:
platform: sun
event: sunset
action:
service: light.turn_on
```
After a few days of running this automation rule you come to realize that this automation rule is not good enough. It was already dark when the lights went on and the one day you weren't home, the lights turned on anyway. Time for some tweaking. Let's add an offset to the sunset trigger and a condition to only turn on the lights if anyone is home.
```yaml
# Example configuration.yaml entry
automation:
alias: Turn on light when sun sets
trigger:
platform: sun
event: sunset
offset: "-01:00:00"
condition:
platform: state
entity_id: group.all_devices
state: home
action:
service: light.turn_on
```
Now you're happy and all is good. You start to like this automation business and buy some more lights, this time you put them in the bedroom. But what you now realize is that when the sun is setting, the lights in the bedroom are also being turned on! Time to tweak the automation to only turn on the living room lights.
The first thing you do is to look at the entities in the developer tools (second icon) in the app. You see the names of your lights and you write them down: `light.table_lamp`, `light.bedroom`, `light.ceiling`.
Instead of hard coding the entity IDs of the lights in the automation rule, we will set up a group. This will allow us to see the living room separate in the app and be able to address it from automation rules.
So we tweak the config to add the group and have the automation rule only turn on the group.
```yaml
# Example configuration.yaml entry
group:
living_room:
- light.table_lamp
- light.ceiling
automation:
alias: Turn on light when sun sets
trigger:
platform: sun
event: sunset
offset: "-01:00:00"
condition:
platform: state
entity_id: group.all_devices
state: home
action:
service: light.turn_on
entity_id: group.living_room
```
Christmas is coming along and you decide to buy a remote switch to control the Christmas lights from Home Assistant. You can't claim to live in the house of the future if you're still manually turning on your Christmas lights!
We hook the switch up to Home Assistant and grab the entity ID0 from the developer tools: `switch.christmas_lights`. We will update the group to include the switch and will change our action. We are no longer able to call `light.turn_on` because we also want to turn on a switch. This is where `homeassistant.turn_on` comes to the rescue. This service is capable of turning any entity on.
```yaml
# Example configuration.yaml entry
group:
living_room:
- light.table_lamp
- light.ceiling
- switch.christmas_lights
automation:
alias: Turn on light when sun sets
trigger:
platform: sun
event: sunset
offset: "-01:00:00"
condition:
platform: state
entity_id: group.all_devices
state: home
action:
service: homeassistant.turn_on
entity_id: group.living_room
```

View file

@ -0,0 +1,64 @@
---
layout: page
title: "Automation Examples"
description: "Some automation examples to get you started."
date: 2016-04-24 08:30 +0100
sidebar: true
comments: false
sharing: true
footer: true
---
```yaml
# Example of entry in configuration.yaml
automation:
# Turns on lights 1 hour before sunset if people are home
# and if people get home between 16:00-23:00
- alias: 'Rule 1 Light on in the evening'
trigger:
# Prefix the first line of each trigger configuration
# with a '-' to enter multiple
- platform: sun
event: sunset
offset: '-01:00:00'
- platform: state
entity_id: group.all_devices
state: 'home'
condition:
# Prefix the first line of each condition configuration
# with a '-'' to enter multiple
- platform: state
entity_id: group.all_devices
state: 'home'
- platform: time
after: '16:00:00'
before: '23:00:00'
action:
service: homeassistant.turn_on
entity_id: group.living_room
# Turn off lights when everybody leaves the house
- alias: 'Rule 2 - Away Mode'
trigger:
platform: state
entity_id: group.all_devices
state: 'not_home'
action:
service: light.turn_off
entity_id: group.all_lights
# Notify when Paulus leaves the house in the evening
- alias: 'Leave Home notification'
trigger:
platform: zone
event: leave
zone: zone.home
entity_id: device_tracker.paulus
condition:
platform: time
after: '20:00'
action:
service: notify.notify
data:
message: 'Paulus left the house'
```

View file

@ -0,0 +1,140 @@
---
layout: page
title: "Automation Trigger"
description: "All the different ways how automations can be triggered."
date: 2016-04-24 08:30 +0100
sidebar: true
comments: false
sharing: true
footer: true
---
Triggers are what starts the processing of an automation rule. It is possible to specify multiple triggers for the same rule. Once a trigger starts, Home Assistant will validate the conditions, if any, and call the action.
#### {% linkable_title Event trigger %}
Triggers when an event is being processed. Events are the raw building blocks of Home Assistant. You can match events on just the event name or also require specific event data to be present.
```yaml
automation:
trigger:
platform: event
event_type: MY_CUSTOM_EVENT
# optional
event_data:
mood: happy
```
#### {% linkable_title MQTT trigger %}
Triggers when a specific message is received on given topic. Optionally can match on the payload being sent over the topic.
```yaml
automation:
trigger:
platform: mqtt
topic: living_room/switch/ac
# Optional
payload: 'on'
```
#### {% linkable_title Numeric state trigger %}
On state change of a specified entity, attempts to parse the state as a number and triggers if value is above and/or below a threshold.
```yaml
automation:
trigger:
platform: numeric_state
entity_id: sensor.temperature
# Optional
value_template: '{% raw %}{{ state.attributes.battery }}{% endraw %}'
# At least one of the following required
above: 17
below: 25
```
#### {% linkable_title State trigger %}
Triggers when the state of an entity changes. If only entity_id given will match all state changes.
```yaml
automation:
trigger:
platform: state
entity_id: device_tracker.paulus
# Optional
from: 'not_home'
to: 'home'
# If given, will trigger when state has been the to state for X time.
for:
hours: 1
minutes: 10
seconds: 5
```
<p class='note warning'>
Use quotes around your values for `from` and `to` to avoid the YAML parser interpreting values as booleans.
</p>
#### {% linkable_title Sun trigger %}
Trigger when the sun is setting or rising. An optional time offset can be given to have it trigger for example 45 minutes before sunset, when dusk is setting in.
```yaml
automation:
trigger:
platform: sun
# Possible values: sunset, sunrise
event: sunset
# Optional time offset. This example is 45 minutes.
offset: '-00:45:00'
```
#### {% linkable_title Template trigger %}
Template triggers work by evaluating a [template] on each state change. The trigger will fire if the state change caused the template to render 'true'. This is achieved by having the template result in a true boolean expression (`{% raw %}{{ is_state('device_tracker.paulus', 'home') }}{% endraw %}`) or by having the template render 'true' (example below).
```yaml
automation:
trigger:
platform: template
value_template: '{% raw %}{% if is_state('device_tracker.paulus', 'home') %}true{% endif %}{% endraw %}'
```
#### {% linkable_title Time trigger %}
Time can be triggered in many ways. The most common is to specify `after` and trigger at a specific point in time each day. Alternatively, you can also match if the hour, minute or second of the current time has a specific value. You can prefix the value with a `/` to match whenever the value is divisible by that number. You cannot use `after` together with hour, minute or second.
```yaml
automation:
trigger:
platform: time
# Matches every hour at 5 minutes past whole
minutes: 5
seconds: 0
automation 2:
trigger:
platform: time
# When 'after' is used, you cannot also match on hour, minute, seconds.
# Military time format.
after: '15:32:00'
automation 3:
trigger:
platform: time
# You can also match on interval. This will match every 5 minutes
minutes: '/5'
seconds: 0
```
#### {% linkable_title Zone trigger %}
Zone triggers can trigger when an entity is entering or leaving the 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 [OwnTracks platform](/components/device_tracker.owntracks/) as well as the [iCloud platform](/components/device_tracker.icloud/).
```yaml
automation:
trigger:
platform: zone
entity_id: device_tracker.paulus
zone: zone.home
# Event is either enter or leave
event: enter
```

View file

@ -0,0 +1,25 @@
---
layout: page
title: "Troubleshooting Automations"
description: "Tips on how to troubleshoot your automations."
date: 2016-04-24 08:30 +0100
sidebar: true
comments: false
sharing: true
footer: true
---
You can verify that your automation rules are being initialized correctly by watching both the realtime logs (`homeassistant.log` in the configuration directory) and also the [Logbook](/components/logbook/). The realtime logs will show the rules being initialized (once for each trigger), example:
```plain
INFO [homeassistant.components.automation] Initialized rule Rainy Day
INFO [homeassistant.components.automation] Initialized rule Rainy Day
INFO [homeassistant.components.automation] Initialized rule Rainy Day
INFO [homeassistant.components.automation] Initialized rule Rain is over
```
The Logbook component will show a line entry when an automation is triggered. You can look at the previous entry to determine which trigger in the rule triggered the event.
![Logbook example](/images/components/automation/logbook.png)
[template]: /topics/templating/

View file

@ -9,7 +9,7 @@ sharing: true
footer: true
---
When all your devices are set up it's time to put the cherry on the pie: automation. Home Assistant offers [a few built-in automations](/components/#automation) but mainly you'll be using [the automation component](/components/automation/) to set up your own rules.
When all your devices are set up it's time to put the cherry on the pie: automation. Home Assistant offers [a few built-in automations](/components/#automation) but mainly you'll be using the automation component to set up your own rules.
### {% linkable_title The basics of automation %}
@ -37,104 +37,14 @@ Each device is represented in Home Assistant as an entity consisting of the foll
A service can be called to have Home Assistant perform an action. Turn on a light, run a script or enable a scene. Each service has a domain and a name. For example the service `light.turn_on` is capable of turning on any light device in your system. Services can be passed parameters to for example tell which device to turn on or what color to use.
## {% linkable_title Creating your first automation rule %}
Before we dive deeper into what every piece of automation _can_ do, let's look at a simple automation rule: **Turn on the lights when the sun sets**
In this example, we are defining a trigger to track the sunset and tell it to fire when the sun is setting. When this event is triggered, the service `light.turn_on` is called without any parameters. Because we specify no parameters, it will turn on all the lights.
```yaml
# Example configuration.yaml entry
automation:
alias: Turn on light when sun sets
trigger:
platform: sun
event: sunset
action:
service: light.turn_on
```
After a few days of running this automation rule you come to realize that this automation rule is not good enough. It was already dark when the lights went on and the one day you weren't home, the lights turned on anyway. Time for some tweaking. Let's add an offset to the sunset trigger and a condition to only turn on the lights if anyone is home.
```yaml
# Example configuration.yaml entry
automation:
alias: Turn on light when sun sets
trigger:
platform: sun
event: sunset
offset: "-01:00:00"
condition:
platform: state
entity_id: group.all_devices
state: home
action:
service: light.turn_on
```
Now you're happy and all is good. You start to like this automation business and buy some more lights, this time you put them in the bedroom. But what you now realize is that when the sun is setting, the lights in the bedroom are also being turned on! Time to tweak the automation to only turn on the living room lights.
The first thing you do is to look at the entities in the developer tools (second icon) in the app. You see the names of your lights and you write them down: `light.table_lamp`, `light.bedroom`, `light.ceiling`.
Instead of hard coding the entity IDs of the lights in the automation rule, we will set up a group. This will allow us to see the living room separate in the app and be able to address it from automation rules.
So we tweak the config to add the group and have the automation rule only turn on the group.
```yaml
# Example configuration.yaml entry
group:
living_room:
- light.table_lamp
- light.ceiling
automation:
alias: Turn on light when sun sets
trigger:
platform: sun
event: sunset
offset: "-01:00:00"
condition:
platform: state
entity_id: group.all_devices
state: home
action:
service: light.turn_on
entity_id: group.living_room
```
Christmas is coming along and you decide to buy a remote switch to control the Christmas lights from Home Assistant. You can't claim to live in the house of the future if you're still manually turning on your Christmas lights!
We hook the switch up to Home Assistant and grab the entity ID0 from the developer tools: `switch.christmas_lights`. We will update the group to include the switch and will change our action. We are no longer able to call `light.turn_on` because we also want to turn on a switch. This is where `homeassistant.turn_on` comes to the rescue. This service is capable of turning any entity on.
```yaml
# Example configuration.yaml entry
group:
living_room:
- light.table_lamp
- light.ceiling
- switch.christmas_lights
automation:
alias: Turn on light when sun sets
trigger:
platform: sun
event: sunset
offset: "-01:00:00"
condition:
platform: state
entity_id: group.all_devices
state: home
action:
service: homeassistant.turn_on
entity_id: group.living_room
```
### {% linkable_title Further reading %}
We went over the basics of creating a home automation rule. Now, go automate!
- Learn about the available [automation triggers](/components/automation/#triggers)
- Learn about the available [automation conditions](/components/automation/#conditions)
- Learn about the available [automation triggers](/getting-started/automation-trigger/)
- Learn about the available [automation conditions](/getting-started/automation-condition/)
- Learn about [scripts](/components/script/) to help you trigger multiple actions and delays
- Learn about [scenes](/components/scene/) to help you set many entities at once to your liking
- Setup a [notification platform](/components/#notifications) to sent yourself messages

View file

@ -50,6 +50,6 @@ Home Assistant will know the location of your device if you are using OwnTracks.
[mqtt-self]: /components/mqtt/#run-your-own
[mqtt-cloud]: /components/mqtt/#cloudmqtt
[zone]: /components/zone/
[trigger]: /components/automation/#zone-trigger
[condition]: /components/automation/#zone-condition
[trigger]: /getting-started/automation-trigger/#zone-trigger
[condition]: /getting-started/automation-condition/#zone-condition