Close windows based on humidity and temperature

Hello all,

I would like to create an automation for the winter that tells me “intelligently” when to close my windows.
This should take into account both temperature and humidity.
The idea is that I close the window when the temperature in the room has dropped more than 2°C or the humidity outside is higher than in the room. I have already calculated the absolute humidity needed for this.

For the implementation I have temperature and humidity sensor and a window sensors.
I use the current version HA 2022.10.3.

Thanks for your help. :slight_smile:

1 Like

Welcome to the community forums!

You could try this:


trigger:
  - platform: state
    entity_id: sensor.room_temp
    to: null
    id: room
  - platform: state
    entity_id: sensor.humidity_outside
    to: null
    id: humidity

condition:
  - condition: state
    entity_id: binary_sensor.window
    state: "on"
  - condition: template
    value_template: |-
      {% if trigger.id == 'room' %}
        {{ diff > 2 }}
      {% else %} 
         {{ out > in }}
      {% endif %}

action:
  - service: notify.persistent_notification
    data:
      message: |-
        Please close the window.
        {% if trigger.id == 'room' %}
          The temperature has dropped by {{ diff }}°
        {% else %} 
          The humidity has risen by {{ out - in }}%
        {% endif %}

variables:
  diff: |-
    {{ trigger.from_state.state |float(0) - trigger.to_state.state |float(0) }}   
  out: "{{ states('sensor.humidity_outside') |float(0) }}"
  in: "{{ states('sensor.humidity_inside') |float(0) }}"

mode: parallel

1 Like

Hey,

thank you for your feedback.
I will try that directly on Monday. Unfortunately, I do not get before that.

Hello pedolsky,

I created the automation this morning. Unfortunately I have the problem that it shows me the following error message during the template test: “In ‘template’ condition: UndefinedError: ‘trigger’ is undefined”.
I have already tried around a bit, but can’t find the error.
The automation looks like this for me.

alias: 99-99 Test Automation
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.temperatursensor_buro
    to: "null"
    id: buro
  - platform: state
    entity_id:
      - sensor.humidity_draussen
    to: "null"
    id: humidity
condition:
  - condition: state
    entity_id: binary_sensor.fenstersensor_buero
    state: "on"
  - condition: template
    value_template: |-
      {% if trigger.id == buro %}
        {{ diff > 2 }}
      {% else %} 
         {{ out > in }}
      {% endif %}
action:
  - service: notify.mobile_app_pixel_6_pro
    data:
      message: |-
        Please close the window.
        {% if trigger.id == 'buro' %}
          The temperature has dropped by {{ diff }}°
        {% else %} 
          The humidity has risen by {{ out - in }}%
        {% endif %}
    enabled: true
variables:
  diff: >-
    {{ trigger.from_state.state |float(0) - trigger.to_state.state |float(0)
    }}   
  out: "{{ states('binary.sensor.absolute_humidity_draussen') |float(0) }}"
  in: "{{ states('binary.sensor.absolute_humidity_buro') |float(0) }}"
mode: parallel


Can you see the problem in the automation?

Thank you already :slight_smile:

You missed the colons:


`  {% if trigger.id == 'buro' %}`

Independent from the needed automation, I think it might be worth looking into the additional calculation of the dew point. Just comparing humidity without taking the temperature of the air that humidity is measured in does not create a fully reliable picture of the situation.

Just in case you are German or can understand it: the German version of make magazine had a nice article about it, including the needed formulas etc:

He is already using absolute humidity, which displays the amount of water per volume, for instancte g / m3. What does using the dewpoint add to this?

Ah, indeed, I missed the “absolute” part in the original post, you got a point there :slight_smile:

1 Like

Unfortunately, adding the colons has not changed the situation

  - condition: template
    value_template: |-
      {% if trigger.id == 'buro' %}
        {{ diff > 2 }}
      {% else %} 
         {{ out > in }}
      {% endif %}

The same error message still comes up.

Do you have a similar automation? If so, how do you do it?
I was going to look at the Heise article, but would need to sign up for it. Unfortunately, I don’t have an account for that.

Delete the quotes around null (or leave the to: field in the trigger section empty; compare with my example).

Thank you for your example.
I have some questions.
Isn’t it better to use the window contact to call the service to start an automation and run that startet automation in a loop, until the window contact state change to close?

In that case the automation would only be triggered once.

If i understand it correct, the {{ diff > 2 }} calculation means, the temp must drop by 2 celsius on each temperature change.
But my temp sensor triggers the automation on smaller temp changes.
The temp diff should be calculated from the start of window opening.
So the initial temp must be save.
Hope it’s understandable.

Post your automation code if you like.