在 configuration.yaml 中设置自定义句子

意图和句子可以添加到你的 configuration.yaml 文件中的 conversation 配置中:

# 示例 configuration.yaml
conversation:
  intents:
    HassTurnOn:
      - "activate [the] {name}"

这扩展了 HassTurnOn 意图的默认英文句子,允许你说 “activate the kitchen lights” 以及 “turn on the kitchen lights”。

新的意图也可以添加,并通过 intent_script 集成定义其响应和操作:

# 示例 configuration.yaml
conversation:
  intents:
    YearOfVoice:
      - "how is the year of voice going"
      
intent_script:
  YearOfVoice:
    speech:
      text: "非常好!我们已经支持超过 40 种语言,还有更多。"

除了文本响应外,intent_script 可以触发 Home Assistant 中可用的任何 action,例如调用服务或触发事件。

在配置目录中设置句子

可以在 Home Assistant 的 config 目录中进行更高级的自定义。例如,当请求英文句子(语言代码 en)时,将加载 config/custom_sentences/en 中的 YAML 文件。

以下示例创建了一个新的 SetVolume 意图,用于改变两个媒体播放器中的一个的音量:

# 示例 config/custom_sentences/en/media.yaml
language: "en"
intents:
  SetVolume:
    data:
      - sentences:
          - "(set|change) {media_player} volume to {volume} [percent]"
          - "(set|change) [the] volume for {media_player} to {volume} [percent]"
lists:
  media_player:
    values:
      - in: "living room"
        out: "media_player.living_room"
      - in: "bedroom"
        out: "media_player.bedroom"
  volume:
    range:
      from: 0
      to: 100

如上所述,然后可以使用 intent_script 集成来实现 SetVolume 的操作并提供响应:

# 示例 configuration.yaml
intent_script:
  SetVolume:
    action:
      service: "media_player.volume_set"
      data:
        entity_id: "{{ media_player }}"
        volume_level: "{{ volume / 100.0 }}"
    speech:
      text: "音量已更改为 {{ volume }}"

自定义响应

现有意图的响应也可以在 config/custom_sentences/<language> 中自定义:

# 示例 config/custom_sentences/en/responses.yaml
language: "en"
responses:
  intents:
    HassTurnOn:
      default: "我已经打开了 {{ slots.name }}"

相关设备和安装教程