Advanced automation documentation using templating.
Automations support templating in the same way as scripts do. In addition to the Home Assistant template extensions available to scripts, the trigger template variable is available.
Be aware that if you reference a trigger state object in templates of an automation' action or condition sections, 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 check that the templates work as expected by pasting them in Developer Tools > Template together with your trigger's definition like:
{%raw%}
{% set trigger={'to_state':{'state': 'heat'}} %}{% set option = trigger.to_state.state %}{{'on'if option == 'heat' else 'off' }}
{%endraw%}
Available Trigger Data
The following tables show the available trigger data per platform.
# Example configuration.yaml entriesautomation:trigger:platform:stateentity_id:device_tracker.paulusaction:service:notify.notifydata:message:> Paulus just changed from {{ trigger.from_state.state }}
to {{ trigger.to_state.state }}automation 2:trigger:platform:mqtttopic:/notify/+action:service:> notify.{{ trigger.topic.split('/')[-1] }}data:message:"{{ trigger.payload }}"automation 3:trigger:# Multiple entities for which you want to perform the same action.- platform:stateentity_id:- light.bedroom_closet- light.kiddos_closet- light.linen_closetto:"on"# Trigger when someone leaves one of those lights on for 10 minutes.for:"00:10:00"action:- service:light.turn_offtarget:# Turn off whichever entity triggered the automation.entity_id:"{{ trigger.entity_id }}"