If you’re into smart homes, chances are you’re also into adding more sensors. The more sensors, the more data. And the more data, the more you can analyze and automate.
Some time ago, I ordered a few new sensors and expanded my smart home setup. I bought three Aqara door and window contact sensors. These allow me to monitor the open/closed state of windows and doors and trigger actions based on these events. I got them because I wanted to extend the automation of our smart radiator thermostats.
The Thermostats
First, a quick word about the thermostats: We have three Tado radiator thermostats—one in the bathroom, one in the home office, and one in the living room. These are the rooms where we use them the most. If we ever find more thermostats on sale, we might equip the other rooms too, but they are quite expensive.
The Tado thermostats are connected to the Tado Bridge, and through the Tado integration, they are also linked to Home Assistant, which is responsible for the entire smart home control. I’ve set up a simple heating dashboard in Home Assistant that allows us to control the thermostats. We also have a hardware button in the corridor that, when pressed, turns all heaters to full power—perfect for quickly warming up the apartment when coming home on a cold day. We don’t use the Tado app at all anymore, except for configuration work from my side.
Tado does offer a feature to detect open windows. If I understand correctly, it works by checking if the thermostat detects a rapid temperature drop. Then, you get a notification in the Tado app asking if you want to turn off the heating. However, from my experience this is pretty unreliable and rarely detects open windows correctly.
That’s why I had the idea to solve this via Home Assistant automation. The new Aqara sensors provide reliable data on whether the window is open or closed, and the heating should be controlled accordingly.
In the end, this became one of first and most reliable automations I’ve implemented in Home Assistant. We never heat against an open window again—so I’d call that a complete success.
The Home Assistant Automation
Here’s a breakdown of the automation in Home Assistant, using the bathroom as an example. I could probably turn this into a template at some point, but for now, I’ve just copied the automation for each room.
The automation is split into two main parts: triggers, which start the automation, and actions, which execute when the automation is triggered.
Triggers
triggers:
- entity_id:
- binary_sensor.aqara_sensor_magnet_bathroom_window_contact
to: "on"
trigger: state
- entity_id:
- binary_sensor.aqara_sensor_magnet_bathroom_window_contact
to: "off"
trigger: state
- device_id: b1410c676788c5c0bb64f6616c0920eb
domain: climate
entity_id: d6059d4efc04f29257410d523b6819ae
type: hvac_mode_changed
trigger: device
to: heat
There are three different triggers:
- The window is opened.
- The window is closed.
- The heating is turned on.
Actions
- choose:
- conditions:
- condition: state
entity_id: binary_sensor.aqara_sensor_magnet_bathroom_window_contact
state: "on"
sequence:
...
- conditions:
- condition: state
entity_id: binary_sensor.aqara_sensor_magnet_bathroom_window_contact
state: "off"
sequence:
...
The actions are split into two sequences based on whether the window was opened or closed.
If the window is opened:
sequence:
- if:
- condition: state
entity_id: climate.bathroom
state: heat
then:
- target:
entity_id: climate.bathroom
action: climate.turn_off
data: {}
- action: input_boolean.turn_on
data: {}
target:
entity_id:
- input_boolean.heating_state_bathroom
The heating is only turned off if it was actively heating. At the same time, a variable (heating_state_bathroom
) is set to remember that the heating was running before turning it off. This way, the system knows whether to turn the heating back on when the window is closed.
If the window is closed:
sequence:
- if:
- condition: state
entity_id: input_boolean.heating_state_bathroom
state: "on"
then:
- action: climate.turn_on
data: {}
target:
area_id: bathroom
- action: input_boolean.turn_off
data: {}
target:
entity_id: input_boolean.heating_state_bathroom
Here, the variable heating_state_bathroom
is reset after the heating is reactivated—if the variable was set to true
. Technically, the name of this variable is a bit misleading, as it doesn’t store the general heating state of the bathroom but rather only whether the heating was turned off due to the window automation.
What’s Missing? What’s Next?
In theory, it would be nice to have more options for how the heating should behave when the window is closed again. Maybe I don’t want it to turn back on if it’s already 2 AM. Right now, the heating just turns on as soon as the window is closed.
Since the heating control in Home Assistant shows the heating as “off” in this case, you can’t manually turn it off either. From a UX perspective, I’m still missing a proper solution for this problem. That being said, we’ve almost never encountered a situation where this behavior was an issue.
Also, I have a nighttime automation running that regularly checks if any heating is still on and turns it off if necessary. So, if the heating were mistakenly turned back on, it would be shut down again later anyway.
But that automation is a topic for another post.