Echo notificaion if front foor is open and there is no motion in the hallway

I have a long hallway and 2 motion sensors on each end.
I got front door with contact sensor.

I want to make amazon echo dot notification “the front door is open” when the door has been forgotten open for more than 1 min. I was able to do that with this action:

alias: "Hallway: Front door not closed"
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.hallway_door_contact_sensor
    for:
      hours: 0
      minutes: 1
      seconds: 0
    to: "on"
condition:
  - condition: state
    entity_id: binary_sensor.hallway_motion_sensor1
    for:
      hours: 0
      minutes: 0
      seconds: 5
    enabled: false
    state: "off"
  - condition: state
    entity_id: binary_sensor.hallway_motion_sensor2
    for:
      hours: 0
      minutes: 0
      seconds: 5
    enabled: false
    state: "off"
action:
  - repeat:
      until:
        - condition: state
          entity_id: binary_sensor.hallway_door_contact_sensor
          state: "off"
          for:
            hours: 0
            minutes: 0
            seconds: 3
      sequence:
        - device_id: ad0fd90aa8dc6755e5f8fc4d9ecde791
          domain: mobile_app
          type: notify
          message: Someone forgot to close the front door
          title: "Home alert:"
        - service: notify.alexdesktop
          data:
            message: Someone forgot to close the front door
            title: "Home alert:"
        - service: notify.alexa_media_polk_react
          data:
            message: Someone forgot to close the front door
            title: "Home alert:"
        - service: notify.alexa_media_portal
          data:
            title: "Home alert:"
            message: Someone forgot to close the front door
        - service: notify.alexa_media_echo_hallway
          data:
            title: "Home alert:"
            message: Someone forgot to close the front door
        - service: notify.alexa_media_echo_livingroom
          data:
            title: "Home alert:"
            message: Someone forgot to close the front door
        - service: notify.alexa_media_echo_diningroom
          data:
            title: "Home alert:"
            message: Someone forgot to close the front door
        - service: notify.alexa_media_echo_kitchen
          data:
            message: Someone forgot to close the front door
            title: "Home alert:"
        - delay:
            hours: 0
            minutes: 1
            seconds: 0
            milliseconds: 0
mode: single

However, what I want to make now is to stop the notification IF ANY of the 2 motions sensor has motion. Once both motions sensor show “clear” NO MOTION, then the automation to run again and notify my ONLY when there is no motion in the hallway. I tried “repeat” and “if this and that” ways but no success. I put the mode to “RESTART” from single and that did not do it. Please help?

You have put this in the wrong category. It should be placed under “Configuration”, not “Community Guides”.

Use match: any in your repeat condition to allow motion detected by either sensor to cancel the loop.

alias: "Hallway: Front door not closed"
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.hallway_door_contact_sensor
    for: "00:01:00"
    to: "on"
condition:
  - condition: state
    entity_id: 
      - binary_sensor.hallway_motion_sensor1
      - binary_sensor.hallway_motion_sensor2
    for: "00:00:05"
    state: "off"
action:
  - repeat:
      until:
        - condition: state
          entity_id: 
            - binary_sensor.hallway_door_contact_sensor1
            - binary_sensor.hallway_door_contact_sensor2
          state: "on"
          match: any
...

How do you add MATCH ANY in the visual editor? I am not much of a YMAL person. I am very new to automations and HA to be honest and I could not figure out your code (where to put it and what to change). I am sorry. I want the automation to keep checking every 15 seconds if every sensor is on “clear” until BOTH motion sensor are on “clear”. In my automation if one or both sensors are showing motion, the automation just stops and is never executed.

I see OR and AND conditions. so I used AND so both are true, both motion sensors are “clear” no motion.

Here is the YAML

alias: "Hallway: Front door not closed"
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.hallway_door_contact_sensor
    for:
      hours: 0
      minutes: 1
      seconds: 0
    to: "on"
condition:
  - condition: and
    conditions:
      - condition: state
        entity_id: binary_sensor.hallway_motion_sensor1
        state: "off"
        for:
          hours: 0
          minutes: 0
          seconds: 5
      - condition: state
        entity_id: binary_sensor.hallway_motion_sensor2
        state: "off"
        for:
          hours: 0
          minutes: 0
          seconds: 5
action:
  - repeat:
      until:
        - condition: state
          entity_id: binary_sensor.hallway_door_contact_sensor
          state: "off"
          for:
            hours: 0
            minutes: 0
            seconds: 3
      sequence:
        - device_id: ad0fd90aa8dc6755e5f8fc4d9ecde791
          domain: mobile_app
          type: notify
          message: Someone forgot to close the front door
          title: "Home alert:"
        - service: notify.alexdesktop
          data:
            message: Someone forgot to close the front door
            title: "Home alert:"
        - service: notify.alexa_media_polk_react
          data:
            message: Someone forgot to close the front door
            title: "Home alert:"
        - service: notify.alexa_media_portal
          data:
            title: "Home alert:"
            message: Someone forgot to close the front door
        - service: notify.alexa_media_echo_hallway
          data:
            title: "Home alert:"
            message: Someone forgot to close the front door
        - service: notify.alexa_media_echo_livingroom
          data:
            title: "Home alert:"
            message: Someone forgot to close the front door
        - service: notify.alexa_media_echo_diningroom
          data:
            title: "Home alert:"
            message: Someone forgot to close the front door
        - service: notify.alexa_media_echo_kitchen
          data:
            message: Someone forgot to close the front door
            title: "Home alert:"
        - delay:
            hours: 0
            minutes: 1
            seconds: 0
            milliseconds: 0
mode: single

That will not do anything…

  1. They were already AND, as that is the default logic of a series of condition.
  2. The conditions in the condition block do not affect what happens in the repeat once it has started.

Also the condition in your repeat is still incorrect. To exit the until loop on motion you want to look for a state of “on” for the motion sensors.

I am confused. So how to do it, so I get notification only when the door is open for 1 min, and both sensors are “clear - no motion”. Usually when the automation stars 1 or 2 sensors are on “motion” but after 1 min they change to “clear”, but I still dont get notification. :confused:

Once both motion sensors change to “clear” I want to keep receiving notificaitons (every 3 seconds in my case) Until someone close the door.

Since you are using a repeat until, you should always get 1 round of notifications as long as the conditions in the condition block are satisfied. If you aren’t getting any notifications that point to a problem with the conditions in the condition block. You may need to set the for in your trigger longer than 1 minute.

That’s okay, but it is the most efficient and accurate way for us to share and debug automations and scripts. I recommend taking a look a this video by @ResinChem. It’s a little dated, but it is a good primer on translating automation editor automations to YAML and vice versa.

alias: "Hallway: Front door not closed"
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.hallway_door_contact_sensor
    for:
      hours: 0
      minutes: 1
      seconds: 30
    to: "on"
condition:
  - condition: state
    entity_id: binary_sensor.hallway_motion_sensor1
    state: "off"
    for:
      hours: 0
      minutes: 0
      seconds: 5
  - condition: state
    entity_id: binary_sensor.hallway_motion_sensor2
    state: "off"
    for:
      hours: 0
      minutes: 0
      seconds: 5
action:
  - repeat:
      until:
        - alias: Exit loop when door is closed or either motion sensor detects motion
          condition: or
          conditions:
            - condition: state
              entity_id: binary_sensor.hallway_door_contact_sensor
              state: "off"
              for: "00:00:03"
            - condition: state
              entity_id: binary_sensor.hallway_motion_sensor1
              state: "on"
            - condition: state
              entity_id: binary_sensor.hallway_motion_sensor2
              state: "on"
      sequence:
        - device_id: ad0fd90aa8dc6755e5f8fc4d9ecde791
          domain: mobile_app
          type: notify
          message: Someone forgot to close the front door
          title: "Home alert:"
        - service: notify.alexdesktop
          data:
            message: Someone forgot to close the front door
            title: "Home alert:"
        - service: notify.alexa_media_polk_react
          data:
            message: Someone forgot to close the front door
            title: "Home alert:"
        - service: notify.alexa_media_portal
          data:
            title: "Home alert:"
            message: Someone forgot to close the front door
        - service: notify.alexa_media_echo_hallway
          data:
            title: "Home alert:"
            message: Someone forgot to close the front door
        - service: notify.alexa_media_echo_livingroom
          data:
            title: "Home alert:"
            message: Someone forgot to close the front door
        - service: notify.alexa_media_echo_diningroom
          data:
            title: "Home alert:"
            message: Someone forgot to close the front door
        - service: notify.alexa_media_echo_kitchen
          data:
            message: Someone forgot to close the front door
            title: "Home alert:"
        - delay: 15
