找回密码
 立即注册
楼主: bugensui

[技术探讨] ha中怎么在输入框中输入几小时后关闭空调

[复制链接]

8

主题

372

回帖

4225

积分

论坛元老

积分
4225
金钱
3845
HASS币
10
发表于 3 天前 | 显示全部楼层

自带的计时器就可以

0011.jpg


0022.jpg

homeassistant:
  customize:
#    timer.timer_task:
#      friendly_name: 延迟倒计时
#    input_text.timer_task_time:
#      friendly_name: 设置延迟时间
#      icon: mdi:timer-cog-outline
    # script.timer_task_cancel:
    #   friendly_name: 取消倒计时
    #   icon: mdi:timer-off
    automation.timer_task:
      friendly_name: 延迟任务自动化
      icon: mdi:av-timer

timer:
  timer_task:
    name: 延迟任务计时器
    duration: '00:00:00'
    restore: true
#    icon: 

input_text:
  timer_task_time:
    name: 设置延迟任务时间
    initial: 00:00
    pattern: '([01][0-9]|2[0-3]):[0-5][0-9]'
    icon: mdi:timer-cog-outline

input_boolean:
  timer_task_action:
    name: 延时任务执行操作
    icon: mdi:power

input_select:
  timer_task_device:
    name: 延时任务执行设备
    options:
      - = = = =
      - 风扇
      - 空调
      - 电热毯
      - 电蚊香
    initial: '= = = ='
    icon: mdi:form-select

# script:              # 此处取消是由前端卡片中的 互动-点击行为-执行动作实现
#   timer_task_cancel:
#     sequence:
#       - action: timer.cancel             ## 取消倒计时
#         entity_id: timer.timer_task
# #      - delay: "00:00:01"
#       - action: input_text.set_value     ## 时间框恢复默认值
#         target:
#           entity_id: input_text.timer_task_time
#         data:
#           value: "00:00"
#       - action: input_select.select_option     ## 设备选择框恢复默认值
#         target:
#           entity_id: input_select.timer_task_device
#         data:
#           option: "= = = ="

automation:
  - alias: "timer_task"
    description: "倒计时开关选定设备的自动化"
    mode: single
    triggers:
      - event_type: timer.finished                         # 此处不可使用 timer.started 否则会立即关闭设备
        event_data:                                        # 文档:https://www.home-assistant.io/integrations/timer/#events
          entity_id: timer.timer_task
        trigger: event
        id: 计时器执行触发
        alias: 计时器执行触发
      - entity_id: input_text.timer_task_time
        trigger: state
        id: 设置时间后触发
        alias: 设置时间后触发
      - trigger: template
        value_template: "{{ states('input_select.timer_task_device') != "= = = =" }}"
        id: 选择设备后触发
        alias: 选择设备后触发
      - trigger: state
        entity_id:
          - timer.timer_task
        to: idle
        id: 计时器结束触发
        from: active
        for:
          hours: 0
          minutes: 0
          seconds: 1

    conditions: []

    actions:
      - choose:
          # 分支1:启动计时器
          - conditions:
              - condition: trigger
                id: 
                  - 选择设备后触发
                  - 设置时间后触发
              - condition: template
                value_template: "{{ states('input_text.timer_task_time') != '00:00' }}"
                alias: 时间输入框不等于"00:00"
              - condition: template
                value_template: "{{ states('input_select.timer_task_device') != '= = = =' }}"
                alias: 设备选择框不等于"= = = ="
            sequence:
              - service: timer.start
                data_template:
                  entity_id: timer.timer_task
                  duration: "{{ states('input_text.timer_task_time') }}"
          # 分支2:计时器执行触发——设备控制(开/关)
          - conditions:
              - condition: trigger
                id: 计时器执行触发
              - condition: template
                value_template: "{{ states('input_select.timer_task_device') != '= = = =' }}"
                alias: 设备选择框不等于"= = = ="
            sequence:
              - variables:
                  device: "{{ states('input_select.timer_task_device') }}"
                  action_state: "{{ states('input_boolean.timer_task_action') }}"
                  service: >
                    {% if device == '风扇' %}
                      button.press
                    {% elif device == '空调' %}
                      climate.{{ 'turn_on' if action_state == 'on' else 'turn_off' }}
                    {% elif device in ['电热毯','电蚊香'] %}
                      switch.{{ 'turn_on' if action_state == 'on' else 'turn_off' }}
                    {% endif %}
                  target_entity: >
                    {% if device == '风扇' %}
                      button.gree_fan_1_dian_yuan
                    {% elif device == '空调' %}
                      climate.lumi_mcn02_e418_air_conditioner
                    {% elif device == '电热毯' %}
                      switch.broadlink1_s2
                    {% elif device == '电蚊香' %}
                      switch.broadlink1_s1
                    {% endif %}
              - service: "{{ service }}"
                target:
                  entity_id: "{{ target_entity }}"
          # 分支3:计时器结束触发——恢复默认值
          - conditions:
              - condition: trigger
                id: 计时器结束触发
            sequence:
              - service: input_select.select_option
                target:
                  entity_id: input_select.timer_task_device
                data:
                  option: "= = = ="
                alias: 执行选择框恢复默认值
              # - service: input_text.set_value
              #   target:
              #     entity_id: input_text.timer_task_time
              #   data:
              #     value: "00:00"
              #   alias: 时间框恢复默认值


