场景
您可以创建场景,以捕捉您希望某些实体具备的状态。例如,场景可以指定灯 A 应该打开,而灯 B 应该是亮红色。
# 示例 configuration.yaml 条目
scene:
- name: 浪漫
entities:
light.tv_back_light: "on"
light.ceiling:
state: "on"
xy_color: [0.33, 0.66]
brightness: 200
- name: 电影
entities:
light.tv_back_light:
state: "on"
brightness: 125
light.ceiling: off
media_player.sony_bravia_tv:
state: "on"
source: HDMI 1
如何配置场景
在您定义的 YAMLYAML 是一种人类可读的数据序列化语言。它用于以结构化格式存储和传输数据。在 Home Assistant 中,YAML 用于配置,例如在 configuration.yaml
或 automations.yaml
文件中。 [Learn more] 文件中的场景中,请确保使用
下面列出的所有必需参数。
如您所见,有两种方法可以定义每个 entity_id
的状态:
- 直接与实体定义
state
。请注意,state
需要被定义。 - 使用其属性定义复杂状态。您可以在
developer-tools -> state
下查看特定实体的所有可用属性。
可以通过操作 scene.turn_on
触发场景(没有 ‘scene.turn_off’ 操作)。
# 示例自动化
automation:
triggers:
- trigger: state
entity_id: device_tracker.sweetheart
from: "not_home"
to: "home"
actions:
- action: scene.turn_on
target:
entity_id: scene.romantic
在未定义场景的情况下应用场景
通过 scene.apply
操作,您可以在未首先通过配置定义场景的情况下应用场景。相反,您将状态作为数据的一部分传递。数据的格式与配置中的 entities
字段相同。
# 示例自动化
automation:
triggers:
- trigger: state
entity_id: device_tracker.sweetheart
from: "not_home"
to: "home"
actions:
- action: scene.apply
data:
entities:
light.tv_back_light:
state: "on"
brightness: 100
light.ceiling: off
media_player.sony_bravia_tv:
state: "on"
source: "HDMI 1"
使用场景过渡
scene.apply
和 scene.turn_on
操作都支持设置过渡,
这使您能够平滑过渡到场景。
这是一个设置浪漫场景的自动化示例,在该场景中, 灯光将在 2.5 秒内过渡到该场景。
# 示例自动化
automation:
triggers:
- trigger: state
entity_id: device_tracker.sweetheart
from: "not_home"
to: "home"
actions:
- action: scene.turn_on
target:
entity_id: scene.romantic
data:
transition: 2.5
过渡目前仅支持灯光,而灯光本身也必须支持此功能。但是,场景本身不必仅由灯光组成即可设置过渡。
重新加载场景
每当您更改场景配置时,可以调用 scene.reload
操作以重新加载场景。