Get warning when window open and cold outside

Hi guys,

i try to understand how timer work with this use case.
I have a window state which is offen/zu/gekippt (open/close/tilt).

- alias: "Warn for open windows when cold"
  trigger:
    - platform: state
      entity_id:
        - sensor.window_arbeiten
      to: "offen"
      for:
        seconds: 15
    - platform: event
      event_type: timer.finished
      event_data:
        entity_id:
          - timer.window_open_reminder_arbeiten
  condition:
    - condition: numeric_state
      entity_id: sensor.netatmo_aussen_temperature
      below: 17
  action:
    - service: notify.alexa_media
      data_template:
        target:
          - media_player.echo_wohnen
        data:
          type: announce
        message: "Das Fenster {{ trigger.to_state.name }} ist auf."
    - service: timer.start
      data_template:
        entity_id: "timer.window_open_reminder_{{ trigger.entity_id | replace('sensor.window_','') | replace('timer.window_open_reminder_','') }}"
        duration: '00:00:15'

The 15 seconds is for testing only. Later it should be 15 minutes.

I doo the replace stuff bc I would like to have one rule for many windows and respective timers.

Whats wrong with this. The timer seems not get set in the 2nd service call.

Regards
Ralf

There’s no state ‘offen’ in HA. These are translations.
Check the states of the sensor.window_arbeiten in Dev tools/states.
Don’t know if the rest will work, too lazy at the moment. :slightly_smiling_face:

I have two on/off sensors on each window. With a template sensor I create those window sensor which I use above.

- platform: template
  sensors:
    window_arbeiten:
      friendly_name: "Arbeiten"
      value_template: >
        {% if   is_state("binary_sensor.arbeiten_1",    "on") and is_state("binary_sensor.arbeiten_2",    "on") %}offen
        {% elif is_state("binary_sensor.arbeiten_1",   "on") %}gekippt
        {% else %} zu
        {% endif %}
      icon_template: >-
        {% if is_state('sensor.window_arbeiten', 'zu') %}
          mdi:window-closed-variant
        {% else %}
          mdi:window-open-variant
        {% endif %}

Ooops, sorry then. :upside_down_face:

When the trigger is the event the trigger variable does not have an entity_id field. You would have to use trigger.event.data.entity_id in that case. See available trigger data for event triggers.

EDIT:

So maybe try:

    - service: timer.start
      data_template:
        entity_id: >
          {% if trigger.platform == 'state' %}
            {% set entity_id = trigger.entity_id %}
          {% else %}
            {% set entity_id = trigger.event.data.entity_id %}
          {% endif %}
          timer.window_open_reminder_{{ entity_id.rsplit('_', 1)[1] }}
        duration: '00:00:15'

Thanks for stepping in. That helped.

Above makes sense. I implemented it, no change. Whats get me thinking is that if the above was the only problem, it should at least warn twice before the change. That was not the case.

When I manually start the timer from the debug interface in the UI the automation gets not triggered either. Something is wrong with the trigger event above.

    - platform: event
      event_type: timer.finished
      event_data:
        entity_id:
          - timer.window_open_reminder_arbeiten

I tried this

    - platform: event
      event_type: timer.finished
      event_data:
        entity_id: timer.window_open_reminder_arbeiten

This worked. I get a warning every 10 seconds.

Last problem is now how to get the friendly name of both the state trigger or the timer trigger entity.

I tried this with no luck this:

    - service: notify.alexa_media
      data_template:
        target:
          - media_player.echo_wohnen
        data:
          type: announce
        message: >-
          {% if trigger.platform == 'state' %}
            {% set entity_id = trigger.entity_id %}
          {% else %}
            {% set entity_id = trigger.event.data.entity_id %}
          {% endif %}
          Das Fenster {{ entity_id.name}} ist auf.

Anyone a hint for me?

1 Like

Yeah, the trigger did look weird (i.e., a list instead of a single item), but I didn’t focus on that. Sorry, should have said something.

{{ state_attr(entity_id, 'friendly_name') }}

Hi @rak

Do you have any update on this?

I have the same need: make an alert when windows is open and/or 1) is cold outsitde 2)air quality is bad.

