Left Open Automation

I need help with a automation. I am Smarthings Hub owner and recently started playing with HASS using MQTT to setup all my devices. It work great and I like that the automation is local. So now the task is to move my automation to that is on Smartthings to HASS. I am having trouble with one particular one that alerts me if anyone of my outside doors is left open.

If any one of doors.group = open/on
then
alert after 10 minutes.
else cancel alert

I am struggling wiht doing one automation that handles all doors or do each automation for each door in a separately.

Any help would be appreciated.

I was just dealing with a similar automation yesterday; see if this thread helps you:

alias: 'Notify if any doors left open for 10 minutes'
trigger:
  platform: state
  entity_id:
    - sensor.front_door
    - sensor.back_door
    - sensor.garage_side_door
    - sensor.garage_overhead_door
  state: 'on'
  for:
    minutes: 10
action:
  service: notify.notify
  data_template:
    message: >
        The {{ trigger.to_state.attributes.friendly_name }} was left open

No need to cancel this alert as the message won’t be sent unless the door was open for the full 10 minutes. If it closes before 10 minutes, the action will never run

You can use hours, minutes or seconds in this scenario, by themselves, or as a combination:

  for:
    hours: 1
    minutes: 10
    seconds: 5
action:
  service: notify.notify
  data_template:
    message: >
        The {{ trigger.to_state.attributes.friendly_name }} was left open for 1 hour, 10 minutes and 5 seconds
4 Likes

Thanks! This is what I am looking for.

How would you do this trigger with a numeric state, e.g. a device is below a certain numeric state for at least 30 seconds?

Note for future readers:

“State:” was deprecated and removed in v. 0.50, so you need to change one line of this to make it work. Rather than “state: ‘on’” use “to: ‘on’” and all is good!

3 Likes

Hi all, I have this working using this code (thanks jbardi):

- action:
  - data_template:
      message: >
        The {{ trigger.to_state.attributes.friendly_name }} was left open
      title: Door left open
    service: notify.home_assistant_alert
  alias: Door left open
  condition: []
  id: '1518041349912'
  trigger:
  - entity_id: 
    - sensor.bed_door
    - sensor.door_2
    from: closed
    platform: state
    to: open
    for:
      minutes: 2

It works well for each door individually or if both doors are left open. However if both doors are opened and only one is closed (before 2 mins) you do not get any alerts after the 2 mins ends.

Please can someone help?

1 Like

@Simmo Have you found a workaround for this?

I have the same automation setup and the same issue (if I close one door/window after I open the initially opened one, it resets the automation and it won’t notify anymore).

@Igor_Jurisic - unfortunately not yet no.

Maybe you can try to group those sensors? But that will work only if both sensors are opened.

@Simmo @Igor_Jurisic You can solve this with binary_sensor and delay_on. Like this.

- alias: 'Notify if any doors left open'
  trigger:
    platform: state
    entity_id:
      - binary_sensor.door_one
      - binary_sensor.door_two
    to: 'on'
  action:
    service: persistent_notification.create
    data_template:
      message: "The {{ trigger.to_state.attributes.friendly_name }} was left open"
      title: "Warning"

binary_sensor:
  - platform: template
    sensors:
      door_one:
        friendly_name: "Door one"
        delay_on:
          seconds: 30
        value_template: "{{ is_state('sensor.one_open_closed', 'open') }}"
  - platform: template
    sensors:
      door_two:
        friendly_name: "Door two"
        delay_on:
          seconds: 30
        value_template: "{{ is_state('sensor.two_open_closed', 'open') }}"
2 Likes

Hi @vladosam,

Can you please explain what exactly is happening in the second part?
I’m fairly new in HA so I haven’t used “binary_sensor” yet.

I assume I can replace persistent_notification with notify.pushover, right?

Ok. Add this binary_sensor code to configuration.yaml. Replace ‘sensor.one_open_closed’ with your door sensor and ‘open’ with state of your sensor when door is open. Important part is delay_on: Change seconds to minutes: 30 if you want to be notified after 30 minutes. Yes, you can replace persistent_notification with notify.pushover.

Cool :slight_smile:

So now I just have to pile all my door / window sensors under binary_sensor one by one?
Since I have 15 sensors, is there any way to group them, to keep configuration.yaml clean?
Also, I have all of them in customize.yaml and they have friendly names defined etc., do I still have to state friendly_name for each (or is this now a separate thing)?

