Add note and tweak examples

A common question in the support channels involves the `UndefinedError` returned when an automation that references a trigger state object in the action is manually triggered. Hopefully this note will reduce those questions in the future. I also tweaked the example automations.
This commit is contained in:
Jay 2020-01-28 08:50:43 -06:00 committed by GitHub
parent 785f786956
commit 38d0ab195d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -34,27 +34,19 @@ automation 2:
automation 3:
trigger:
# Multiple Entities for which you want to perform the same action.
# Multiple entities for which you want to perform the same action.
- platform: state
entity_id:
- light.bedroom_closet
to: 'on'
# Trigger when someone leaves the closet light on for 10 minutes.
for: '00:10:00'
- platform: state
entity_id:
- light.kiddos_closet
to: 'on'
for: '00:10:00'
- platform: state
entity_id:
- light.linen_closet
to: 'on'
# Trigger when someone leaves one of those lights on for 10 minutes.
for: '00:10:00'
action:
- service: light.turn_off
data_template:
# Whichever entity triggers the automation we want to turn off THAT entity, not the others.
# Turn off whichever entity triggered the automation.
entity_id: "{{ trigger.entity_id }}"
```
{% endraw %}
@ -76,12 +68,18 @@ It is possible to use `data` and `data_template` concurrently but be aware that
## Trigger State Object
Knowing how to access the [state object](/docs/configuration/state_object/) of a trigger entity could be one of the more common questions. Here are a few ways for the [`state`](#state), [`numeric_state`](#numeric_state) and [`template`](#template) triggers:
Knowing how to access the [state object](/docs/configuration/state_object/) of a trigger entity can be useful in automations. Here are a few ways to access the [`state`](#state), [`numeric_state`](#numeric_state) and [`template`](#template) triggers:
* `trigger.from_state` will return the **previous** [state object](/docs/configuration/state_object/) of the entity.
* `trigger.to_state` will return the **new** [state object](/docs/configuration/state_object/) that triggered trigger.
* `states[trigger.to_state.domain][trigger.to_state.object_id]` will return the **current** [state object](/docs/configuration/state_object/) of the entity.
<div class='note'>
Be aware that if you reference a trigger state object in an automation action, attempting to test that automation by calling the `automation.trigger` service or by clicking EXECUTE in the More Info box for the automation will not work. This is because the trigger state object doesn't exist in those contexts. One way to test automations like these is to manually change the state of the trigger entity at Developer Tools > States.
</div>
## Available Trigger Data
The following tables show the available trigger data per platform.