『瀚思彼岸』» 智能家居技术论坛

 找回密码
 立即注册
123
返回列表 发新帖
楼主: yylwhy

[技术探讨] 一个有关暖气(燃气炉)自动化控制的讨论

[复制链接]

17

主题

798

帖子

5728

积分

论坛元老

Rank: 8Rank: 8

积分
5728
金钱
4925
HASS币
0
发表于 2021-8-14 09:21:26 | 显示全部楼层
XCray 发表于 2021-8-12 19:21
建议还是用ha自带的“Generic Thermostat”组件,然后用自动化控制这个虚拟温控器的目标温度和开关,远比自 ...

嗯,理论上用通用组件会容易一些。
当时撸完了也考虑过要不要改过去,不过看了下,我这边实际大部分代码是处理界面的定时器,换了能改进的也不多,也就这样了。
实际用了一冬天,没碰到啥问题,就这么用啦~~~

硬件温控器其实也有,以前试过改下DNS解析撸个服务端可以用。不过现在HA实现了之后,基本就没啥用了。。
回复

使用道具 举报

17

主题

798

帖子

5728

积分

论坛元老

Rank: 8Rank: 8

积分
5728
金钱
4925
HASS币
0
发表于 2021-8-14 09:26:18 | 显示全部楼层
loveyolanda 发表于 2021-8-12 16:55
大佬,请问这这两个代码怎么用?我把控制代码添加到自动化后,再把ui添加到lovelace,但是全是黄色的感叹 ...

所有Input_number,input_boolean之类的,都需要单独定义的不过就像楼上说的,如果你打算自己弄一份,最好用通用温控器,多少省事一些

input_boolean:
  heating_boolean_on_off_1:
    name: 温控开关
    # initial: true
  heating_boolean_manual_1:
    name: 手动模式
    initial: false
  #####################################
  # 工作日/休息日
  #####################################
  heating_boolean_group_1_week_1:
    name: 周一
  heating_boolean_group_1_week_2:
    name: 周二
  heating_boolean_group_1_week_3:
    name: 周三
  heating_boolean_group_1_week_4:
    name: 周四
  heating_boolean_group_1_week_5:
    name: 周五
  heating_boolean_group_1_week_6:
    name: 周六
  heating_boolean_group_1_week_7:
    name: 周日
  heating_boolean_group_2_week_1:
    name: 周一
  heating_boolean_group_2_week_2:
    name: 周二
  heating_boolean_group_2_week_3:
    name: 周三
  heating_boolean_group_2_week_4:
    name: 周四
  heating_boolean_group_2_week_5:
    name: 周五
  heating_boolean_group_2_week_6:
    name: 周六
  heating_boolean_group_2_week_7:
    name: 周日
  heating_boolean_var_init_1:
    name: 温控器控制变量1
    initial: false
  heating_boolean_var_init_2:
    name: 温控器控制变量2
    initial: false
  #####################################
  # 模式2
  #####################################
  heating_boolean_mode2_group_1_switch_1:
    name: 时间段启用1
  heating_boolean_mode2_group_1_switch_2:
    name: 时间段启用2
  heating_boolean_mode2_group_1_switch_3:
    name: 时间段启用3
  heating_boolean_mode2_group_1_switch_4:
    name: 时间段启用4
  heating_boolean_mode2_group_1_switch_5:
    name: 时间段启用5
  heating_boolean_mode2_group_2_switch_1:
    name: 时间段启用1
  heating_boolean_mode2_group_2_switch_2:
    name: 时间段启用2
  heating_boolean_mode2_group_2_switch_3:
    name: 时间段启用3
  heating_boolean_mode2_group_2_switch_4:
    name: 时间段启用4
  heating_boolean_mode2_group_2_switch_5:
    name: 时间段启用5
  #####################################
  # 定时启动
  #####################################
  heating_boolean_timer_exec_switch_1:
    name: 定时启动1
  heating_boolean_timer_exec_switch_2:
    name: 定时启动2
  heating_boolean_timer_exec_switch_3:
    name: 定时启动3
  heating_boolean_timer_exec_switch_4:
    name: 定时启动4

input_number:
  #####################################
  # 手动模式
  #####################################
  heating_number_temp_manual_1:
    name: 手动温度
    # initial: 24
    min: 0
    max: 30
    step: 0.5
    unit_of_measurement: ℃
  heating_number_save_temp_manual:
    name: 保存手动温度用
    min: 0
    max: 99
    step: 0.5
  heating_number_open_duration:
    name: 手动加热时间
    min: 0
    max: 120
    step: 1
    unit_of_measurement: 分
  #####################################
  # 温度(模式1)
  #####################################
  heating_number_temp_high_1:
    name: 舒适温度(默认值)
    initial: 21
    min: 15
    max: 25
    step: 0.5
    unit_of_measurement: ℃
  heating_number_temp_low_1:
    name: 节能温度(默认值)
    initial: 19
    min: 15
    max: 25
    step: 0.5
    unit_of_measurement: ℃
  heating_number_group_1_temp_high_1:
    name: 舒适温度
    min: 15
    max: 25
    step: 0.5
    unit_of_measurement: ℃
  heating_number_group_1_temp_low_1:
    name: 节能温度
    min: 15
    max: 25
    step: 0.5
    unit_of_measurement: ℃
  heating_number_group_2_temp_high_1:
    name: 舒适温度
    min: 15
    max: 25
    step: 0.5
    unit_of_measurement: ℃
  heating_number_group_2_temp_low_1:
    name: 节能温度
    min: 15
    max: 25
    step: 0.5
    unit_of_measurement: ℃
  #####################################
  # 温度(模式2)
  #####################################
  heating_number_mode2_group_1_temp_1:
    name: 工作日温度1
    # initial: 21
    min: 15
    max: 30
    step: 0.5
    unit_of_measurement: ℃
  heating_number_mode2_group_1_temp_2:
    name: 工作日温度2
    min: 15
    max: 30
    step: 0.5
    unit_of_measurement: ℃
  heating_number_mode2_group_1_temp_3:
    name: 工作日温度3
    min: 15
    max: 30
    step: 0.5
    unit_of_measurement: ℃
  heating_number_mode2_group_1_temp_4:
    name: 工作日温度4
    min: 15
    max: 30
    step: 0.5
    unit_of_measurement: ℃
  heating_number_mode2_group_1_temp_5:
    name: 工作日温度5
    min: 15
    max: 30
    step: 0.5
    unit_of_measurement: ℃
  heating_number_mode2_group_2_temp_1:
    name: 休息日温度1
    min: 15
    max: 30
    step: 0.5
    unit_of_measurement: ℃
  heating_number_mode2_group_2_temp_2:
    name: 休息日温度2
    min: 15
    max: 30
    step: 0.5
    unit_of_measurement: ℃
  heating_number_mode2_group_2_temp_3:
    name: 休息日温度3
    min: 15
    max: 30
    step: 0.5
    unit_of_measurement: ℃
  heating_number_mode2_group_2_temp_4:
    name: 休息日温度4
    min: 15
    max: 30
    step: 0.5
    unit_of_measurement: ℃
  heating_number_mode2_group_2_temp_5:
    name: 休息日温度5
    min: 15
    max: 30
    step: 0.5
    unit_of_measurement: ℃
  #####################################
  # 定时启动
  #####################################
  heating_number_timer_exec_open_duration_1:
    name: 定时启动持续时间1
    min: 0
    max: 120
    step: 1
    unit_of_measurement: 分
  heating_number_timer_exec_open_duration_2:
    name: 定时启动持续时间2
    min: 0
    max: 120
    step: 1
    unit_of_measurement: 分
  heating_number_timer_exec_open_duration_3:
    name: 定时启动持续时间3
    min: 0
    max: 120
    step: 1
    unit_of_measurement: 分
  heating_number_timer_exec_open_duration_4:
    name: 定时启动持续时间4
    min: 0
    max: 120
    step: 1
    unit_of_measurement: 分