Thanks again!

Yup. Add them all in binary sensor. You can skip friendly_name if have them customized already. Only way to keep configuration.yaml clean is to split configuration.

1 Like

Would it be possible to enhance this rule to notify every X min?
I want a Garage Left open notification that nags me every 30 min.

UPDATE SOLVED: More research and I found this.

Adding in case someone else finds this thread and needs same feature.

1 Like

I’m doing it a bit different, and I can’t figure out if this really helps you or not, but it works well for me! (Note: huge shoutout to @Petro, who posted the template for this in another thread)

Right now I have 3 window sensors in my house.

They are put in a group together (group.yaml), like this:

all_windows:
  view: no
  entities:
    - binary_sensor.sovevaerelsevindue
    - binary_sensor.badvindue
    - binary_sensor.georgvindue

Then I have a few automations:
The first one gives me a notification on Slack, when a window has been open for 20 mins.
In automations.yaml:

  - alias: Window has been open for 20 mins
    initial_state: True
    hide_entity: False
    trigger:
      - platform: state
        entity_id: binary_sensor.sovevaerelsevindue
        to: 'on'
        for: 
          minutes: 20
      - platform: state
        entity_id: binary_sensor.badvindue
        to: 'on'
        for: 
          minutes: 20
      - platform: state
        entity_id: binary_sensor.georgvindue  
        to: 'on'
        for: 
          minutes: 20
    action:
      - service: notify.slack
        data_template:
          title: "Open windows"
          message: > 
            {% set open_windows = states | selectattr('entity_id', 'in', state_attr('group.all_windows','entity_id')) | selectattr('state','in',['on','open']) | map(attribute='name') | list %}
            {% if open_windows | length == 1 %}
              The window: "{{ trigger.from_state.attributes.friendly_name }}" has been open for 20 mins. 
            {% else %}
              The window: "{{ trigger.from_state.attributes.friendly_name }}" has been open for 20 mins. Currently open windows: {{ open_windows[:-1] | join(', ') }}{{',' if open_windows | length > 2 else ''}} and {{ open_windows[-1]}}.
            {% endif %}  

and one that notifies me when a window is closed:

  - alias: Window has been closed
    initial_state: True
    hide_entity: False
    trigger:
      - platform: state
        entity_id: binary_sensor.sovevaerelsevindue
        to: 'off'
        for: 
          seconds: 2
      - platform: state
        entity_id: binary_sensor.badvindue
        to: 'off'
        for: 
          seconds: 2
      - platform: state
        entity_id: binary_sensor.georgvindue  
        to: 'off'
        for: 
          seconds: 2
    action:
      - service: notify.slack
        data_template:
          title: "Open windows"
          message: > 
            {% set open_windows = states | selectattr('entity_id', 'in', state_attr('group.all_windows','entity_id')) | selectattr('state','in',['on','open']) | map(attribute='name') | list %}
            {% if open_windows | length == 0 %}
              All windows are closed.
            {% else %}
              The window "{{ trigger.from_state.attributes.friendly_name }}" has been closed. Currently open windows: {{ open_windows[:-1] | join(', ') }}{{', ' if open_windows | length > 2}}{{ open_windows[-1]}}.
            {% endif %}  
1 Like

@cklit Thanks, I think your version still just notifies you once, right. But I found my solution anyway and updated my post to say I found the alert component. This component works really well and keeps notifying you until you either acknowledge or fix the condition causing the alert. It’s exactly what I was looking for.

1 Like

Hi guys, thanks for the inspiration so far, but how to repeat these push notification if the windows stays open for another 10 or 20 minutes.

Managed it for me like below:

  alias: 'Fenster: Lüften Push'
  trigger:
  - entity_id: group.windows
    for: 00:20:00
    platform: state
    to: 'on'
  condition:
  - condition: state
    entity_id: group.persons
    state: home
  - below: '15'
    condition: numeric_state
    entity_id: sensor.dark_sky_temperature
  action:
  - data:
      message: '{{ states | selectattr(''entity_id'',''in'', state_attr(''group.windows'',''entity_id''))
        | selectattr(''state'',''eq'',''on'') | map(attribute=''name'') | join('',
        '') }} seit 20 Minuten geöffnet...'
      title: Fenster geöffnet...
    service: notify.push_iphones
2 Likes

Maybe you can use the alert component for this.

1 Like