Skip to content

Instantly share code, notes, and snippets.

@mdziekon
Last active October 27, 2022 00:41
Show Gist options
  • Save mdziekon/53abfacf3430cc08b28382dddf6fe3f4 to your computer and use it in GitHub Desktop.
Save mdziekon/53abfacf3430cc08b28382dddf6fe3f4 to your computer and use it in GitHub Desktop.
Smart Thermostat Controller with Window open/close detection. Persists previous thermostat target temperature and later restores it from a variable.
blueprint:
name: Smart Thermostat Controller - Set target temperature
description:
A script to set the target temperature on a thermostat device,
which works with the rest of "Smart Thermostat Controller" ecosystem.
Namely, when a thermostat is in "paused state" (eg. a window has been opened),
instead of blindly setting the target, it will store the target value to be later restored
when normal operation is resumed (eg. a window has been closed).
When a thermostat is in "normal state" though, it will simply set the target temperature directly.
domain: script
input:
climate_device_attribute:
name: Climate / Thermostat target temperature attribute
description:
The device's "target temperature" attribute name.
default: "current_heating_setpoint"
selector:
text:
source_url: https://gist.github.com/mdziekon/53abfacf3430cc08b28382dddf6fe3f4
variables:
climate_device_attribute: !input climate_device_attribute
fields:
entity_id:
name: Entities
selector:
entity:
domain: climate
multiple: true
input_target_value:
name: Target temperature value
required: true
selector:
number:
min: 0
max: 100
step: 0.5
unit_of_measurement: °C
mode: slider
sequence:
- alias: For each passed entity
repeat:
for_each: "{{ entity_id }}"
sequence:
- alias: Setup iteration variables
variables:
climate_device_entity: "{{ repeat.item }}"
climate_device_attribute: current_heating_setpoint
restore_target_temp_value_storage_entity: >-
{{ (climate_device_entity | replace('climate.', 'input_number.'))
+ '_restore_target_temp_storage' }}
- alias: Determine action
continue_on_error: true
choose:
- conditions:
- condition: template
value_template: >-
{{ (states(restore_target_temp_value_storage_entity) | is_number)
== false }}
sequence:
- service: system_log.write
data:
message: "{{ 'Ignoring entity ' + climate_device_entity + ' because restore_target_temp_value_storage_entity does not exist' }}"
level: warning
- conditions:
- condition: template
value_template: >-
{{ (states(restore_target_temp_value_storage_entity) | int)
== -1 }}
sequence:
alias: Set target temperature directly
service: climate.set_temperature
target:
entity_id:
- "{{ climate_device_entity }}"
data:
temperature: "{{ input_target_value }}"
default:
- alias: Persist desired target temperature for later
service: input_number.set_value
data:
entity_id:
- "{{ restore_target_temp_value_storage_entity }}"
value: "{{ input_target_value }}"
mode: single
blueprint:
name: Smart Thermostat Controller - Window detection
description:
An automation to automatically reduce the thermostat's temperature (switch off)
when a contact sensor (eg. window) is opened, and automatically restore it
once the contact is closed.
Designed for devices which do not have ON/OFF toggles, but use "target temperature" instead.
domain: automation
input:
contact_sensor_entity:
name: Contact sensor(s)
description:
The contact sensor(s) that should be tied to the thermostat.
For multiple entities, use a group helper without "all entities" option NOT selected.
The entity has to be in "binary sensor" domain, and can be of any device class (eg. contact sensor presented as "door" representing a balcony door).
selector:
entity:
domain: binary_sensor
contact_sensor_on_open_delay:
name: Contact sensor open delay time
description:
How much time should the automation wait before acting upon contact sensor open event.
default: 10
selector:
number:
min: 0.0
max: 600.0
unit_of_measurement: seconds
mode: slider
step: 1.0
climate_entity:
name: Climate / Thermostat device
description:
The climate device, representing thermostats to be controlled by this automation.
selector:
entity:
domain: climate
climate_device_attribute:
name: Climate / Thermostat target temperature attribute
description:
The device's "target temperature" attribute name.
default: "current_heating_setpoint"
selector:
text:
restore_target_temp_value_storage_entity:
name:
Helper "restore temperature" storage entity
description:
Helper input number, used to store the "previous" thermostat value to be restored once contact sensor is closed.
Used instead of a local automation variable to enable other scripts & automations to change the target temperature during the "heating paused" state
(eg. when the window is open, but in the meantime someone has changed the target temperature to a different value;
this allows this automation to restore target temp to the new value once the window is closed).
The helper should accept values starting from -1 (which is used to represent "non paused" state).
selector:
entity:
domain: input_number
is_enabled_entity:
name: Additional enablement entity [optional]
description:
Optional entity-based condition for this automation to run.
Eg. "winter-mode" representing entity.
default:
selector:
entity:
domain: input_boolean
source_url: https://gist.github.com/mdziekon/53abfacf3430cc08b28382dddf6fe3f4
variables:
is_enabled_entity: !input is_enabled_entity
climate_device_entity: !input climate_entity
climate_device_attribute: !input climate_device_attribute
restore_target_temp_value_storage_entity: !input restore_target_temp_value_storage_entity
contact_sensor_on_open_delay: !input contact_sensor_on_open_delay
trigger:
- platform: state
entity_id: !input "contact_sensor_entity"
to: "on"
for: !input "contact_sensor_on_open_delay"
condition:
- condition: template
value_template: "{{ (is_enabled_entity == none) or (states(is_enabled_entity) == 'on') }}"
action:
- alias: "Persist current target temperature"
service: input_number.set_value
data:
entity_id:
- "{{ restore_target_temp_value_storage_entity }}"
value: "{{ state_attr(climate_device_entity, climate_device_attribute) }}"
- alias: "Switch off thermostat"
service: climate.set_temperature
target:
entity_id:
- "{{ climate_device_entity }}"
data:
temperature: 5
- alias: "Wait for contact sensor to be closed"
wait_for_trigger:
- platform: state
entity_id: !input "contact_sensor_entity"
to: "off"
continue_on_timeout: false
- alias: "Switch on thermostat and restore target temperature value"
service: climate.set_temperature
target:
entity_id:
- "{{ climate_device_entity }}"
data:
temperature: "{{ states(restore_target_temp_value_storage_entity) }}"
- alias: "Cleanup restore value storage"
service: input_number.set_value
data:
entity_id:
- "{{ restore_target_temp_value_storage_entity }}"
value: -1
mode: restart
max_exceeded: silent
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment