Open window sensor

i built a binary sensor for open window detection using a couple of components. the first part consists of a statistics sensor to get min and max values computed from 4 samples:

sensor:
  - platform: statistics
    name: tado kinderzimmer statistik
    entity_id: sensor.tado_kinderzimmer_temperatur
    sampling_size: 4

i then calculate the delta value of the min/max values of the thermometer in the room where i want to detect an open window:

sensor:
  - platform: template
    sensors:
      kinderzimmer_temperatur_delta:
        friendly_name: "Kinderzimmer Temperatur Delta"
        value_template: "{{ states.sensor.tado_kinderzimmer_statistik_mean.attributes.max_value | float - states.sensor.tado_kinderzimmer_statistik_mean.attributes.min_value | float }}"
        entity_id: sensor.tado_kinderzimmer_statistik_mean
        unit_of_measurement: "C"

third part is a trend sensor, to see if the temperature is falling due to an open window:

binary_sensor:
  - platform: trend
    sensors:
      kinderzimmer_temperatur_down:
        entity_id: sensor.tado_kinderzimmer_temperatur
        invert: Yes

fourth part is an automation, that notifies me of the open window, if delta is above 0.3 and the trend falling:

automation:
  alias: 'fenster kinderzimmer offen'
  trigger:
    - platform: numeric_state
      entity_id: 
        sensor.kinderzimmer_temperatur_delta
      above: 0.3
  condition:
    - condition: state
      entity_id: binary_sensor.kinderzimmer_temperatur_down
      state: 'on'
  action:
    - service: notify.notify
      data:
        title: "oops: "
        message: "fenster kinderzimmer offen"

this is rather complicated, but it works. however it takes up to 5 minutes to register the open window and it doesn’t notify if the window is closed again. registering a closed window would work the same way: if delta > 0.3 and the temperature trend is rising, it’s likely, that the window is closed again. it probably would be fairly easy to do this with app daemon, but (i guess) also with a component that would create a binary sensor of type opening.

the sensor configuration would need these parameters:

binary_sensor:
  - platform: open_window
    sensors:
      living_room_window:
        entity_id: sensor.living_room_temperature
        sampling_size: 4
        threshold: 0.3

this binary sensor would then calculate the mean value using sampling_size samples, calculate the delta and check if the trend is up or down as soon as the delta is greater than threshold.

It’s a cool idea trying to detect an open window through falling room temperature.
But why didn’t you just buy/build an open door/window sensor that uses a reed contact?
That would make things much easier and you still could include the room temperature as a factor when deciding if you want HA to send an alert.

However, the sensor you propose looks pretty useful in general as it could also be used with pretty much any other metric (light, humidity, current).
The use cases would be pretty specific, though.

Sebastian

ich have one chinese 433mhz rf window sensor that indeed works pretty well, but still you have to buy it and mount it on the window. and if you have a room with a lot of windows or doors, you’ll need quite a few sensors to detect an opening reliably. the temperature detection does not need a single additional sensor to work — if you measure the room temperature.

i gather that most people have temperature sensors in every room anyhow, so why not use the data that is already there to detect open windows? my use case is actually mostly a reminder (for my wife) not to forget opened windows. it does work with the setup described above and i can imagine that an open window binary sensor for each room would be useful for quite a lot of people that are unwilling or unable to buy and set up a sensor for each window.

the idea is actually not mine. some smart thermostat makers have this feature implemented, tado is testing it as a beta right now. so the binary open window sensor would be a useful addidion the the generic thermostat component or actually every smart heating (or cooling) device used with HA.

1 Like

…plus a reed sensor does not cover if a open window will cause any problems. During summer and open windows might differ from an open window in the winter time and here the outside temperature might also play a big game. I really love diplix approach

1 Like

@diplix just want to thank you a thousand times.
i got the tado valves and was chocked and angry that they charge a annual service fee for the open window detection. :exploding_head::face_with_symbols_over_mouth::face_with_symbols_over_mouth:
Since i was too cheap and stubborn, i started googling.
i found this thread and started putting your sensor into my config.
And everything works like a charm.
It takes around 3-5 minutes to detect a open window, and shuts off the heat in that room.
i went a step further and did make a binary_sensor called ROOM_Window_state with this template:

  - platform: template
    sensors:
      bedroom_window_state:
        friendly_name: "Bedroom window state"
        value_template: >-
          {{is_state('binary_sensor.bedroom_temperature_down', 'on') and states('sensor.bedroom_temperature_delta') | float > 0.3}}
        entity_id: sensor.bedroom_temperature_delta
        device_class: window

So now i have a sensor that show what window is open and when is it closed again.
i have a automation in node-red that shuts off the valve in the specific room when open and turn on again when closed.
i have had this setup with a lot of testing in the last couple of days and it works very well.
Just if anybody else finds this thread when searching.