input_datetime:
  #####################################
  # 时间段(模式1)
  #####################################
  heating_time_start_1:
    name: 工作日舒适温度开始(默认值)
    has_date: false
    has_time: true
    initial: "05:00"
  heating_time_end_1:
    name: 工作日舒适温度结束(默认值)
    has_date: false
    has_time: true
    initial: "22:00"
  heating_time_start_6:
    name: 周末舒适温度开始(默认值)
    has_date: false
    has_time: true
    initial: "06:00"
  heating_time_end_6:
    name: 周末舒适温度结束(默认值)
    has_date: false
    has_time: true
    initial: "22:00"
  heating_time_group_1_start_1:
    name: 舒适温度开始
    has_date: false
    has_time: true
  heating_time_group_1_end_1:
    name: 舒适温度结束
    has_date: false
    has_time: true
  heating_time_group_1_start_2:
    name: 舒适温度开始(时间段2)
    has_date: false
    has_time: true
  heating_time_group_1_end_2:
    name: 舒适温度结束(时间段2)
    has_date: false
    has_time: true
  heating_time_group_1_start_3:
    name: 舒适温度开始(时间段3)
    has_date: false
    has_time: true
  heating_time_group_1_end_3:
    name: 舒适温度结束(时间段3)
    has_date: false
    has_time: true
  heating_time_group_2_start_1:
    name: 舒适温度开始
    has_date: false
    has_time: true
  heating_time_group_2_end_1:
    name: 舒适温度结束
    has_date: false
    has_time: true
  heating_time_group_2_start_2:
    name: 舒适温度开始(时间段2)
    has_date: false
    has_time: true
  heating_time_group_2_end_2:
    name: 舒适温度结束(时间段2)
    has_date: false
    has_time: true
  heating_time_group_2_start_3:
    name: 舒适温度开始(时间段3)
    has_date: false
    has_time: true
  heating_time_group_2_end_3:
    name: 舒适温度结束(时间段3)
    has_date: false
    has_time: true
  #####################################
  # 时间段(模式2)
  #####################################
  heating_time_mode2_group_1_start_1:
    name: 工作日开始1
    has_date: false
    has_time: true
  heating_time_mode2_group_1_end_1:
    name: 工作日结束1
    has_date: false
    has_time: true
  heating_time_mode2_group_1_start_2:
    name: 工作日开始2
    has_date: false
    has_time: true
  heating_time_mode2_group_1_end_2:
    name: 工作日结束2
    has_date: false
    has_time: true
  heating_time_mode2_group_1_start_3:
    name: 工作日开始3
    has_date: false
    has_time: true
  heating_time_mode2_group_1_end_3:
    name: 工作日结束3
    has_date: false
    has_time: true
  heating_time_mode2_group_1_start_4:
    name: 工作日开始4
    has_date: false
    has_time: true
  heating_time_mode2_group_1_end_4:
    name: 工作日结束4
    has_date: false
    has_time: true
  heating_time_mode2_group_1_start_5:
    name: 工作日开始5
    has_date: false
    has_time: true
  heating_time_mode2_group_1_end_5:
    name: 工作日结束5
    has_date: false
    has_time: true
  heating_time_mode2_group_2_start_1:
    name: 休息日开始1
    has_date: false
    has_time: true
  heating_time_mode2_group_2_end_1:
    name: 休息日结束1
    has_date: false
    has_time: true
  heating_time_mode2_group_2_start_2:
    name: 休息日开始2
    has_date: false
    has_time: true
  heating_time_mode2_group_2_end_2:
    name: 休息日结束2
    has_date: false
    has_time: true
  heating_time_mode2_group_2_start_3:
    name: 休息日开始3
    has_date: false
    has_time: true
  heating_time_mode2_group_2_end_3:
    name: 休息日结束3
    has_date: false
    has_time: true
  heating_time_mode2_group_2_start_4:
    name: 休息日开始4
    has_date: false
    has_time: true
  heating_time_mode2_group_2_end_4:
    name: 休息日结束4
    has_date: false
    has_time: true
  heating_time_mode2_group_2_start_5:
    name: 休息日开始5
    has_date: false
    has_time: true
  heating_time_mode2_group_2_end_5:
    name: 休息日结束5
    has_date: false
    has_time: true
  #####################################
  # 定时启动
  #####################################
  heating_time_timer_exec_start_time_1:
    name: 定时启动时间1
    has_date: false
    has_time: true
  heating_time_timer_exec_start_time_2:
    name: 定时启动时间2
    has_date: false
    has_time: true
  heating_time_timer_exec_start_time_3:
    name: 定时启动时间3
    has_date: false
    has_time: true
  heating_time_timer_exec_start_time_4:
    name: 定时启动时间4
    has_date: false
    has_time: true



# ------------------------------------------------------------------------------#
# 倒计时 打开手动加热
# ------------------------------------------------------------------------------#
- alias: Heating Turn On Manual By Timer Change
  id: heating_turn_on_manual_by_timer_change
  trigger:
    # 改变倒计时时间
    - platform: numeric_state
      entity_id: input_number.heating_number_open_duration
      above: "0.0"
  condition:
    # 温控打开状态
    - condition: state
      entity_id: input_boolean.heating_boolean_on_off_1
      state: "on"
    # 手动模式关闭
    - condition: state
      entity_id: input_boolean.heating_boolean_manual_1
      state: "off"
    # 倒计时不是0
    - condition: numeric_state
      entity_id: input_number.heating_number_open_duration
      above: "0.0"
  action:
    # 备份手动温度
    - service: input_number.set_value
      data_template:
        entity_id: input_number.heating_number_save_temp_manual
        value: >-
          {{ states("input_number.heating_number_temp_manual_1") }}
    # 手动温度设置为最大
    - service: input_number.set_value
      data:
        entity_id: input_number.heating_number_temp_manual_1
        value: "30"
    # 打开手动模式
    - service: input_boolean.turn_on
      data:
        entity_id: input_boolean.heating_boolean_manual_1

# ------------------------------------------------------------------------------#
# 倒计时 关闭手动加热
# ------------------------------------------------------------------------------#
- alias: Heating Turn off Manual By Timer To Zero
  id: heating_turn_off_manual_by_timer_to_zero
  trigger:
    # 清零倒计时时间
    - platform: state
      entity_id: input_number.heating_number_open_duration
      to: "0.0"
  condition:
    # 温控打开状态
    - condition: state
      entity_id: input_boolean.heating_boolean_on_off_1
      state: "on"
    # 手动模式打开
    - condition: state
      entity_id: input_boolean.heating_boolean_manual_1
      state: "on"
    # 倒计时是0
    # - condition: state
    #   entity_id: input_number.heating_number_open_duration
    #   state: "0.0"
    # 备份温度大于0
    - condition: numeric_state
      entity_id: input_number.heating_number_save_temp_manual
      above: "0.0"
  action:
    # 还原手动温度
    - service: input_number.set_value
      data_template:
        entity_id: input_number.heating_number_temp_manual_1
        value: >-
          {{ states("input_number.heating_number_save_temp_manual") }}
    # 清空备份温度
    - service: input_number.set_value
      data:
        entity_id: input_number.heating_number_save_temp_manual
        value: "0"
    # 关闭手动模式
    - service: input_boolean.turn_off
      data:
        entity_id: input_boolean.heating_boolean_manual_1

# ------------------------------------------------------------------------------#
# 关闭手动模式时还原温度和清空倒计时
# 关闭手动模式的处理,无法调用 倒计时 关闭手动加热,因为手动模式已经关闭
# ------------------------------------------------------------------------------#
- alias: Heating Reset Timer By Turn Off Manual
  id: heating_reset_timer_by_turn_off_manual
  trigger:
    # 关闭手动模式
    - platform: state
      entity_id: input_boolean.heating_boolean_manual_1
      to: "off"
  condition:
    # 温控打开状态
    - condition: state
      entity_id: input_boolean.heating_boolean_on_off_1
      state: "on"
    # 备份温度大于0
    - condition: numeric_state
      entity_id: input_number.heating_number_save_temp_manual
      above: "0.0"
  action:
    # 还原手动温度
    - service: input_number.set_value
      data_template:
        entity_id: input_number.heating_number_temp_manual_1
        value: >-
          {{ states("input_number.heating_number_save_temp_manual") }}
    # 清空备份温度
    - service: input_number.set_value
      data:
        entity_id: input_number.heating_number_save_temp_manual
        value: "0"
    # 清空倒计时
    - service: input_number.set_value
      data:
        entity_id: input_number.heating_number_open_duration
        value: "0"

# ------------------------------------------------------------------------------#
# 倒计时 关闭手动加热
# ------------------------------------------------------------------------------#
- alias: Heating Turn Off Manual By Timer Limit
  id: heating_turn_off_manual_by_timer_limit
  trigger:
    # 每1分钟执行
    - platform: time_pattern
      minutes: "/1"
      seconds: "12"
  condition:
    # 手动模式打开
    - condition: state
      entity_id: input_boolean.heating_boolean_manual_1
      state: "on"
    # 倒计时大于0
    - condition: numeric_state
      entity_id: input_number.heating_number_open_duration
      above: "0"
    # 超过设定时间
    - condition: template
      value_template: >-
        {{ (as_timestamp(now()) - as_timestamp(states.input_boolean.heating_boolean_manual_1.last_changed)) | int >= states("input_number.heating_number_open_duration") | int * 60 }}
  action:
    # # 还原手动温度
    # - service: input_number.set_value
    #   data_template:
    #     entity_id: input_number.heating_number_temp_manual_1
    #     value: >-
    #       {{ states("input_number.heating_number_save_temp_manual") }}
    # # 清空倒计时
    # - service: input_number.set_value
    #   data:
    #     entity_id: input_number.heating_number_open_duration
    #     value: "0"
    # # 关闭手动模式
    # - service: input_boolean.turn_off
    #   data:
    #     entity_id: input_boolean.heating_boolean_manual_1
    #
    # 清空倒计时,自动调用 倒计时 关闭手动加热
    - service: input_number.set_value
      data:
        entity_id: input_number.heating_number_open_duration
        value: "0"

