Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save Joly0/f21a773fe428cb32f252ce11d8bbd0f2 to your computer and use it in GitHub Desktop.

Select an option

Save Joly0/f21a773fe428cb32f252ce11d8bbd0f2 to your computer and use it in GitHub Desktop.
Turn Off Heating When Door or Window Is Open
blueprint:
name: Turn Off Heating When Door or Window Is Open (with Restore)
description: >
Turns off the heating if a door or window remains open for a defined
duration. When all doors/windows close again, restore the previous
HVAC mode, but only if the automation turned it off.
domain: automation
input:
open_sensors:
name: Door or Window Sensors
description: Select the door and/or window sensors to monitor.
selector:
entity:
domain: binary_sensor
device_class:
- door
- window
multiple: true
climate_entity:
name: Climate Device
description: The thermostat / climate entity to control.
selector:
entity:
domain: climate
open_duration:
name: Open Duration
description: How long a door or window must stay open before turning off the heating.
# FIXED: proper duration object, not a string
default:
hours: 0
minutes: 10
seconds: 0
selector:
duration:
auto_control_flag:
name: Automation Control Flag (input_boolean)
description: >
An input_boolean helper that this automation uses to remember
that it turned the heating off, so it knows whether to turn it
back on later.
selector:
entity:
domain: input_boolean
previous_hvac_mode:
name: Previous HVAC Mode (input_text)
description: >
An input_text helper where the automation stores the previous
HVAC mode (e.g., heat, auto, cool) before turning the climate off.
selector:
entity:
domain: input_text
variables:
open_sensors_list: !input open_sensors
climate_id: !input climate_entity
auto_flag_helper: !input auto_control_flag
prev_mode_helper: !input previous_hvac_mode
trigger:
# Any monitored sensor stays open for the configured duration
- id: opened
platform: state
entity_id: !input open_sensors
from: "off"
to: "on"
for: !input open_duration
# Any monitored sensor closes
- id: closed
platform: state
entity_id: !input open_sensors
from: "on"
to: "off"
condition: []
action:
- choose:
# ---- CASE 1: A door/window stayed open -> turn heating OFF ----
- conditions:
- condition: trigger
id: opened
sequence:
# Only act if the climate is currently not off
- condition: template
value_template: "{{ states(climate_id) != 'off' }}"
# Store the current HVAC mode in the input_text helper
- service: input_text.set_value
target:
entity_id: !input previous_hvac_mode
data:
value: "{{ states(climate_id) }}"
# Remember that this automation turned it off
- service: input_boolean.turn_on
target:
entity_id: !input auto_control_flag
# Turn the climate off
- service: climate.set_hvac_mode
target:
entity_id: !input climate_entity
data:
hvac_mode: "off"
# ---- CASE 2: A door/window closed -> maybe turn heating back ON ----
- conditions:
- condition: trigger
id: closed
sequence:
# Only proceed if ALL monitored sensors are now closed (state == 'off')
- condition: template
value_template: >
{{ expand(open_sensors_list)
| selectattr('state','ne','off')
| list
| length == 0 }}
# Only restore if this automation previously turned it off
- condition: state
entity_id: !input auto_control_flag
state: "on"
# Restore previous HVAC mode from the input_text helper
- service: climate.set_hvac_mode
target:
entity_id: !input climate_entity
data:
hvac_mode: "{{ states(prev_mode_helper) }}"
# Clear the "I turned it off" flag
- service: input_boolean.turn_off
target:
entity_id: !input auto_control_flag
mode: single
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment