Switching heaters on and off automatically
Tags: homeassistant, smarthome, automation, automated
Published: 2023-12-22 21:03:26

![](https://blog.disane.dev/content/images/2023/12/switching_heaters-on-and-off-automatically_banner.jpeg)Want to control your heating thermostats automatically via door and window contacts in Home Assistant? I'll show you how to do it 🎉

---

Since I have now equipped my home with smart thermostats everywhere and fitted all the windows with corresponding contacts from Aqara, the question now arises as to how I can most cleverly switch the thermostats as soon as a window is opened.

## Why all this? 🤔

When I was still in the AVM ecosystem, it made sense to get the corresponding thermostats from AVM too. That also worked very well. However, the "ventilation mode" doesn't work very well with the thermostats in the old building. That's why I had to automate it via Home Assistant.

## Prerequisites 📃

For this to work, you should have radiator thermostats and corresponding door and window contacts. It doesn't matter which manufacturer, the main thing is that they are already integrated into Home Assistant.

Then you should create the corresponding rooms as "areas" in Home Assistant and assign the sensors and thermostats to these areas.

## The automation 🤖

Once everything is done, create a new automation with the following content. Please adjust the corresponding lines:

```yaml
- binary_sensor.window_bathroom
- binary_sensor.window_living_room
- binary_sensor.window_kids_room
- binary_sensor.window_bedroom
```

This list should contain your `binary_sensor` window and door contacts.

```yaml
alias: Heating off when window open
description: ""
trigger:
  - platform: state
    to:
      - "on"
      - "off"
    entity_id:
      - binary_sensor.window_bathroom
      - binary_sensor.window_living_room
      - binary_sensor.window_kinderzimmer
      - binary_sensor.window_bedroom
condition: []
action:
  - variables:
      area: "{{ area_name(trigger.entity_id) }}"
      targets: >
        {{ expand(area_entities(area)) | selectattr('domain','eq','climate') |
        map(attribute='entity_id') | join(',') }}
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ trigger.to_state.state == 'on' }}"
        sequence:
          - service: scene.create
            data:
              scene_id: thermostat_settings_{{area | lower}}
              snapshot_entities: "{{ targets }}"
          - delay: 5
          - service: climate.turn_off
            target:
              entity_id: "{{ targets }}"
          - service: notify.notify
            data:
              title: "{{ state_attr(trigger.entity_id, 'friendly_name') }} open"
              message: Heaters in {{ area }} switched off
      - conditions:
          - condition: template
            value_template: "{{ trigger.to_state.state == 'off' }}"
        sequence:
          - service: scene.turn_on
            target:
              entity_id: scene.thermostat_settings_{{area | lower}}
          - service: notify.notify
            data:
              title: "{{ state_attr(trigger.entity_id, 'friendly_name') }} closed"
              message: Heaters in {{ area }} switched on
mode: parallel
max: 10

```

The automation is actually quite simple, because it does the following:

* Variable assignment:  
   * area = The triggering area  
   * targets = All entities with the domain `climate`, return only the `entity_id`and separate with comma
* condition if the state of the entity is `on`:  
   * Execute the sequence:  
         * Create a scene to cache the current state of the thermostat  
         * Wait 5 seconds  
         * Service `climate.turn_off` with the determined entity(ies)  
         * Send notification
* Condition if the state of the entity is `off`:  
   * The sequence contains:  
         * Turning on the previously saved scene (resetting the state before opening the windows)
* Service `climate.turn_off` with the determined entity/entities
* Send notification

---

If you like my posts, it would be nice if you follow my [Blog](https://blog.disane.dev) for more tech stuff.