Switching heaters on and off automatically

Want to control your heating thermostats automatically via door and window contacts in Home Assistant? I'll show you how to do it πŸŽ‰

Switching heaters on and off automatically
Photo by e24 / Unsplash / Image / Image

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:

- 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.

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_idand 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