I started with this:

  - id: d13baaf3-807d-4268-af0d-60c91da31eec
    alias: "Avviso finestra aperta con aria inquinata"
    mode: parallel
    initial_state: true
    trigger:
      - platform: state
        from: 'off'
        to: 'on'
        entity_id: 
          - binary_sensor.001558a99d47fc_state
          - binary_sensor.001558a99d4529_state
          - binary_sensor.001558a99d5eea_state
          - binary_sensor.finestra_cucina
          - binary_sensor.001558a99d4a8d_state
          - binary_sensor.porta_finestra_camera_di_adele
          - binary_sensor.001558a99ef4be_state
          - binary_sensor.001558a99d4e50_state
          - binary_sensor.001558a99ef07a_state
        for:
          # hours: 1
          minutes: 20
          # seconds: 5
    condition:
      - condition: template
        value_template: "{{ states('sensor.waqi_rebaudengo_torino_piemonte_italy')|int >= 101 }}"
    action:
      repeat:
        while:
          - condition: template
            value_template: "{{ repeat.index <= 6 }}"
          - condition: template
            value_template: "{{ trigger.to_state.state == 'on' }}"
          - condition: template
            value_template: "{{ now().month >= 10 or now().month <= 4 }}"
        sequence:
          - service: script.my_notify
            data_template:
              call_no_annuncio: 1 
              title: "Finestra Aperta"
              google: true
              message: "{{ state_attr(trigger.entity_id, 'friendly_name') }} Ăš aperta da piĂč di 20 minuti. L'aria a Torino oggi Ăš inquinata quindi ti suggerisco di chiuderla."
          - delay:
              minutes: 10

It does not use timers but new automations capabilities.

Hey @Jokerigno,

more often than you might think, the situation arises where you still want to leave the windows open. Then you will be flooded with (in this case unwanted) notifications. Therefore, think about the use of “alerts”. These can be acknowledged and are thus muted until the windows are closed (and automatically activated again).

Alerts do not allow complex conditions, so you use an automation to set an input_boolean (as a side effect, this makes development and testing easier). And this variable will be used as trigger for the alert.

A simplyfied example:

Setup group

group: 
  fenster_tueren:
    name: Fenster und TĂŒren
    entities:
      - binary_sensor.tuer_eingang_reed
      - binary_sensor.tuer_terrasse_reed
      - binary_sensor.tuer_balkon_reed
      - binary_sensor.fenster_wc_reed
      - binary_sensor.fenster_bad_reed
      # an so on

Setup input_boolean

input_boolean:
  fenster_tueren_offen:
    name: Fenster oder TĂŒren sind offen

Automation to trigger the input_boolean:

- alias: Fenster/TĂŒren sind offen - Statusvariable auf true setzen
  description: ''
  trigger:
    - platform: state
      entity_id: group.fenster_tueren
      to: 'on'
  condition:
    - condition: numeric_state
      entity_id: sensor.klima_garten_temperatur
      below: '20'
    #some more conditions...
  action:
    - service: input_boolean.turn_on
      data: 
        entity_id: input_boolean.fenster_tueren_offen
  mode: single

- alias: Fenster/TĂŒren sind offen - Statusvariable auf false setzen
  description: ''
  trigger:
    - platform: state
      entity_id: group.fenster_tueren
      to: 'off'
  condition: []
  action:
    - service: input_boolean.turn_off
      data: 
        entity_id: input_boolean.fenster_tueren_offen
  mode: single

The corresponding alert:

alert: 
  fenster_tueren_lange_offen:
    name: Fenster/TĂŒren sind offen
    entity_id: input_boolean.fenster_tueren_offen
    state: 'on'
    repeat:
      - 15
      - 30
    can_acknowledge: true
    skip_first: true
    message: 'Seit mindestens 15 Minuten geöffnet: {{ states | selectattr(''entity_id'',''in'', state_attr(''group.fenster_tueren'',''entity_id''))
        | selectattr(''state'',''eq'',''on'') | map(attribute=''name'') | join('',
        '') }}'
    data:
      inline_keyboard:
        - 'Quittieren:/fenster_tueren_offen_quittieren'
    notifiers:
      - telegram_gruppe

Bye, Dominik

3 Likes

That’s an interest point of view. I will test it to see if I have better luck (at the moment my automation has a bug that I cannot solve). Thank you :slight_smile:

One question: this automation is triggered when a windows is open. But If there are 2 windows open and I close just one the alert repeat is reset or not?

The group is set to “on” as soon as one or more windows or doors are opened. Only when all windows/doors are closed again does the group go back to “off”. So the alert is not reset, if you close only one of the two opened windows.

uhm ok so it means that I can receive an alert for the second window without the 15 min delay

Yes. If you open the first window and 10 minutes later the second window, you’ll get a notification after 15 minutes with both windows mentioned, although the second window is opened for just 5 minutes.