模板风扇

模板 integration集成将 Home Assistant 与您的设备、服务等连接和集成。 [Learn more] 创建结合了多种集成的风扇,并提供对每个风扇 turn_onturn_offset_percentageset_preset_modeset_oscillatingset_direction 命令运行脚本或调用操作的能力。

配置

要在您的安装中启用模板风扇,请将以下内容添加到您的 configuration.yamlconfiguration.yaml 文件是 Home Assistant 的主要配置文件。它列出了要加载的集成及其特定配置。在某些情况下,需要直接在 configuration.yaml 文件中手动编辑配置。大多数集成可以在 UI 中配置。 [Learn more] 文件中:

# 示例 configuration.yaml 条目
fan:
  - platform: template
    fans:
      bedroom_fan:
        friendly_name: "卧室风扇"
        value_template: "{{ states('input_boolean.state') }}"
        percentage_template: "{{ states('input_number.percentage') }}"
        preset_mode_template: "{{ states('input_select.preset_mode') }}"
        oscillating_template: "{{ states('input_select.osc') }}"
        direction_template: "{{ states('input_select.direction') }}"
        turn_on:
          action: script.fan_on
        turn_off:
          action: script.fan_off
        set_percentage:
          action: script.fans_set_speed
          data:
            percentage: "{{ percentage }}"
        set_preset_mode:
          action: script.fans_set_preset_mode
          data:
            preset_mode: "{{ preset_mode }}"
        set_oscillating:
          action: script.fan_oscillating
          data:
            oscillating: "{{ oscillating }}"
        set_direction:
          action: script.fan_direction
          data:
            direction: "{{ direction }}"
        speed_count: 6
        preset_modes:
          - '自动'
          - '智能'
          - '呼啸'

Configuration Variables

fans map Required

您的风扇列表。

friendly_name string (Optional)

前端使用的名称。

unique_id string (Optional)

唯一标识此风扇的 ID。将此设置为唯一值,以便通过用户界面进行自定义。

value_template template Required

定义一个模板以获取风扇的状态。有效值: onoff

percentage_template template (Optional)

定义一个模板以获取风扇的速度百分比。

preset_mode_template template (Optional)

定义一个模板以获取风扇的预设模式。

oscillating_template template (Optional)

定义一个模板以获取风扇的摆动状态。有效值: truefalse

direction_template template (Optional)

定义一个模板以获取风扇的方向。有效值: forwardreverse

availability_template template (Optional, default: true)

定义一个模板以获取实体的 available 状态。如果模板渲染失败或返回 True"1""true""yes""on""enable" 或非零数字,则该实体将为 available。如果模板返回其他任何值,则该实体将为 unavailable。如果未配置,该实体将始终为 available。请注意,字符串比较不区分大小写;"TrUe""yEs" 是允许的。

turn_on action Required

定义风扇开启时执行的操作。

turn_off action Required

定义风扇关闭时执行的操作。

set_percentage action (Optional)

定义当风扇接收到速度百分比命令时执行的操作。

set_preset_mode action (Optional)

定义当风扇接收到预设命令时执行的操作。

set_oscillating action (Optional)

定义当风扇接收到摆动状态命令时执行的操作。

set_direction action (Optional)

定义当风扇接收到方向命令时执行的操作。

preset_modes string | list (Optional, default: [])

风扇能够使用的预设模式列表。这是一个任意的字符串列表,不得包含任何速度。

speed_count integer (Optional, default: 100)

风扇支持的速度数量。用于计算 fan.increase_speedfan.decrease_speed 操作的百分比步骤。

模板和操作变量

基于状态的模板实体在其模板和操作中具有特殊的模板变量 thisthis 变量帮助 自我引用 实体的状态和属性。

从速度转换为百分比

将三速风扇从旧的风扇实体模型转换时,可以使用以下百分比:

0 - 关闭 33 - 66 - 100 -

示例

辅助风扇

此示例使用 input_boolean 和 input_number 来模拟风扇,并且示例展示了 set_percentage 的多个操作。

fan:
  - platform: template
    fans:
      helper_fan:
        friendly_name: "辅助风扇"
        value_template: "{{ states('input_boolean.state') }}"
        turn_on:
          - action: input_boolean.turn_on
            target:
              entity_id: input_boolean.state
        turn_off:
          - action: input_boolean.turn_off
            target:
              entity_id: input_boolean.state
        percentage_template: >
          {{ states('input_number.percentage') if is_state('input_boolean.state', 'on') else 0 }}
        speed_count: 6
        set_percentage:
          - action: input_boolean.turn_{{ 'on' if percentage > 0 else 'off' }}
            target:
              entity_id: input_boolean.state
          - action: input_number.set_value
            target:
              entity_id: input_number.percentage
            data:
              value: "{{ percentage }}"

预设模式风扇

此示例使用现有风扇,仅采用百分比。它将百分比值扩展为可用的预设模式,而无需助手实体。

fan:
  - platform: template
    fans:
      preset_mode_fan:
        friendly_name: "预设模式风扇示例"
        value_template: "{{ states('fan.percentage_fan') }}"
        turn_on:
          - action: fan.turn_on
            target:
              entity_id: fan.percentage_fan
        turn_off:
          - action: fan.turn_off
            target:
              entity_id: fan.percentage_fan
        percentage_template: >
          {{ state_attr('fan.percentage_fan', 'percentage') }}
        speed_count: 3
        set_percentage:
          - action: fan.set_percentage
            target:
              entity_id: fan.percentage_fan
            data:
              percentage: "{{ percentage }}"
        preset_modes:
          - "关闭"
          - "低"
          - "中"
          - "高"
        preset_mode_template: >
          {% if is_state('fan.percentage_fan', 'on') %}
            {% if state_attr('fan.percentage_fan', 'percentage') == 100  %}
              
            {% elif state_attr('fan.percentage_fan', 'percentage') == 66 %}
              
            {% else %}
              
            {% endif %}
          {% else %}
            关闭
          {% endif %}
        set_preset_mode:
          - action: fan.set_percentage
            target:
              entity_id: fan.percentage_fan
            data:
              percentage: >-
                {% if preset_mode == '高' %}
                  100
                {% elif preset_mode == '中' %}
                  66
                {% elif preset_mode == '低' %}
                  33
                {% else %}
                  0
                {% endif %}