模板风扇
模板 integration集成将 Home Assistant 与您的设备、服务等连接和集成。 [Learn more] 创建结合了多种集成的风扇,并提供对每个风扇 turn_on
、turn_off
、set_percentage
、set_preset_mode
、set_oscillating
和 set_direction
命令运行脚本或调用操作的能力。
配置
要在您的安装中启用模板风扇,请将以下内容添加到您的 configuration.yaml
configuration.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
您的风扇列表。
定义一个模板以获取风扇的状态。有效值: on
、off
定义一个模板以获取风扇的速度百分比。
定义一个模板以获取风扇的预设模式。
定义一个模板以获取风扇的摆动状态。有效值: true
、false
定义一个模板以获取风扇的方向。有效值: forward
、reverse
定义一个模板以获取实体的 available
状态。如果模板渲染失败或返回 True
、"1"
、"true"
、"yes"
、"on"
、"enable"
或非零数字,则该实体将为 available
。如果模板返回其他任何值,则该实体将为 unavailable
。如果未配置,该实体将始终为 available
。请注意,字符串比较不区分大小写;"TrUe"
和 "yEs"
是允许的。
定义风扇开启时执行的操作。
定义风扇关闭时执行的操作。
定义当风扇接收到速度百分比命令时执行的操作。
定义当风扇接收到预设命令时执行的操作。
定义当风扇接收到摆动状态命令时执行的操作。
定义当风扇接收到方向命令时执行的操作。
模板和操作变量
基于状态的模板实体在其模板和操作中具有特殊的模板变量 this
。this
变量帮助 自我引用 实体的状态和属性。
从速度转换为百分比
将三速风扇从旧的风扇实体模型转换时,可以使用以下百分比:
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 %}