Notification if window is open for too long

I would like to write an automation that reminds me when our windows are open for too long. The duration until the windows are closed should depend on the outside temperature. I have put together the following status template “open_for_x_min”:

{% set t = states('sensor.openweathermap_temperature') | float(0) %}
{% if t <= 0 %}
5
{% elif t > 0 and t <= 10 %}
10
{% else %}
15
{% endif %}

The notification should come when the windows have been open for 5, 10 or 15 minutes.
I have tried it once with an automation:

alias: window-open
description: window is open
trigger:
  - type: opened
    platform: device
    device_id: xxx
    entity_id: xxxx
    domain: binary_sensor
    for:
      hours: 0
      minutes: 0
      seconds: 0
condition: []
action:
  - delay: 00:{{ states('sensor.open_for_x_min') }}
  - action: notify.pushover
    metadata: {}
    data:
      message: close window!
      title: Office
mode: single

This works so far, but the message appears even if the window has already been closed again.
The solution seems to be the confirmation function with a status query:

condition: state
entity_id: binary_sensor.windowsensorXY
attribute: value_state
state: Open
for:
  hours: 0
  minutes: 10
  seconds: 0

However, I can’t get my variable {{ states(‘sensor.open_for_x_min’) }} that works for the delay above into the state query.
When I insert it:

alias: window-open
description: window is open
trigger:
  - type: opened
    platform: device
    device_id: xxx
    entity_id: xxxx
    domain: binary_sensor
    for:
      hours: 0
      minutes: 0
      seconds: 0
condition:
  - condition: state
    entity_id: binary_sensor.hm_sec_sco_oeq0222795
    attribute: value_state
    state: Open
    for:
      hours: 0
      minutes: {{ states('sensor.open_for_x_min') }}
      seconds: 0
action:
  - action: notify.pushover
    metadata: {}
    data:
      message: close window!
      title: Office
mode: single

I get the error:

Message malformed: expected float for dictionary value @ data['condition'][0]['for']['minutes']

After the error message, my variable in YAML is changed to:

condition: state
    entity_id: binary_sensor.hm_sec_sco_oeq0222795
    attribute: value_state
    state: Open
    for:
      hours: 0
      minutes:
        "[object Object]": null
      seconds: 0

Do you know what I need to change? Or do you have a completely different way?

I changed my approach to using an alert. It works fine with a static value for repeat, but if I want to use the value of my template I get an error.
Excerpt from configuration.yaml:

alert:
  az_offen:
    name: Fenster im Arbeitszimmer offen
    entity_id: binary_sensor.hm_sec_sco_oeqxxxxxx
    state: "on"
    repeat: {{ states('sensor.dauer_fensteroffnung') }}
    skip_first: true
    notifiers:
      - mobile_app_handy

Template-Editor says its fine:

alert:
  az_offen:
    name: Fenster im Arbeitszimmer offen
    entity_id: binary_sensor.hm_sec_sco_oeqxxxxxx
    state: "on"
    repeat: 15
    skip_first: true
    notifiers:
      - mobile_app_handy

This is the error I got when I check the configuration:

Error loading /config/configuration.yaml: invalid key: "{"states('sensor.dauer_fensteroffnung')": None}"
  in "/config/configuration.yaml", line 19, column 0```

Based on the difficulties you are encountering, and the errors in your examples, I suggest you use the following Blueprint:

If you don’t want to use a blueprint, you can examine its code for inspiration to create your own functional automation.

1 Like

This is awesome! I’m just wondering if its somehow possible to use a programmable time before alert… With that it would be possible to react to outside temperature

How do you mean? Are you wanting to change the time manually?

There are a number of ways to do what you are asking to do, and your posted YAML above does have some problems with it. What still is/is not working for you?