回复

使用道具 举报

174

主题

2608

回帖

8736

积分

元老级技术达人

积分
8736
金钱
5944
HASS币
50
 楼主| 发表于 3 天前 | 显示全部楼层
s2233 发表于 2025-5-31 16:16
自带的计时器就可以

这个很清爽,不错
回复

使用道具 举报

2

主题

26

回帖

209

积分

中级会员

积分
209
金钱
181
HASS币
0
QQ
发表于 昨天 17:45 | 显示全部楼层
bugensui 发表于 2025-5-29 15:19
我想判断滑动的值是不是等于0或者说大于0,在值模板里怎么判断,你会不?

我这里按你这样写判断没有问题,只是稍微规范了一下语法
{{ value | round == 0 }}
回复

使用道具 举报

174

主题

2608

回帖

8736

积分

元老级技术达人

积分
8736
金钱
5944
HASS币
50
 楼主| 发表于 昨天 17:56 | 显示全部楼层
s763279737 发表于 2025-6-2 17:45
我这里按你这样写判断没有问题,只是稍微规范了一下语法

奇怪,我在辅助元素里,这样判断不行,真奇怪
回复

使用道具 举报

0

主题

98

回帖

908

积分

高级会员

积分
908
金钱
810
HASS币
0
发表于 6 小时前 | 显示全部楼层
bugensui 发表于 2025-5-30 14:39
延时靠谱不,比如延时4个小时关闭空调,晚上睡觉关空调的场景,假如晚上ha系统更新了重启后,或者我手动 ...

延迟是通过控制脚本来实现的,无论是延时几个小时,几分钟还是几秒都行,还可以直接指定时间关闭(比如说明天早上6点关闭空调),但是我没做周期处理。延时任务我做在了内存中,重启就没了,要固化其实可以写入文件来实现。目前的问题就是这个任务可以配置到卡片中显示,但是还没做到可以从卡片中取消。
回复

使用道具 举报

174

主题

2608

回帖

8736

积分

元老级技术达人

积分
8736
金钱
5944
HASS币
50
 楼主| 发表于 6 小时前 | 显示全部楼层
本帖最后由 bugensui 于 2025-6-3 16:27 编辑
dahai2626 发表于 2025-6-3 16:14
延迟是通过控制脚本来实现的,无论是延时几个小时,几分钟还是几秒都行,还可以直接指定时间关闭(比如说 ...

我搞定了,拖动滑块到指定的数字,就可以在几个小时后自动化关闭空调,我没有用延时策略,我感觉不可靠,因为都是几个小时以上,万一中间重启ha,策略就失效了。我是计算出延时后的到期时间,储存在辅助元素的文本里,然后新建1个自动化,触发条件是时间,当前时间的文本,等于该文本,就触发关闭空调。如果滑块滑到0.就关闭空调,并且重置之前的时间文本为null,滑块最大值是12,步长为1,就是最长只能定时12小时,


1.png
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Hassbian ( 晋ICP备17001384号-1 )

GMT+8, 2025-6-3 22:26 , Processed in 0.184413 second(s), 24 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表