mode: single

You can probably simplify your action block a lot by using different service calls. If you are notifying all your echos devices you can use:

- service: notify.alexa_media_everywhere
  data:
    message: Someone forgot to close the front door

To notify multiple echos but not all of them you can list multiple targets as follows:

- service: notify.alexa_media_everywhere
  data:
    message: Someone forgot to close the front door
    target: 
      - media_player.echo_livingroom
      - media_player.echo_kitchen
      - media_player.echo_diningroom
      - media_player.echo_hallway

EDIT: Corrected indentation error

Thank you for all your help. I will watch the video tomorrow as now its too late and my brain is sleeping. I copied the YAML code from your last replay and paste it into my automation but nothing happened. There is no SAVE button while in YAML mode, only in UI. I then created new automation empty one, and tried to paste it there as it had SAVE button but nothing happed again. How am I supposed to save YAML modifications? Only UI shows SAVE. Is it a bug?

You should see a “Save” button…

There was an indentation error in one of the conditions, I have corrected it above.

Yes, there is SAVE button now and it did work. However, the behavior of the automation is not what I wanted. Right now with this automation, after 1 min 30 sec I keep getting notification (not matter if the motions sensor are detecting motion or are clear. Anyways, I guess what I want is not possible.

Basically, I guess what I want is this. If trigger fires automations, and conditions kills it, the automation stops and never run agian, until the same trigger fires it again, correct?

Is there a way to keep runing the automation after period of time? Example.

  1. Door sensor is open - trigger fires
  2. Condition: Motion sensor must NOT detect motion
  3. Action triggered.

In this case in 2 (the condition) stops the action when there is motion. Is there a way to start the automation again after 15 sec without the door being open (since is still open). And if this time the sensor DOES NOT detect motion, to fire the action?

Basically to start automation once again, after is being halted by condition without being triggered by a trigger.

I am really sorry to be annoying and probably dumb but I am noob and trying to learn. I am very appreciative of all your time and help, I really do! Thank you.

It’s much easier if you can reformulate the problem to allow all of the evaluations to be “instant”, rather than waiting or repeatedly checking something. A common pattern to use for checking multiple things is to trigger off any of the things; and have conditions for all of the things. That way, the action runs only when all the conditions are met, but it doesn’t matter in what sequence / order they are met.

Would it work if you did this?

  • Triggers on:
    • door being open for one minute; or
    • any motion sensor being clear for one minute
  • Condition that:
    • door is open; and
    • door state has not changed for at least a minute; and
    • both motion sensors are “clear”; and
    • both motion sensors have not changed for at least a minute
- alias: "Hallway: front door not closed"
  id: 0df31413-5b35-4e20-898a-68bbce98e25c
  trigger:
    - platform: state
      entity_id: binary_sensor.hallway_door_contact_sensor
      to: 'on'
      for:
        minutes: 1
    - platform: state
      entity_id: 
        - binary_sensor.hallway_motion_sensor1
        - binary_sensor.hallway_motion_sensor2
      to: 'off'
      for:
        minutes: 1
  condition:
    - condition: state
      entity_id: binary_sensor.hallway_door_contact_sensor
      state: 'on'
    - "{{ now()|as_timestamp - states.binary_sensor.hallway_door_contact_sensor.last_changed|as_timestamp(0) >= 60}}"
    - condition: state
      entity_id: 
        - binary_sensor.hallway_motion_sensor1
        - binary_sensor.hallway_motion_sensor2
      state: 'off'
    - "{{ now()|as_timestamp - states.binary_sensor.hallway_motion_sensor1.last_changed|as_timestamp(0) >= 60}}"
    - "{{ now()|as_timestamp - states.binary_sensor.hallway_motion_sensor2.last_changed|as_timestamp(0) >= 60}}"
  action:
    # all your notifications

You won’t be able to do the template conditions in the UI directly — you might be able to do then via Edit as YAML.

If you wanted to repeat the warning periodically, you could add an additional trigger like this:

  - platform: state
    entity_id: automation.hallway_front_door_not_closed
    attribute: last_triggered
    for:
      minutes: 2

I tried implementing your way but I do not get SAVE button. There must be an error. Can you please past the entire code? thank you.

The UI Edit As YAML needs different indentation to the manual files I use. Try this:

alias: "Hallway: front door not closed"
trigger:
  - platform: state
    entity_id: binary_sensor.hallway_door_contact_sensor
    to: 'on'
    for:
      minutes: 1
  - platform: state
    entity_id: 
      - binary_sensor.hallway_motion_sensor1
      - binary_sensor.hallway_motion_sensor2
    to: 'off'
    for:
      minutes: 1
condition:
  - condition: state
    entity_id: binary_sensor.hallway_door_contact_sensor
    state: 'on'
  - "{{ now()|as_timestamp - states.binary_sensor.hallway_door_contact_sensor.last_changed|as_timestamp(0) >= 60}}"
  - condition: state
    entity_id: 
      - binary_sensor.hallway_motion_sensor1
      - binary_sensor.hallway_motion_sensor2
    state: 'off'
  - "{{ now()|as_timestamp - states.binary_sensor.hallway_motion_sensor1.last_changed|as_timestamp(0) >= 60}}"
  - "{{ now()|as_timestamp - states.binary_sensor.hallway_motion_sensor2.last_changed|as_timestamp(0) >= 60}}"
action:

…plus the rest of your action.

Yes, it was working but it triggers the first time after about 2 min 30 sec. I change the Trigger values and stopped working.

alias: "Hallway: front door not closed (FIXED2)"
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.hallway_door_contact_sensor
    to: "on"
    for:
      hours: 0
      minutes: 1
      seconds: 30
  - platform: state
    entity_id:
      - binary_sensor.hallway_motion_sensor1
      - binary_sensor.hallway_motion_sensor2
    to: "off"
    for:
      hours: 0
      minutes: 0
      seconds: 0
condition:
  - condition: state
    entity_id: binary_sensor.hallway_door_contact_sensor
    state: "on"
  - >-
    {{ now()|as_timestamp -
    states.binary_sensor.hallway_door_contact_sensor.last_changed|as_timestamp(0)
    >= 60}}
  - condition: state
    entity_id:
      - binary_sensor.hallway_motion_sensor1
      - binary_sensor.hallway_motion_sensor2
    state: "off"
  - >-
    {{ now()|as_timestamp -
    states.binary_sensor.hallway_motion_sensor1.last_changed|as_timestamp(0) >=
    60}}
  - >-
    {{ now()|as_timestamp -
    states.binary_sensor.hallway_motion_sensor2.last_changed|as_timestamp(0) >=
    60}}
action:

I tried open door trigger in 1 min and sensors at 0 (immediately), but then stopped working

Basically it was working now as I would like, but instead of 2 min 30/40 sec I want to trigger at 1 min sharp.

It sounds like your motion sensor has a delayed-off: my code will trigger at exactly one minute after the motion sensor goes clear.

If you alter the trigger you must also change the conditions. The last two conditions block the action happening unless the motion sensors have been unchanged for 60 seconds. If you change the trigger to 0 you should remove those last two conditions.

LIKE THIS ?

condition:
  - condition: state
    entity_id: binary_sensor.hallway_door_contact_sensor
    state: "on"
  - >-
    {{ now()|as_timestamp -
    states.binary_sensor.hallway_door_contact_sensor.last_changed|as_timestamp(0)
    >= 60}}
  - condition: state
    entity_id:
      - binary_sensor.hallway_motion_sensor1
      - binary_sensor.hallway_motion_sensor2
    state: "off"
  - >-
    {{ now()|as_timestamp -
    states.binary_sensor.hallway_motion_sensor1.last_changed|as_timestamp(0) >=
    0}}
  - >-
    {{ now()|as_timestamp -
    states.binary_sensor.hallway_motion_sensor2.last_changed|as_timestamp(0) >=
    0}}
action:

I said remove the last two conditions — what you have done is pointless but will probably still work.

You do need to add your actions to the code, of course…

i am sorry for not understanding you well. What I did, not not change anything to be honest. The notifications still execute at 2:30 or so. I guess I will leave it like that. I guess it cannot be faster.

I do add my actions, that I am aware of. Thank you for all your support and help!

Have a look at the automation traces to see why and when it triggers.