it is ALOT of sensors to add to your config, but it works very good.
One again thanks to @diplix for posting the sensors.

yes, it’s kind of a suprising move to charge for open window detection and automatic away-mode. fortunately one can do all that with the HA tado integration. it’s even possible to get the tado device-tracker into HA, to use it automate the tado away mode.

i just recently added an automation that is not even doable with the anual subscription: if my wife is not home, i tell tado not to heat the living room, if only i am home. as soon as my wife moves towards home, the smart schedule is reenabled.

Hello, I was interested in this topic and I believe there is an easier way to achieve this, please let me know your thoughts.

You can handle all this just with the trend sensor, I set mine in this way:

binary_sensor:

  • platform: trend
    sensors:
    nicole_temperature_down:
    entity_id: sensor.second_floor_temperature
    sample_duration: 7200
    min_gradient: -0.001
    device_class: window

the min_gradient is measured in sensor unit per seconds, so in my case I wanted check when the temperature would drop at a rate of 0.3 in 5 minutes, therefore the formula is: -0.3 / (60seconds x 5min) = -0.001

In this way My window sensor toggle to on if the temperature in the room fall too quickly.
It isn’t really perfect when it come to set the status to off since the assumption would be that the temperature should constantly drop.

Any comment or suggestion would be appreciated.

1 Like

Hi,

your trend sensor is a good base idea to detect an open window. But it misses some critical informations. the gradient needs to be interpreted different regarding the target temperatur of the heating device and the current temperatur. Some example. If the current temp is at 20° and the target temp is 22°, the heating device is hot and will try to make the room reach 22°, so the gradient is positive. If the temp is at 20° and the target temp is 16° the heating device isbe already cold and the gradient will be already negative. Next is, that if the room temperatur is already low, the gradient will be not as negative as if the room temperatur is high, so the difference between room and outside room is also relevant…

I tested your current idea and exactly failed here. Sometime an open window was detected as false positive sometimes an open window is not detected, because the device is strong enough to equalize the open window and so on. The tado internal open window detection seems to work better, but I have no clue how it works exactly. And right now I dont see an API to read the info if a window is open or not.

Regards
Lukas

I assume this would also fail during the warmer times of the year. At least for me during summer (without any active heating) my room temperatures never go below 20°C usually. So this open window detection seems to only work when it’s cold outside. Of course this is useful when the intent of the window detection is to not forget to close them so the heating works properly. I always think about physical security in such cases.

you’re right my solution is less than perfect and produces a lot of false positives and doesn’t work reliably in the summer. however, the tado-solution produces even more unreliable results (for me). however, in theory, you can obtain those values from the web-API. when you scrape the zone state from the web API you’ll get:

{
  "activityDataPoints": {
    "heatingPower": {
      "percentage": 0.0, 
      "timestamp": "2018-12-12T13:00:58.352Z", 
      "type": "PERCENTAGE"
    }
  }, 
  "geolocationOverride": false, 
  "geolocationOverrideDisableTime": null, 
  "link": {
    "state": "ONLINE"
  }, 
  "nextScheduleChange": null, 
  "openWindow": null, 
  "overlay": {
    "setting": {
      "power": "OFF", 
      "temperature": null, 
      "type": "HEATING"
    }, 
    "termination": {
      "projectedExpiry": null, 
      "type": "MANUAL"
    }, 
    "type": "MANUAL"
  }, 
  "overlayType": "MANUAL", 
  "preparation": null, 
  "sensorDataPoints": {
    "humidity": {
      "percentage": 54.4, 
      "timestamp": "2018-12-12T13:06:43.970Z", 
      "type": "PERCENTAGE"
    }, 
    "insideTemperature": {
      "celsius": 17.57, 
      "fahrenheit": 63.63, 
      "precision": {
        "celsius": 1.0, 
        "fahrenheit": 1.0
      }, 
      "timestamp": "2018-12-12T13:06:43.970Z", 
      "type": "TEMPERATURE"
    }
  }, 
  "setting": {
    "power": "OFF", 
    "temperature": null, 
    "type": "HEATING"
  }, 
  "tadoMode": "HOME"
}

this is how i use the scraped json to show the internal tado open window sensor:

binary_sensor: 
  platform: command_line
  command: '/bin/cat /Users/ix/.homeassistant/tado/state_1.json'
  value_template: >-
    {% if value_json.openWindow.detectedTime is defined %}
      {% if value_json.openWindow != null %}
        ON
      {% else %}
        OFF
      {% endif %}
    {% else %}
      OFF
    {% endif %}
  name: tado kinderzimmer fenster
  device_class: window

spoiler alert: in my appartment tado hardly ever detects an open widow. if it works better for you, you should try and scrape the tado web API or try to extend the tado component (which reads the web-API). however, my personal solution is, that i use actual window sensors to turn off the heating or automate climate actions when windows or doors open or close.