Smart Automation Solution: Monitoring Windows and Automatically Adjusting Heating

Hi everyone,

I wanted to share a smart automation solution I recently implemented in my home. The goal was to monitor the status of windows in a room and automatically adjust the heating system based on whether the windows were open or closed. This solution helps save energy by preventing heat loss when windows are left open.

The Problem

I noticed that during colder months, if I accidentally left a window open, the heating system would continue to run, wasting energy. I wanted a way to automatically detect open windows and adjust the heating accordingly.

The Solution

I decided to use openHAB 4.x with JavaScript automation rules. Here’s how I set it up:

  1. Window Contact Sensors: I installed contact sensors on all windows in the target room. These sensors detect if a window is open or closed.
  2. Group Item: I created a group item that aggregates the status of all window sensors in the room. This group item is set to OPEN if any window is open and CLOSED only if all windows are closed.
  3. Heating Actuator Lock: I used a lock item to control the heating actuator. When the lock is enabled (ON), the heating system is turned off.
  4. Timer Mechanism: The system monitors the window status. If a window is open for more than three minutes, the heating lock is activated. If all windows are closed before the timer expires, the lock is deactivated.

Implementation Details

Rule Setup

I created a JavaScript rule for each room. Here’s an example for the living room:

javascript
“use strict”;
const { rules, triggers, items, cache } = require(‘openhab’);

rules.JSRule({
id: “LivingRoomWindowMonitoring”,
name: “Living Room Window Monitoring”,
description: “Monitors windows and adjusts heating accordingly”,
triggers: [
triggers.ItemStateUpdateTrigger(“LivingRoom_Window_Status”)
],
tags: [“Living Room”, “Automation”],
execute: (event) => {
const windowStatus = items.getItem(event.itemName);
const heatingLock = items.getItem(“LivingRoom_Heating_Lock”);

processWindowStatus(windowStatus, heatingLock);

}
});

Processing Window Status

This function handles the window status updates and manages the timer:

javascript
function processWindowStatus(windowStatus, heatingLock) {
const contextKey = windowStatus.name;

if (!cache.private.exists(contextKey)) {
cache.private.put(contextKey, {
timer: null,
oldState: null
});
}

const context = cache.private.get(contextKey);
const currentState = windowStatus.state;

if (currentState === “OPEN”) {
// Cancel any existing timer
resetTimer(context);

// Save the current heating lock state
context.oldState = heatingLock.state;

// Set a new timer for 3 minutes
const scheduledTime = time.ZonedDateTime.now().plusMinutes(3);
context.timer = actions.ScriptExecution.createTimer(scheduledTime, () => {
  switchOffHeating(heatingLock);
});

} else {
// Cancel timer if all windows are closed
resetTimer(context);

// Restore previous heating lock state if available
if (context.oldState !== null) {
  heatingLock.sendCommand(context.oldState);
  context.oldState = null;
}

}
}

function resetTimer(context) {
if (context.timer !== null) {
context.timer.cancel();
context.timer = null;
}
}

function switchOffHeating(heatingLock) {
heatingLock.sendCommand(“ON”);
console.info(“Heating system locked due to open window.”);
}

How It Works

  1. Window Opens: When a window is opened, the system starts a 3-minute timer.
  2. Timer Expires: If the window remains open after 3 minutes, the heating system is locked, preventing unnecessary heating.
  3. Window Closes: If all windows are closed before the timer expires, the heating system returns to its previous state.

Benefits

  • Energy Efficiency: Automatically prevents heat loss from open windows.
  • User-Friendly: No manual intervention required; the system runs in the background.
  • Customizable: The timer duration and logic can be easily adjusted to suit different needs.

I’m really happy with how this solution has improved energy efficiency in my home. If you have any questions or suggestions for improvement, feel free to reach out!

Best regards,
[Your Name]