Homeassisant_实现桌面风扇循环控制

配置文件

在configuration.yaml配置文件添加下面代码

input_select:
  fan_speed_selector:   # 创建一个输入选择器实体,实体ID为 fan_speed_selector
    name: 小吊扇档位    # 显示名称为“小吊扇档位”,会在前端界面上展示
    options:            # 定义可供选择的选项列表
      - 关              # 第一个选项:关(关闭风扇)
      - 一档            # 第二个选项:一档(低速)
      - 二档            # 第三个选项:二档(中速)
      - 三档            # 第四个选项:三档(高速)
    initial: 关         # 设置初始值为“关”,即默认选择为关闭状态

添加自动化

编辑yaml文件

image

alias: 小吊扇档位控制  # 自动化的名称,用于在界面中显示和识别
# 触发条件,当 input_select.fan_speed_selector(风扇档位选择器)状态改变时触发
triggers:
  - entity_id: input_select.fan_speed_selector
    trigger: state  # 状态变化触发器
# 执行动作,根据当前档位进行条件判断,并执行不同的控制命令
actions:
  - choose:
      # 档位为“关”时:打开开关
      - conditions:
          - condition: state
            entity_id: input_select.fan_speed_selector
            state: 关
        sequence:
          - target:
              entity_id: switch.c85ccca20cf8_switch  # 实际控制的小吊扇继电器开关
            action: switch.turn_on  # 执行开关打开动作
            data: {}
      # 档位为“一档”时:关闭开关
      - conditions:
          - condition: state
            entity_id: input_select.fan_speed_selector
            state: 一档
        sequence:
          - target:
              entity_id: switch.c85ccca20cf8_switch
            action: switch.turn_off  # 执行开关关闭动作
            data: {}
      # 档位为“二档”时:打开开关
      - conditions:
          - condition: state
            entity_id: input_select.fan_speed_selector
            state: 二档
        sequence:
          - target:
              entity_id: switch.c85ccca20cf8_switch
            action: switch.turn_on
            data: {}
      # 档位为“三档”时:关闭开关
      - conditions:
          - condition: state
            entity_id: input_select.fan_speed_selector
            state: 三档
        sequence:
          - target:
              entity_id: switch.c85ccca20cf8_switch
            action: switch.turn_off
            data: {}
# 执行模式为 single:同一时间只允许一个实例运行,新的触发会被忽略
mode: single

配置UI文件

图片

# 显示风扇信息标题
          info: '************************    桌面风扇扇    ************************'
# 添加一个图片元素(未指定内容,可能是占位或遗漏)
          - type: image
# 第二个图片元素,用于显示风扇状态图标,并可点击切换档位
          - type: image
            entity: input_select.fan_speed_selector  # 绑定风扇档位选择器
            tap_action:
              action: call-service                  # 点击时触发服务
              service: input_select.select_next     # 服务为:切换到下一个选项
              service_data:
                entity_id: input_select.fan_speed_selector  # 指定实体
            style:
              top: 1000px    # 设置图标在界面中的垂直位置
              left: 500px    # 设置图标在界面中的水平位置
              width: 50px    # 设置图标宽度
            state_image:     # 根据不同状态显示不同的图片
              关: /local/pc_ui2/小吊扇off.svg        # 风扇关闭时显示的图标
              一档: /local/pc_ui2/小吊扇on.svg       # 一档时显示的图标
              二档: /local/pc_ui2/小吊扇on2.svg      # 二档时显示的图标
              三档: /local/pc_ui2/小吊扇on3.svg      # 三档时显示的图标
# 自定义按钮卡,显示风扇名称和当前状态
          - type: custom:button-card
            entity: input_select.fan_speed_selector  # 绑定风扇档位选择器
            name: 小吊扇                # 按钮上显示的名称
            show_state: true           # 显示当前状态(如“一档”、“二档”等)
            show_icon: false           # 不显示图标,仅显示文本
            style:
              top: 1050px              # 设置按钮在界面中的垂直位置
              left: 500px              # 设置按钮在界面中的水平位置
              width: 100px             # 设置按钮宽度
            styles:
              card:
                - font-size: 15px      # 设置字体大小为15px

功能总结:

图片切换功能:点击风扇图标会依次切换“关 → 一档 → 二档 → 三档 → 关”。
状态同步:图标和按钮显示实时档位状态,图标随档位变化而变换图片。
交互直观:适用于平板或触摸屏操作的智能家居控制面板。


Homeassisant_实现桌面风扇循环控制
https://halo.tolan.link:6688//archives/1749176721091
发布于
2025年06月06日
许可协议