# ------------------------------------------------------------------------------#
# 手动模式 手动模式关闭后不会自动关闭燃气炉,等温度到达当天设定的温度后燃气炉才会关闭
# ------------------------------------------------------------------------------#
- alias: Heating Turn On Off Schedule Manual
  id: "heating_turn_on_off_schedule_manual"
  trigger:
    # 每1分钟执行
    - platform: time_pattern
      minutes: "/1"
      seconds: "13"
      # 变更手动温度
    - platform: state
      entity_id: input_number.heating_number_temp_manual_1
      # 手动模式
    - platform: state
      entity_id: input_boolean.heating_boolean_manual_1
      to: "on"
  variables:
    varService: >-
      {# # 初始化 # #}
      {% set varService = "" %}

      {# # 低温 # #}
      {% if states('sensor.heating_temperature') | float < ( states('input_number.heating_number_temp_manual_1') | float - 0.5 ) %}
        {% if is_state("switch.cooking_room_heating","off") %}
          {% set varService = "turn_on" %}
        {% endif %}
      {% endif %}

      {# # 高温 # #}
      {% if states('sensor.heating_temperature') | float > ( states('input_number.heating_number_temp_manual_1') | float + 0.5 ) %}
        {% if is_state("switch.cooking_room_heating","on") %}
        {% set varService = "turn_off" %}
        {% endif %}
      {% endif %}

      {# # 返回结果 # #}
      {{ varService }} 

  condition:
    # 温控打开状态
    - condition: state
      entity_id: input_boolean.heating_boolean_on_off_1
      state: "on"
      # 手动模式
    - condition: state
      entity_id: input_boolean.heating_boolean_manual_1
      state: "on"
      # 判断是否需要操作燃气炉
    - condition: template
      value_template: "{{ varService == 'turn_on' or varService == 'turn_off' }}"
        # {# # 初始化 # #}
        # {% set isHeating = false %}
        # {% set varService = "" %}
  
        # {# # 低温 # #}
        # {% if states('sensor.heating_temperature') | float < ( states('input_number.heating_number_temp_manual_1') | float - 0.5 ) %}
        #   {% if is_state("switch.cooking_room_heating","off") %}
        #     {% set isHeating = true %}
        #   {% endif %}
        # {% endif %}
  
        # {# # 高温 # #}
        # {% if states('sensor.heating_temperature') | float > ( states('input_number.heating_number_temp_manual_1') | float + 0.5 ) %}
        #   {% if is_state("switch.cooking_room_heating","on") %}
        #     {% set isHeating = true %}
        #   {% endif %}
        # {% endif %}
  
        # {# # 返回结果 # #}
        # {{ isHeating }} 
  action:
    # 打开暖气
    - service: "switch.{{ varService }}"
      data:
        entity_id: switch.cooking_room_heating

# ------------------------------------------------------------------------------#
# 手动温度 0度时候自动设置22度
# ------------------------------------------------------------------------------#
- alias: Heating Set Initialize Value Manual Temp
  id: "heating_set_initialize_value_manual_temp"
  trigger:
    # 系统启动时
    - platform: homeassistant
      event: start
      # 温控打开时
    - platform: state
      entity_id: input_boolean.heating_boolean_on_off_1
  condition:
    # 温控打开状态
    - condition: state
      entity_id: input_boolean.heating_boolean_on_off_1
      state: "on"
    # 手动温度是0度
    - condition: state
      entity_id: input_number.heating_number_temp_manual_1
      state: "0"
  action:
    - service: input_number.set_value
      data:
        entity_id: input_number.heating_number_temp_manual_1
        value: "22"

# ------------------------------------------------------------------------------#
# 星期设置 未开启 自动开启
# ------------------------------------------------------------------------------#
- alias: Heating Set Initialize Value On Week All Close
  id: "heating_set_initialize_value_on_week_all_close"
  trigger:
    # 系统启动时
    - platform: homeassistant
      event: start
      # 温控打开时
    - platform: state
      entity_id: input_boolean.heating_boolean_on_off_1
      # 每1分钟执行 在温度检查之前执行
  # - platform: time_pattern
  #   minutes: '/1'
  #   seconds: '58'
  condition:
    # 温控打开状态
    - condition: state
      entity_id: input_boolean.heating_boolean_on_off_1
      state: "on"
      # 全打开的自动化执行10秒后才执行
    # - condition: template
    #   value_template: >-
    #     {% if state_attr('automation.heating_set_initialize_value_on_week_all_open','last_triggered') == 'None' %}
    #       true
    #     {% else %}
    #       {{ (as_timestamp(now()) - as_timestamp(state_attr('automation.heating_set_initialize_value_on_week_all_open','last_triggered')) | int > 10 }}
    #     {% endif %}
  action:
    # 周1 同时打开
    - service: >-
        {% if is_state('input_boolean.heating_boolean_group_1_week_1','off') and
                 is_state('input_boolean.heating_boolean_group_2_week_1','off') %}
          input_boolean.turn_on
        {% else %}
          script.none
        {% endif %}
      data_template:
        entity_id: input_boolean.heating_boolean_group_1_week_1
      # 周2 同时打开
    - service: >-
        {% if is_state('input_boolean.heating_boolean_group_1_week_2','off') and
                 is_state('input_boolean.heating_boolean_group_2_week_2','off') %}
          input_boolean.turn_on
        {% else %}
          script.none
        {% endif %}
      data_template:
        entity_id: input_boolean.heating_boolean_group_1_week_2
      # 周3 同时打开
    - service: >-
        {% if is_state('input_boolean.heating_boolean_group_1_week_3','off') and
                 is_state('input_boolean.heating_boolean_group_2_week_3','off') %}
          input_boolean.turn_on
        {% else %}
          script.none
        {% endif %}
      data_template:
        entity_id: input_boolean.heating_boolean_group_1_week_3
      # 周4 同时打开
    - service: >-
        {% if is_state('input_boolean.heating_boolean_group_1_week_4','off') and
                 is_state('input_boolean.heating_boolean_group_2_week_4','off') %}
          input_boolean.turn_on
        {% else %}
          script.none
        {% endif %}
      data_template:
        entity_id: input_boolean.heating_boolean_group_1_week_4
      # 周5 同时打开
    - service: >-
        {% if is_state('input_boolean.heating_boolean_group_1_week_5','off') and
                 is_state('input_boolean.heating_boolean_group_2_week_5','off') %}
          input_boolean.turn_on
        {% else %}
          script.none
        {% endif %}
      data_template:
        entity_id: input_boolean.heating_boolean_group_1_week_5
      # 周6 同时打开
    - service: >-
        {% if is_state('input_boolean.heating_boolean_group_1_week_6','off') and
                 is_state('input_boolean.heating_boolean_group_2_week_6','off') %}
          input_boolean.turn_on
        {% else %}
          script.none
        {% endif %}
      data_template:
        entity_id: input_boolean.heating_boolean_group_2_week_6
      # 周日 同时打开
    - service: >-
        {% if is_state('input_boolean.heating_boolean_group_1_week_7','off') and
                 is_state('input_boolean.heating_boolean_group_2_week_7','off') %}
          input_boolean.turn_on
        {% else %}
          script.none
        {% endif %}
      data_template:
        entity_id: input_boolean.heating_boolean_group_2_week_7

# ------------------------------------------------------------------------------#
# 星期设置重复 自动关闭
# ------------------------------------------------------------------------------#
- alias: Heating Set Initialize Value On Week All Open
  id: "heating_set_initialize_value_on_week_all_open"
  trigger:
    # 系统启动时
    - platform: homeassistant
      event: start
      # 温控打开时
    - platform: state
      entity_id: input_boolean.heating_boolean_on_off_1
      # 更改星期时
    - platform: state
      entity_id:
        - input_boolean.heating_boolean_group_1_week_1
        - input_boolean.heating_boolean_group_1_week_2
        - input_boolean.heating_boolean_group_1_week_3
        - input_boolean.heating_boolean_group_1_week_4
        - input_boolean.heating_boolean_group_1_week_5
        - input_boolean.heating_boolean_group_1_week_6
        - input_boolean.heating_boolean_group_1_week_7
        - input_boolean.heating_boolean_group_2_week_1
        - input_boolean.heating_boolean_group_2_week_2
        - input_boolean.heating_boolean_group_2_week_3
        - input_boolean.heating_boolean_group_2_week_4
        - input_boolean.heating_boolean_group_2_week_5
        - input_boolean.heating_boolean_group_2_week_6
        - input_boolean.heating_boolean_group_2_week_7
  condition:
    # 温控打开状态
    - condition: state
      entity_id: input_boolean.heating_boolean_on_off_1
      state: "on"
    #   # 日期编辑模式
    # - condition: state
    #   entity_id: input_boolean.heating_boolean_edit_mode_1
    #   state: 'on'
  action:
    # 周1 同时打开
    - service: >-
        {% if is_state('input_boolean.heating_boolean_group_1_week_1','on') and
                 is_state('input_boolean.heating_boolean_group_2_week_1','on') %}
          input_boolean.turn_off
        {% else %}
          script.none
        {% endif %}
      data_template:
        entity_id: input_boolean.heating_boolean_group_2_week_1
      # 周2 同时打开
    - service: >-
        {% if is_state('input_boolean.heating_boolean_group_1_week_2','on') and
                 is_state('input_boolean.heating_boolean_group_2_week_2','on') %}
          input_boolean.turn_off
        {% else %}
          script.none
        {% endif %}
      data_template:
        entity_id: input_boolean.heating_boolean_group_2_week_2
      # 周3 同时打开
    - service: >-
        {% if is_state('input_boolean.heating_boolean_group_1_week_3','on') and
                 is_state('input_boolean.heating_boolean_group_2_week_3','on') %}
          input_boolean.turn_off
        {% else %}
          script.none
        {% endif %}
      data_template:
        entity_id: input_boolean.heating_boolean_group_2_week_3
      # 周4 同时打开
    - service: >-
        {% if is_state('input_boolean.heating_boolean_group_1_week_4','on') and
                 is_state('input_boolean.heating_boolean_group_2_week_4','on') %}
          input_boolean.turn_off
        {% else %}
          script.none
        {% endif %}
      data_template:
        entity_id: input_boolean.heating_boolean_group_2_week_4
      # 周5 同时打开
    - service: >-
        {% if is_state('input_boolean.heating_boolean_group_1_week_5','on') and
                 is_state('input_boolean.heating_boolean_group_2_week_5','on') %}
          input_boolean.turn_off
        {% else %}
          script.none
        {% endif %}
      data_template:
        entity_id: input_boolean.heating_boolean_group_2_week_5
      # 周6 同时打开
    - service: >-
        {% if is_state('input_boolean.heating_boolean_group_1_week_6','on') and
                 is_state('input_boolean.heating_boolean_group_2_week_6','on') %}
          input_boolean.turn_off
        {% else %}
          script.none
        {% endif %}
      data_template:
        entity_id: input_boolean.heating_boolean_group_1_week_6
      # 周日 同时打开
    - service: >-
        {% if is_state('input_boolean.heating_boolean_group_1_week_7','on') and
                 is_state('input_boolean.heating_boolean_group_2_week_7','on') %}
          input_boolean.turn_off
        {% else %}
          script.none
        {% endif %}
      data_template:
        entity_id: input_boolean.heating_boolean_group_1_week_7

# ------------------------------------------------------------------------------#
# 温度/时间 设置检查
# ------------------------------------------------------------------------------#
- alias: Heating Mode2 Set Initialize Value
  id: "heating_mode2_set_initialize_value"
  trigger:
    # 系统启动时
    - platform: homeassistant
      event: start
      # 温控打开时
    - platform: state
      entity_id: input_boolean.heating_boolean_on_off_1
      to: "on"

  variables:
    varSetGroup1_Init: >-
      {# # 初始化 # #}
      {% set varInitTime = False %}
      {% set varInitTemp = False %}
      {% set varGroup = "1" %}

      {# # 全部时间段未打开时 # #}
      {% if is_state("input_boolean.heating_boolean_mode2_group_" + varGroup +"_switch_1","off") and 
            is_state("input_boolean.heating_boolean_mode2_group_" + varGroup +"_switch_2","off") and
            is_state("input_boolean.heating_boolean_mode2_group_" + varGroup +"_switch_3","off") and
            is_state("input_boolean.heating_boolean_mode2_group_" + varGroup +"_switch_4","off") and
            is_state("input_boolean.heating_boolean_mode2_group_" + varGroup +"_switch_5","off")  %}
        {% set varInitTime = True %}
      {% endif %}

      {# # 全部时间段温度相同 # #}
      {% if states("input_datetime.heating_time_mode2_group_" + varGroup +"_start_1") == states("input_datetime.heating_time_mode2_group_" + varGroup +"_end_1") and 
            states("input_datetime.heating_time_mode2_group_" + varGroup +"_start_2") == states("input_datetime.heating_time_mode2_group_" + varGroup +"_end_2") and 
            states("input_datetime.heating_time_mode2_group_" + varGroup +"_start_3") == states("input_datetime.heating_time_mode2_group_" + varGroup +"_end_3") and 
            states("input_datetime.heating_time_mode2_group_" + varGroup +"_start_4") == states("input_datetime.heating_time_mode2_group_" + varGroup +"_end_4") and 
            states("input_datetime.heating_time_mode2_group_" + varGroup +"_start_5") == states("input_datetime.heating_time_mode2_group_" + varGroup +"_end_5")  %}
        {% set varInitTemp = True %}
      {% endif %}

      {# # 开关和时间段都是初期状态的话,返回需要初始化 # #}
      {% if varInitTime == True or varInitTemp == True %}
        True
      {% else %}
        False
      {% endif %} 

    varSetGroup1_Init_Service_Boolean: >-
      {% if varSetGroup1_Init == True %}
        input_boolean.turn_on
      {% else %}
        script.none
      {% endif %} 
    varSetGroup1_Init_Service_Datetime: >-
      {% if varSetGroup1_Init == True %}
        input_datetime.set_datetime
      {% else %}
        script.none
      {% endif %} 
    varSetGroup1_Init_Service_Number: >-
      {% if varSetGroup1_Init == True %}
        input_number.set_value
      {% else %}
        script.none
      {% endif %} 

    varSetGroup2_Init: >-
      {# # 初始化 # #}
      {% set varInitTime = False %}
      {% set varInitTemp = False %}
      {% set varGroup = "2" %}

      {# # 全部时间段未打开时 # #}
      {% if is_state("input_boolean.heating_boolean_mode2_group_" + varGroup +"_switch_1","off") and 
            is_state("input_boolean.heating_boolean_mode2_group_" + varGroup +"_switch_2","off") and
            is_state("input_boolean.heating_boolean_mode2_group_" + varGroup +"_switch_3","off") and
            is_state("input_boolean.heating_boolean_mode2_group_" + varGroup +"_switch_4","off") and
            is_state("input_boolean.heating_boolean_mode2_group_" + varGroup +"_switch_5","off")  %}
        {% set varInitTime = True %}
      {% endif %}

      {# # 全部时间段温度相同 # #}
      {% if states("input_datetime.heating_time_mode2_group_" + varGroup +"_start_1") == states("input_datetime.heating_time_mode2_group_" + varGroup +"_end_1") and 
            states("input_datetime.heating_time_mode2_group_" + varGroup +"_start_2") == states("input_datetime.heating_time_mode2_group_" + varGroup +"_end_2") and 
            states("input_datetime.heating_time_mode2_group_" + varGroup +"_start_3") == states("input_datetime.heating_time_mode2_group_" + varGroup +"_end_3") and 
            states("input_datetime.heating_time_mode2_group_" + varGroup +"_start_4") == states("input_datetime.heating_time_mode2_group_" + varGroup +"_end_4") and 
            states("input_datetime.heating_time_mode2_group_" + varGroup +"_start_5") == states("input_datetime.heating_time_mode2_group_" + varGroup +"_end_5")  %}
        {% set varInitTemp = True %}
      {% endif %}

      {# # 开关和时间段都是初期状态的话,返回需要初始化 # #}
      {% if varInitTime == True or varInitTemp == True %}
        True
      {% else %}
        False
      {% endif %} 

    varSetGroup2_Init_Service_Boolean: >-
      {% if varSetGroup2_Init == True %}
        input_boolean.turn_on
      {% else %}
        script.none
      {% endif %} 
    varSetGroup2_Init_Service_Datetime: >-
      {% if varSetGroup2_Init == True %}
        input_datetime.set_datetime
      {% else %}
        script.none
      {% endif %} 
    varSetGroup2_Init_Service_Number: >-
      {% if varSetGroup2_Init == True %}
        input_number.set_value
      {% else %}
        script.none
      {% endif %} 

  condition:
    # 温控打开状态
    - condition: state
      entity_id: input_boolean.heating_boolean_on_off_1
      state: "on"
    # 需要初始化
    - condition: template
      value_template: "{{ varSetGroup1_Init == True or varSetGroup2_Init == True }}"
  action:
    # - service: notify.persistent_notification
    #   data:
    #     title: "var"
    #     message: "Boolean:{{ varSetGroup1_Init_Service_Boolean }} Datetime:{{ varSetGroup1_Init_Service_Datetime }} Number:{{ varSetGroup1_Init_Service_Number }} "
    #####################
    # 工作日
    #####################
    # 打开时间段1
    - service: "{{ varSetGroup1_Init_Service_Boolean }}"
      data:
        entity_id: input_boolean.heating_boolean_mode2_group_1_switch_1
    # 设定时间段1开始
    - service: "{{ varSetGroup1_Init_Service_Datetime }}"
      data:
        entity_id: input_datetime.heating_time_mode2_group_1_start_1
        time: "00:00:00"
    # 设定时间段1结束
    - service: "{{ varSetGroup1_Init_Service_Datetime }}"
      data:
        entity_id: input_datetime.heating_time_mode2_group_1_end_1
        time: "23:59:59"
    # 设定时间段1温度
    - service: "{{ varSetGroup1_Init_Service_Number }}"
      data:
        entity_id: input_number.heating_number_mode2_group_1_temp_1
        value: "19"
    # 打开时间段2
    - service: "{{ varSetGroup1_Init_Service_Boolean }}"
      data:
        entity_id: input_boolean.heating_boolean_mode2_group_1_switch_2
    # 设定时间段2开始
    - service: "{{ varSetGroup1_Init_Service_Datetime }}"
      data:
        entity_id: input_datetime.heating_time_mode2_group_1_start_2
        time: "05:00:00"
    # 设定时间段2结束
    - service: "{{ varSetGroup1_Init_Service_Datetime }}"
      data:
        entity_id: input_datetime.heating_time_mode2_group_1_end_2
        time: "22:00:00"
    # 设定时间段2温度
    - service: "{{ varSetGroup1_Init_Service_Number }}"
      data:
        entity_id: input_number.heating_number_mode2_group_1_temp_2
        value: "21.5"
    #####################
    # 休息日
    #####################
    # 打开时间段1
    - service: "{{ varSetGroup2_Init_Service_Boolean }}"
      data:
        entity_id: input_boolean.heating_boolean_mode2_group_2_switch_1
    # 设定时间段1开始
    - service: "{{ varSetGroup2_Init_Service_Datetime }}"
      data:
        entity_id: input_datetime.heating_time_mode2_group_2_start_1
        time: "00:00:00"
    # 设定时间段1结束
    - service: "{{ varSetGroup2_Init_Service_Datetime }}"
      data:
        entity_id: input_datetime.heating_time_mode2_group_2_end_1
        time: "23:59:59"
    # 设定时间段1温度
    - service: "{{ varSetGroup2_Init_Service_Number }}"
      data:
        entity_id: input_number.heating_number_mode2_group_2_temp_1
        value: "19"
    # 打开时间段2
    - service: "{{ varSetGroup2_Init_Service_Boolean }}"
      data:
        entity_id: input_boolean.heating_boolean_mode2_group_2_switch_2
    # 设定时间段2开始
    - service: "{{ varSetGroup2_Init_Service_Datetime }}"
      data:
        entity_id: input_datetime.heating_time_mode2_group_2_start_2
        time: "06:00:00"
    # 设定时间段2结束
    - service: "{{ varSetGroup2_Init_Service_Datetime }}"
      data:
        entity_id: input_datetime.heating_time_mode2_group_2_end_2
        time: "22:00:00"
    # 设定时间段2温度
    - service: "{{ varSetGroup2_Init_Service_Number }}"
      data:
        entity_id: input_number.heating_number_mode2_group_2_temp_2
        value: "21.5"


# ------------------------------------------------------------------------------#
# 定时启动暖气
# 如果当前已经是手动模式,则不执行
# ------------------------------------------------------------------------------#
- alias: "Heating Auto TurnOn By Time"
  id: "heating_auto_turnon_by_time"
  trigger:
    # 每1分钟执行
    - platform: time_pattern
      minutes: "/1"
      seconds: "10"
  variables:
    # 判断是否需要定时启动,需要启动返回 需要启动的时长,不需要则返回0
    openDuration: >-
      {# # 初始化 # #}
      {% set openDuration = "0" %}

      {% if is_state("input_boolean.heating_boolean_timer_exec_switch_1","on") %}
        {% if states("input_datetime.heating_time_timer_exec_start_time_1") == (states("sensor.time") + ":00") %}
          {% set openDuration = states("input_number.heating_number_timer_exec_open_duration_1") %}
        {% endif %}
      {% endif %}

      {% if is_state("input_boolean.heating_boolean_timer_exec_switch_2","on") %}
        {% if states("input_datetime.heating_time_timer_exec_start_time_2") == (states("sensor.time") + ":00") %}
          {% set openDuration = states("input_number.heating_number_timer_exec_open_duration_2") %}
        {% endif %}
      {% endif %}

      {% if is_state("input_boolean.heating_boolean_timer_exec_switch_3","on") %}
        {% if states("input_datetime.heating_time_timer_exec_start_time_3") == (states("sensor.time") + ":00") %}
          {% set openDuration = states("input_number.heating_number_timer_exec_open_duration_3") %}
        {% endif %}
      {% endif %}

      {% if is_state("input_boolean.heating_boolean_timer_exec_switch_4","on") %}
        {% if states("input_datetime.heating_time_timer_exec_start_time_4") == (states("sensor.time") + ":00") %}
          {% set openDuration = states("input_number.heating_number_timer_exec_open_duration_4") %}
        {% endif %}
      {% endif %}

      {# # 返回结果 # #}
      {{ openDuration }}

  condition:
    # 打开状态
    - condition: state
      entity_id: input_boolean.heating_boolean_on_off_1
      state: "on"
      # 自动模式
    - condition: state
      entity_id: input_boolean.heating_boolean_manual_1
      state: "off"
      # 没有倒计时
    - condition: state
      entity_id: input_number.heating_number_open_duration
      state: "0.0"
      # 判断是否需要定时启动
    - condition: template
      value_template: "{{ ( openDuration | float > 0.0 ) }}"

      # 判断是否需要启动燃气炉
    # - condition: template
    #   value_template: >-
    #     {# # 初始化 # #}
    #     {% set isHeating = false %}
    #     {% if states("input_datetime.heating_time_timer_exec_start_time_1") == (states("sensor.time") + ":00") %}
    #       {% set isHeating = true %}
    #     {% endif %}
    #     {% if states("input_datetime.heating_time_timer_exec_start_time_2") == (states("sensor.time") + ":00") %}
    #       {% set isHeating = true %}
    #     {% endif %}
    #     {% if states("input_datetime.heating_time_timer_exec_start_time_3") == (states("sensor.time") + ":00") %}
    #       {% set isHeating = true %}
    #     {% endif %}
    #     {% if states("input_datetime.heating_time_timer_exec_start_time_4") == (states("sensor.time") + ":00") %}
    #       {% set isHeating = true %}
    #     {% endif %}
    #     {# # 返回结果 # #}
    #     {{ isHeating }}

  action:
    # - service: notify.persistent_notification
    #   data:
    #     title: "定时启动暖气"
    #     message: "{{ states('sensor.time') }} 启动暖气 {{ openDuration }} 分"
    # 启动暖气
    - service: input_number.set_value
      data:
        entity_id: input_number.heating_number_open_duration
        value: "{{ openDuration }}"

# ------------------------------------------------------------------------------#
# 打开暖气
# ------------------------------------------------------------------------------#
- alias: "Heating Mode2 Auto Control"
  id: "heating_mode2_auto_control"
  trigger:
    # 每1分钟执行
    - platform: time_pattern
      minutes: "/1"
      seconds: "19"
      # 调整时间/温度执行
    - platform: state
      entity_id:
        - input_boolean.heating_boolean_on_off_1
        - input_boolean.heating_boolean_manual_1
        - input_boolean.heating_boolean_mode2_group_1_switch_1
        - input_boolean.heating_boolean_mode2_group_1_switch_2
        - input_boolean.heating_boolean_mode2_group_1_switch_3
        - input_boolean.heating_boolean_mode2_group_1_switch_4
        - input_boolean.heating_boolean_mode2_group_1_switch_5
        - input_boolean.heating_boolean_mode2_group_2_switch_1
        - input_boolean.heating_boolean_mode2_group_2_switch_2
        - input_boolean.heating_boolean_mode2_group_2_switch_3
        - input_boolean.heating_boolean_mode2_group_2_switch_4
        - input_boolean.heating_boolean_mode2_group_2_switch_5
        - input_datetime.heating_time_mode2_group_1_start_1
        - input_datetime.heating_time_mode2_group_1_start_2
        - input_datetime.heating_time_mode2_group_1_start_3
        - input_datetime.heating_time_mode2_group_1_start_4
        - input_datetime.heating_time_mode2_group_1_start_5
        - input_datetime.heating_time_mode2_group_2_start_1
        - input_datetime.heating_time_mode2_group_2_start_2
        - input_datetime.heating_time_mode2_group_2_start_3
        - input_datetime.heating_time_mode2_group_2_start_4
        - input_datetime.heating_time_mode2_group_2_start_5
        - input_datetime.heating_time_mode2_group_1_end_1
        - input_datetime.heating_time_mode2_group_1_end_2
        - input_datetime.heating_time_mode2_group_1_end_3
        - input_datetime.heating_time_mode2_group_1_end_4
        - input_datetime.heating_time_mode2_group_1_end_5
        - input_datetime.heating_time_mode2_group_2_end_1
        - input_datetime.heating_time_mode2_group_2_end_2
        - input_datetime.heating_time_mode2_group_2_end_3
        - input_datetime.heating_time_mode2_group_2_end_4
        - input_datetime.heating_time_mode2_group_2_end_5
        - input_number.heating_number_mode2_group_1_temp_1
        - input_number.heating_number_mode2_group_1_temp_2
        - input_number.heating_number_mode2_group_1_temp_3
        - input_number.heating_number_mode2_group_1_temp_4
        - input_number.heating_number_mode2_group_1_temp_5
        - input_number.heating_number_mode2_group_2_temp_1
        - input_number.heating_number_mode2_group_2_temp_2
        - input_number.heating_number_mode2_group_2_temp_3
        - input_number.heating_number_mode2_group_2_temp_4
        - input_number.heating_number_mode2_group_2_temp_5
  variables:
    # 星期几
    varWeekDay: >-
      {{ now().isoweekday() }}

    # 工作日/休息日
    varGroup: >-
      {# # 初始化 # #}
      {% set numGroup = "0" %}

      {# # 工作日 # #}
      {% if is_state("input_boolean.heating_boolean_group_1_week_" + varWeekDay | string,"on") %}
        {% set numGroup = "1" %}
      {% endif %}

      {# # 休息日 # #}
      {% if is_state("input_boolean.heating_boolean_group_2_week_" + varWeekDay | string,"on") %}
        {% set numGroup = "2" %}
      {% endif %}

      {# # 返回数据 # #}
      {{ numGroup }}

    # 温度
    varTemperature: >-
      {# # 初始化 # #}
      {% set numTemp = 0.0 %}

      {# # 循环5个时间段 # #}

        {# # 时间段启动 # #}
        {% if is_state("input_boolean.heating_boolean_mode2_group_" + varGroup | string + "_switch_1","on") %}
          {# # 开始<结束 # #}
          {% if states("input_datetime.heating_time_mode2_group_" + varGroup | string + "_start_1") < states("input_datetime.heating_time_mode2_group_" + varGroup | string + "_end_1") %}
            {# # 开始<=现在时间<结束 # #}
            {% if states('sensor.time') >= (state_attr("input_datetime.heating_time_mode2_group_" + varGroup | string + "_start_1",'timestamp') | int | timestamp_custom('%H:%M', False)) %}
              {% if states('sensor.time') < (state_attr("input_datetime.heating_time_mode2_group_" + varGroup | string + "_end_1",'timestamp') | int | timestamp_custom('%H:%M', False)) %}
                {# # 如果设定温度更高 # #}
                {% if states("input_number.heating_number_mode2_group_" + varGroup | string + "_temp_1") | float > numTemp | float %}
                  {# # 设定温度 # #}
                  {% set numTemp = states("input_number.heating_number_mode2_group_" + varGroup | string + "_temp_1") | float %}
                {% endif %}
              {% endif %}
            {% endif %}
          {% endif %}
        {% endif %}

        {# # 时间段启动 # #}
        {% if is_state("input_boolean.heating_boolean_mode2_group_" + varGroup | string + "_switch_2","on") %}
          {# # 开始<结束 # #}
          {% if states("input_datetime.heating_time_mode2_group_" + varGroup | string + "_start_2") < states("input_datetime.heating_time_mode2_group_" + varGroup | string + "_end_2") %}
            {# # 开始<=现在时间<结束 # #}
            {% if states('sensor.time') >= (state_attr("input_datetime.heating_time_mode2_group_" + varGroup | string + "_start_2",'timestamp') | int | timestamp_custom('%H:%M', False)) %}
              {% if states('sensor.time') < (state_attr("input_datetime.heating_time_mode2_group_" + varGroup | string + "_end_2",'timestamp') | int | timestamp_custom('%H:%M', False)) %}
                {# # 如果设定温度更高 # #}
                {% if states("input_number.heating_number_mode2_group_" + varGroup | string + "_temp_2") | float > numTemp | float %}
                  {# # 设定温度 # #}
                  {% set numTemp = states("input_number.heating_number_mode2_group_" + varGroup | string + "_temp_2") | float %}
                {% endif %}
              {% endif %}
            {% endif %}
          {% endif %}
        {% endif %}

        {# # 时间段启动 # #}
        {% if is_state("input_boolean.heating_boolean_mode2_group_" + varGroup | string + "_switch_3","on") %}
          {# # 开始<结束 # #}
          {% if states("input_datetime.heating_time_mode2_group_" + varGroup | string + "_start_3") < states("input_datetime.heating_time_mode2_group_" + varGroup | string + "_end_3") %}
            {# # 开始<=现在时间<结束 # #}
            {% if states('sensor.time') >= (state_attr("input_datetime.heating_time_mode2_group_" + varGroup | string + "_start_3",'timestamp') | int | timestamp_custom('%H:%M', False)) %}
              {% if states('sensor.time') < (state_attr("input_datetime.heating_time_mode2_group_" + varGroup | string + "_end_3",'timestamp') | int | timestamp_custom('%H:%M', False)) %}
                {# # 如果设定温度更高 # #}
                {% if states("input_number.heating_number_mode2_group_" + varGroup | string + "_temp_3") | float > numTemp | float %}
                  {# # 设定温度 # #}
                  {% set numTemp = states("input_number.heating_number_mode2_group_" + varGroup | string + "_temp_3") | float %}
                {% endif %}
              {% endif %}
            {% endif %}
          {% endif %}
        {% endif %}

        {# # 时间段启动 # #}
        {% if is_state("input_boolean.heating_boolean_mode2_group_" + varGroup | string + "_switch_4","on") %}
          {# # 开始<结束 # #}
          {% if states("input_datetime.heating_time_mode2_group_" + varGroup | string + "_start_4") < states("input_datetime.heating_time_mode2_group_" + varGroup | string + "_end_4") %}
            {# # 开始<=现在时间<结束 # #}
            {% if states('sensor.time') >= (state_attr("input_datetime.heating_time_mode2_group_" + varGroup | string + "_start_4",'timestamp') | int | timestamp_custom('%H:%M', False)) %}
              {% if states('sensor.time') < (state_attr("input_datetime.heating_time_mode2_group_" + varGroup | string + "_end_4",'timestamp') | int | timestamp_custom('%H:%M', False)) %}
                {# # 如果设定温度更高 # #}
                {% if states("input_number.heating_number_mode2_group_" + varGroup | string + "_temp_4") | float > numTemp | float %}
                  {# # 设定温度 # #}
                  {% set numTemp = states("input_number.heating_number_mode2_group_" + varGroup | string + "_temp_4") | float %}
                {% endif %}
              {% endif %}
            {% endif %}
          {% endif %}
        {% endif %}

        {# # 时间段启动 # #}
        {% if is_state("input_boolean.heating_boolean_mode2_group_" + varGroup | string + "_switch_5","on") %}
          {# # 开始<结束 # #}
          {% if states("input_datetime.heating_time_mode2_group_" + varGroup | string + "_start_5") < states("input_datetime.heating_time_mode2_group_" + varGroup | string + "_end_5") %}
            {# # 开始<=现在时间<结束 # #}
            {% if states('sensor.time') >= (state_attr("input_datetime.heating_time_mode2_group_" + varGroup | string + "_start_5",'timestamp') | int | timestamp_custom('%H:%M', False)) %}
              {% if states('sensor.time') < (state_attr("input_datetime.heating_time_mode2_group_" + varGroup | string + "_end_5",'timestamp') | int | timestamp_custom('%H:%M', False)) %}
                {# # 如果设定温度更高 # #}
                {% if states("input_number.heating_number_mode2_group_" + varGroup | string + "_temp_5") | float > numTemp | float %}
                  {# # 设定温度 # #}
                  {% set numTemp = states("input_number.heating_number_mode2_group_" + varGroup | string + "_temp_5") | float %}
                {% endif %}
              {% endif %}
            {% endif %}
          {% endif %}
        {% endif %}

      {# # 返回数据 # #}
      {{ numTemp }}

    # # Test
    # varTest: >-
    #   {# # 初始化 # #}
    #   {% set numTemp = "0" %}
    #   {# # 循环5个时间段 # #}
    #   {% for i in ["1","2","3","4","5"] %}
    #       {% set numTemp = i %}
    #   {% endfor %}
    #   {{ numTemp }}

    #   {# # 循环5个时间段 # #}
    #   {% for i in ["1","2","3","4","5"] %}
    #     {# # 时间段启动 # #}
    #     {% if is_state("input_boolean.heating_boolean_mode2_group_" + varGroup | string + "_switch_" + i | string,"on") %}
    #       {# # 开始<结束 # #}
    #       {% if states("input_datetime.heating_time_mode2_group_" + varGroup | string + "_start_" + i | string) < states("input_datetime.heating_time_mode2_group_" + varGroup | string + "_end_" + i | string) %}
    #         {# # 开始<=现在时间<结束 # #}
    #         {% if states('sensor.time') >= (state_attr("input_datetime.heating_time_mode2_group_" + varGroup | string + "_start_" + i | string,'timestamp') | int | timestamp_custom('%H:%M', False)) %}
    #           {% if states('sensor.time') < (state_attr("input_datetime.heating_time_mode2_group_" + varGroup | string + "_end_" + i | string,'timestamp') | int | timestamp_custom('%H:%M', False)) %}
    #             {# # 如果设定温度更高 # #}
    #             {% if states("input_number.heating_number_mode2_group_" + varGroup | string + "_temp_" + i | string) | float > numTemp | float %}
    #               {# # 设定温度 # #}
    #               {% set numTemp = states("input_number.heating_number_mode2_group_" + varGroup | string + "_temp_" + i | string) | float %}
    #             {% endif %}
    #           {% endif %}
    #         {% endif %}
    #       {% endif %}
    #     {% endif %}
    #   {% endfor %}

    # Service
    varService: >-
      {# # 初始化 # #}
      {% set varService = "" %}

      {# # 有设定温度 # #}
      {% if varTemperature | float > 0.0 %}
        {# # 判断是否启动燃气炉 # #}
        {% if states('sensor.heating_temperature') | float < ( varTemperature | float - 0.5 ) %}
          {% set varService = "turn_on" %}
        {% endif %}
        {# # 判断是否关闭燃气炉 # #}
        {% if states('sensor.heating_temperature') | float > ( varTemperature | float + 0.5 ) %}
          {% set varService = "turn_off" %}
        {% endif %}
      {% endif %}

      {# # 返回结果 # #}
      {{ varService }}

  condition:
    # 打开状态
    - condition: state
      entity_id: input_boolean.heating_boolean_on_off_1
      state: "on"
      # 自动模式
    - condition: state
      entity_id: input_boolean.heating_boolean_manual_1
      state: "off"
      # 暖气关闭
    # - condition: state
    #   entity_id: switch.cooking_room_heating
    #   state: "off"
    # 判断是否需要控制暖气
    - condition: template
      value_template: >-
        {# # 初始化 # #}
        {% set isHeating = False %}

        {# # 需要打开 and 暖气现在关闭 # #}
        {% if varService == 'turn_on' and is_state("switch.cooking_room_heating","off") %}
          {% set isHeating = True %}
        {% endif %}

        {# # 需要关闭 and 暖气现在打开 # #}
        {% if varService == 'turn_off' and is_state("switch.cooking_room_heating","on") %}
          {% set isHeating = True %}
        {% endif %}

        {# # 返回结果 # #}
        {{ isHeating }}

  action:
    # - service: notify.persistent_notification
    #   data:
    #     title: "var"
    #     message: "Group:{{ varGroup }} Temp:{{ varTemperature }} Service:{{ varService }}"
    - service: "switch.{{ varService }}"
      data:
        entity_id: switch.cooking_room_heating


评分

参与人数 1金钱 +10 收起 理由
yylwhy + 10 在下对你的景仰犹如滔滔长江之水,连绵不绝.

查看全部评分

回复

使用道具 举报

1

主题

83

帖子

1001

积分

金牌会员

Rank: 6Rank: 6

积分
1001
金钱
918
HASS币
0
发表于 2021-8-14 09:38:10 | 显示全部楼层
本帖最后由 loveyolanda 于 2021-8-14 10:57 编辑
sirakawa 发表于 2021-8-14 09:26
所有Input_number,input_boolean之类的,都需要单独定义的不过就像楼上说的,如果你打算自己弄一份,最好 ...

谢谢,昨天经过摸索已经把自定义添加了,发现你刚发的代码相比之前的多了好多个辅助元素,请问能把最新的UI再发一遍吗?感激
回复

使用道具 举报

1

主题

83

帖子

1001

积分

金牌会员

Rank: 6Rank: 6

积分
1001
金钱
918
HASS币
0
发表于 2021-8-14 11:02:04 | 显示全部楼层
感谢大佬
回复

使用道具 举报

17

主题

798

帖子

5728

积分

论坛元老

Rank: 8Rank: 8

积分
5728
金钱
4925
HASS币
0
发表于 2021-8-14 23:05:49 | 显示全部楼层
loveyolanda 发表于 2021-8-14 09:38
谢谢,昨天经过摸索已经把自定义添加了,发现你刚发的代码相比之前的多了好多个辅助元素,请问能把最新的U ...

UI~

- type: vertical-stack
  cards:

  - type: horizontal-stack
    cards:

    - type: sensor
      name: "温度"
      entity: sensor.heating_temperature
      graph: line
      icon: mdi:home-thermometer-outline

    - type: glance
      title: 传感器
      entities:
        - entity: sensor.heating_temp_sensor_count
        - entity: sensor.time
          name: 时间
        - entity: binary_sensor.cooking_room_heating_status
          double_tap_action: 
            action: call-service
            service: switch.toggle
            service_data:
              entity_id: switch.cooking_room_heating

  - type: entities
    title: 温控器控制
    show_header_toggle: false
    entities:
      - entity: input_boolean.heating_boolean_on_off_1
      - entity: input_boolean.heating_boolean_manual_1
        # type: "custom:secondaryinfo-entity-row"
        # secondary_info: "[[ (input_boolean.heating_boolean_manual_1.last_changed | int | timestamp_custom('%H:%M', False)) ]]"
      - entity: input_number.heating_number_temp_manual_1
      - entity: input_number.heating_number_open_duration
      # - sensor.heating_timer_remaining
      # - input_number.heating_number_save_temp_manual

  - type: custom:vertical-stack-in-card
    cards:

    - type: markdown
      content: >-
        <font size='5'>定时启动</font>

    - type: horizontal-stack
      cards:

      - type: glance
        columns: 3
        entities:
          - entity: input_boolean.heating_boolean_timer_exec_switch_1
            name: "开关"
            icon: "mdi:numeric-1-circle-outline"
          - entity: input_datetime.heating_time_timer_exec_start_time_1
            name: "启动时间"
          - entity: input_number.heating_number_timer_exec_open_duration_1
            name: "持续时间"
      - type: glance
        columns: 3
        entities:
          - entity: input_boolean.heating_boolean_timer_exec_switch_2
            name: "开关"
            icon: "mdi:numeric-2-circle-outline"
          - entity: input_datetime.heating_time_timer_exec_start_time_2
            name: "启动时间"
          - entity: input_number.heating_number_timer_exec_open_duration_2
            name: "持续时间"

    - type: horizontal-stack
      cards:

      - type: glance
        columns: 3
        show_name: false
        entities:
          - entity: input_boolean.heating_boolean_timer_exec_switch_3
            icon: "mdi:numeric-3-circle-outline"
          - entity: input_datetime.heating_time_timer_exec_start_time_3
          - entity: input_number.heating_number_timer_exec_open_duration_3
      - type: glance
        columns: 3
        show_name: false
        entities:
          - entity: input_boolean.heating_boolean_timer_exec_switch_4
            icon: "mdi:numeric-4-circle-outline"
          - entity: input_datetime.heating_time_timer_exec_start_time_4
          - entity: input_number.heating_number_timer_exec_open_duration_4

    # - type: glance
    #   columns: 6
    #   show_name: false
    #   entities:
    #     - entity: input_boolean.heating_boolean_timer_exec_switch_3
    #       icon: "mdi:numeric-3-circle-outline"
    #     - entity: input_datetime.heating_time_timer_exec_start_time_3
    #     - entity: input_number.heating_number_timer_exec_open_duration_3
    #     - entity: input_boolean.heating_boolean_timer_exec_switch_4
    #       icon: "mdi:numeric-4-circle-outline"
    #     - entity: input_datetime.heating_time_timer_exec_start_time_4
    #     - entity: input_number.heating_number_timer_exec_open_duration_4



- type: vertical-stack
  cards:

  # - type: glance
  #   title: 工作日设定
  #   show_header_toggle: false
  #   entities:
  #     - input_number.heating_number_group_1_temp_high_1
  #     - input_number.heating_number_group_1_temp_low_1

  # - type: custom:vertical-stack-in-card
  #   cards:

  #   - type: glance
  #     columns: 2
  #     entities:
  #       - input_datetime.heating_time_group_1_start_1
  #       - input_datetime.heating_time_group_1_end_1

  #   - type: glance
  #     show_name: false
  #     columns: 2
  #     entities:
  #       - input_datetime.heating_time_group_1_start_2
  #       - input_datetime.heating_time_group_1_end_2

  #   - type: glance
  #     show_name: false
  #     columns: 2
  #     entities:
  #       - input_datetime.heating_time_group_1_start_3
  #       - input_datetime.heating_time_group_1_end_3

  - type: glance
    title: 工作日设定
    columns: 7
    show_header_toggle: false
    entities:
    - entity: input_boolean.heating_boolean_group_1_week_1
      icon: mdi:numeric-1-box
    - entity: input_boolean.heating_boolean_group_1_week_2
      icon: mdi:numeric-2-box
    - entity: input_boolean.heating_boolean_group_1_week_3
      icon: mdi:numeric-3-box
    - entity: input_boolean.heating_boolean_group_1_week_4
      icon: mdi:numeric-4-box
    - entity: input_boolean.heating_boolean_group_1_week_5
      icon: mdi:numeric-5-box
    - entity: input_boolean.heating_boolean_group_1_week_6
      icon: mdi:numeric-6-box
    - entity: input_boolean.heating_boolean_group_1_week_7
      icon: mdi:numeric-7-box


    # MODE2
  - type: custom:vertical-stack-in-card
    cards:

    - type: glance
      columns: 4
      entities:
        - entity: input_boolean.heating_boolean_mode2_group_1_switch_1
          name: "开关"
          icon: "mdi:numeric-1-circle-outline"
        - entity: input_datetime.heating_time_mode2_group_1_start_1
          name: "开始时间"
        - entity: input_datetime.heating_time_mode2_group_1_end_1
          name: "结束时间"
        - entity: input_number.heating_number_mode2_group_1_temp_1
          name: "温度"

    - type: glance
      show_name: false
      columns: 4
      entities:
        - entity: input_boolean.heating_boolean_mode2_group_1_switch_2
          icon: "mdi:numeric-2-circle-outline"
        - entity: input_datetime.heating_time_mode2_group_1_start_2
        - entity: input_datetime.heating_time_mode2_group_1_end_2
        - entity: input_number.heating_number_mode2_group_1_temp_2

    - type: glance
      show_name: false
      columns: 4
      entities:
        - entity: input_boolean.heating_boolean_mode2_group_1_switch_3
          icon: "mdi:numeric-3-circle-outline"
        - entity: input_datetime.heating_time_mode2_group_1_start_3
        - entity: input_datetime.heating_time_mode2_group_1_end_3
        - entity: input_number.heating_number_mode2_group_1_temp_3

    - type: glance
      show_name: false
      columns: 4
      entities:
        - entity: input_boolean.heating_boolean_mode2_group_1_switch_4
          icon: "mdi:numeric-4-circle-outline"
        - entity: input_datetime.heating_time_mode2_group_1_start_4
        - entity: input_datetime.heating_time_mode2_group_1_end_4
        - entity: input_number.heating_number_mode2_group_1_temp_4

    - type: glance
      show_name: false
      columns: 4
      entities:
        - entity: input_boolean.heating_boolean_mode2_group_1_switch_5
          icon: "mdi:numeric-5-circle-outline"
        - entity: input_datetime.heating_time_mode2_group_1_start_5
        - entity: input_datetime.heating_time_mode2_group_1_end_5
        - entity: input_number.heating_number_mode2_group_1_temp_5

- type: vertical-stack
  cards:

  # - type: glance
  #   title: 休息日设定
  #   show_header_toggle: false
  #   entities:
  #     - input_number.heating_number_group_2_temp_high_1
  #     - input_number.heating_number_group_2_temp_low_1

  # - type: custom:vertical-stack-in-card
  #   cards:

  #   - type: glance
  #     columns: 2
  #     entities:
  #       - input_datetime.heating_time_group_2_start_1
  #       - input_datetime.heating_time_group_2_end_1

  #   - type: glance
  #     show_name: false
  #     columns: 2
  #     entities:
  #       - input_datetime.heating_time_group_2_start_2
  #       - input_datetime.heating_time_group_2_end_2

  #   - type: glance
  #     show_name: false
  #     columns: 2
  #     entities:
  #       - input_datetime.heating_time_group_2_start_3
  #       - input_datetime.heating_time_group_2_end_3

  - type: glance
    title: 休息日设定
    columns: 7
    show_header_toggle: false
    entities:
    - entity: input_boolean.heating_boolean_group_2_week_1
      icon: mdi:numeric-1-box
    - entity: input_boolean.heating_boolean_group_2_week_2
      icon: mdi:numeric-2-box
    - entity: input_boolean.heating_boolean_group_2_week_3
      icon: mdi:numeric-3-box
    - entity: input_boolean.heating_boolean_group_2_week_4
      icon: mdi:numeric-4-box
    - entity: input_boolean.heating_boolean_group_2_week_5
      icon: mdi:numeric-5-box
    - entity: input_boolean.heating_boolean_group_2_week_6
      icon: mdi:numeric-6-box
    - entity: input_boolean.heating_boolean_group_2_week_7
      icon: mdi:numeric-7-box

    # MODE2
  - type: custom:vertical-stack-in-card
    cards:

    - type: glance
      columns: 4
      entities:
        - entity: input_boolean.heating_boolean_mode2_group_2_switch_1
          name: "开关"
          icon: "mdi:numeric-1-circle-outline"
        - entity: input_datetime.heating_time_mode2_group_2_start_1
          name: "开始时间"
        - entity: input_datetime.heating_time_mode2_group_2_end_1
          name: "结束时间"
        - entity: input_number.heating_number_mode2_group_2_temp_1
          name: "温度"

    - type: glance
      show_name: false
      columns: 4
      entities:
        - entity: input_boolean.heating_boolean_mode2_group_2_switch_2
          icon: "mdi:numeric-2-circle-outline"
        - entity: input_datetime.heating_time_mode2_group_2_start_2
        - entity: input_datetime.heating_time_mode2_group_2_end_2
        - entity: input_number.heating_number_mode2_group_2_temp_2

    - type: glance
      show_name: false
      columns: 4
      entities:
        - entity: input_boolean.heating_boolean_mode2_group_2_switch_3
          icon: "mdi:numeric-3-circle-outline"
        - entity: input_datetime.heating_time_mode2_group_2_start_3
        - entity: input_datetime.heating_time_mode2_group_2_end_3
        - entity: input_number.heating_number_mode2_group_2_temp_3

    - type: glance
      show_name: false
      columns: 4
      entities:
        - entity: input_boolean.heating_boolean_mode2_group_2_switch_4
          icon: "mdi:numeric-4-circle-outline"
        - entity: input_datetime.heating_time_mode2_group_2_start_4
        - entity: input_datetime.heating_time_mode2_group_2_end_4
        - entity: input_number.heating_number_mode2_group_2_temp_4

    - type: glance
      show_name: false
      columns: 4
      entities:
        - entity: input_boolean.heating_boolean_mode2_group_2_switch_5
          icon: "mdi:numeric-5-circle-outline"
        - entity: input_datetime.heating_time_mode2_group_2_start_5
        - entity: input_datetime.heating_time_mode2_group_2_end_5
        - entity: input_number.heating_number_mode2_group_2_temp_5
回复

使用道具 举报

1

主题

83

帖子

1001

积分

金牌会员

Rank: 6Rank: 6

积分
1001
金钱
918
HASS币
0
发表于 2021-8-15 09:20:57 | 显示全部楼层

非常感谢,慢慢学习研究
回复

使用道具 举报

1

主题

83

帖子

1001

积分

金牌会员

Rank: 6Rank: 6

积分
1001
金钱
918
HASS币
0
发表于 2021-8-15 10:03:13 | 显示全部楼层
本帖最后由 loveyolanda 于 2021-8-15 16:27 编辑

大佬,其他都看懂了,都搞定了,这两个传感器没搞懂,是自定义的吗?entity: sensor.heating_temp_sensor_count   entity: binary_sensor.cooking_room_heating_status 1629016006(1).jpg
回复

使用道具 举报

1

主题

40

帖子

159

积分

注册会员

Rank: 2

积分
159
金钱
119
HASS币
0
发表于 2024-10-28 12:49:27 | 显示全部楼层
不错,俺消化一下
回复

使用道具 举报

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

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-11-23 04:41 , Processed in 0.112753 second(s), 32 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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