跳到主要内容

自动化触发器变量

自动化支持与脚本相同方式的模板。除了脚本可用的Home Assistant模板扩展外,还提供了triggerthis模板变量。

在评估配置中声明的任何trigger_variables时,模板变量this也可用。

可用的this数据

变量this是触发动作时自动化的状态对象。状态对象还包含上下文数据,可用于识别导致脚本或自动化执行的用户。请注意,在执行动作时,this不会改变。

可用的触发器数据

变量trigger是一个包含有关哪个触发器触发了自动化的详细信息的对象。

模板可以使用数据来修改自动化执行的动作或显示在消息中。例如,您可以创建一个多个传感器可以触发的自动化,然后使用传感器的位置来指定要激活的灯光;或者您可以发送一个包含触发它的传感器友好名称的通知。

每个触发器平台可以包含特定于该平台的额外数据。

全部通用

所有平台的触发器都将包含以下数据。

模板变量数据
trigger.alias触发器的别名。
trigger.id触发器的ID
trigger.idx触发器的索引。(第一个触发器idx是0。)

日历

这些是日历触发器可用的属性。

模板变量数据
trigger.platform硬编码:calendar
trigger.event触发事件类型,可以是startend
trigger.calendar_event匹配的日历事件对象。
trigger.calendar_event.summary日历事件的标题或摘要。
trigger.calendar_event.start日历事件的开始日期或日期时间的字符串表示,例如2022-04-102022-04-10 11:30:00-07:00
trigger.calendar_event.end日历事件在UTC中的结束时间或日期时间的字符串表示,例如2022-04-112022-04-10 11:45:00-07:00
trigger.calendar_event.all_day表示事件跨越整天。
trigger.calendar_event.description日历事件的详细描述(如果有)。
trigger.calendar_event.location日历事件的位置信息(如果有)。
trigger.offset与事件的时间差对象(如果有)

设备

这些是设备触发器可用的属性。

继承基于为设备选择的触发器类型的事件状态模板中的模板变量。

模板变量数据
trigger.platform硬编码:device

事件

这些是事件触发器可用的属性。

模板变量数据
trigger.platform硬编码:event
trigger.event匹配的事件对象。
trigger.event.event_type事件类型。
trigger.event.data可选的事件数据。

MQTT

这些是MQTT触发器可用的属性。

模板变量数据
trigger.platform硬编码:mqtt
trigger.topic接收负载的主题。
trigger.payload负载。
trigger.payload_jsonJSON解析负载的字典。
trigger.qos负载的QOS。

数值状态

这些是数值状态触发器可用的属性。

模板变量数据
trigger.platform硬编码:numeric_state
trigger.entity_id我们观察的实体ID。
trigger.below低于阈值(如果有)。
trigger.above高于阈值(如果有)。
trigger.from_state实体的前一个状态对象
trigger.to_state触发触发器的新状态对象
trigger.for状态满足高于/低于条件的时间长度(如果有)。

句子

这些是句子触发器可用的属性。

模板变量数据
trigger.platform硬编码:conversation
trigger.sentence匹配的句子文本
trigger.slots带有匹配槽值的对象
trigger.details按名称匹配的槽详细信息对象,如通配符。每个详细信息包含:
name - 槽的名称
text - 匹配的文本
value - 输出值(参见列表
trigger.device_id捕获命令的设备ID(如果有)。

状态

这些是状态触发器可用的属性。

模板变量数据
trigger.platform硬编码:state
trigger.entity_id我们观察的实体ID。
trigger.from_state实体的前一个状态对象
trigger.to_state触发触发器的新状态对象
trigger.for状态已处于该状态的时间长度(如果有)。

太阳

这些是太阳触发器可用的属性。

模板变量数据
trigger.platform硬编码:sun
trigger.event刚刚发生的事件:sunsetsunrise
trigger.offset与事件的时间差对象(如果有)。

模板

这些是模板触发器可用的属性。

模板变量数据
trigger.platform硬编码:template
trigger.entity_id导致变化的实体ID。
trigger.from_state导致变化的实体的前一个状态对象
trigger.to_state导致模板变化的实体的新状态对象
trigger.for状态已处于该状态的时间长度(如果有)。

时间

这些是时间触发器可用的属性。

模板变量数据
trigger.platform硬编码:time
trigger.now触发时间触发器的DateTime对象。

时间模式

这些是时间模式触发器可用的属性。

模板变量数据
trigger.platform硬编码:time_pattern
trigger.now触发time_pattern触发器的DateTime对象。

持久通知

这些是持久通知触发器可用的属性。

模板变量数据
trigger.platform硬编码:persistent_notification
trigger.update_type持久通知更新类型addedremovedcurrentupdated
trigger.notification触发持久通知触发器的通知对象。
trigger.notification.notification_id通知ID
trigger.notification.title通知的标题
trigger.notification.message通知的消息
trigger.notification.created_at指示通知创建时间的DateTime对象。

Webhook

这些是Webhook触发器可用的属性。

模板变量数据
trigger.platform硬编码:webhook
trigger.webhook_id被触发的webhook ID。
trigger.json请求的JSON数据(如果它有JSON内容类型)作为映射。
trigger.data请求的表单数据(如果它有表单数据内容类型)。
trigger.query请求的URL查询参数(如果提供)。

区域

这些是区域触发器可用的属性。

模板变量数据
trigger.platform硬编码:zone
trigger.entity_id我们观察的实体ID。
trigger.from_state实体的前一个状态对象
trigger.to_state实体的新状态对象
trigger.zone区域的状态对象
trigger.event触发器观察到的事件:enterleave

示例

# configuration.yaml条目示例
automation:
triggers:
- trigger: state
entity_id: device_tracker.paulus
id: paulus_device
actions:
- action: notify.notify
data:
message: >
Paulus刚刚从{{ trigger.from_state.state }}
变成了{{ trigger.to_state.state }}

这是由{{ trigger.id }}触发的

automation 2:
triggers:
- trigger: mqtt
topic: "/notify/+"
actions:
- action: >
notify.{{ trigger.topic.split('/')[-1] }}
data:
message: "{{ trigger.payload }}"

automation 3:
triggers:
# 您想对其执行相同操作的多个实体。
- trigger: state
entity_id:
- light.bedroom_closet
- light.kiddos_closet
- light.linen_closet
to: "on"
# 当有人将其中一盏灯开着10分钟时触发。
for: "00:10:00"
actions:
- action: light.turn_off
target:
# 关闭触发自动化的实体。
entity_id: "{{ trigger.entity_id }}"

automation 4:
triggers:
# 当Home Assistant扫描NFC标签时...
- trigger: event
event_type: tag_scanned
# ...由特定人员
context:
user_id:
- 06cbf6deafc54cf0b2ffa49552a396ba
- 2df8a2a6e0be4d5d962aad2d39ed4c9c
conditions:
# 检查NFC标签(ID)是前门旁边的那个
- condition: template
value_template: "{{ trigger.event.data.tag_id == '8b6d6755-b4d5-4c23-818b-cf224d221ab7'}}"
actions:
# 关闭各种灯光
- action: light.turn_off
target:
entity_id:
- light.kitchen
- light.bedroom
- light.living_room