If/Then Statement for lights on and off

Hi all,

I’m probably just missing something silly. I have an August lock on my front door, stairs to my door, and then a door sensor at the top entrance. The lights turn on fine, but when it’s running my script to turn off something whacky happens (I’m not positive where). The idea is when either the august is locked or the top door is closed to wait 10 minutes and check to see if both the lock is still locked and the door is still closed and if so, turn off the lights. It seems to work sometimes, but others not. I have a feeling it’s in my if/then but I can’t figure out where. Help? Thank you!

alias: Stairway Lights OFF
description: ""
trigger:
  - platform: device
    device_id: 13268d8ad9898c7ac224996908fce4bf
    domain: lock
    entity_id: lock.front_door
    type: locked
    for:
      hours: 0
      minutes: 0
      seconds: 0
  - platform: state
    entity_id:
      - binary_sensor.entryway_sensor_contact
    from: "on"
    to: "off"
    for:
      hours: 0
      minutes: 0
      seconds: 0
condition: []
action:
  - delay:
      hours: 0
      minutes: 10
      seconds: 0
      milliseconds: 0
  - if:
      - condition: state
        entity_id: lock.front_door
        state: locked
      - condition: and
        conditions:
          - condition: state
            entity_id: binary_sensor.entryway_sensor_contact
            state: "off"
    then:
      - service: notify.mobile_app_iphone
        data:
          message: >-
            Stairway lights will turn off in 5 minutes without further
            interaction
      - delay:
          hours: 0
          minutes: 5
          seconds: 0
          milliseconds: 0
      - service: scene.turn_on
        target:
          entity_id: scene.stairway_lights_off
        metadata: {}
    else:
      - repeat:
          until:
            - condition: state
              entity_id: lock.front_door
              state: locked
            - condition: and
              conditions:
                - condition: state
                  entity_id: binary_sensor.entryway_sensor_contact
                  state: "off"
          sequence: []
mode: single

The overall structure is a bit confusing. Why use an (unreliable) delay when you could just use conditions to insure both the lock and door are in the desired state before executing the actions?

Your repeat doesn’t have any actions in its action sequence.

Your And conditions are not actually doing anything. The inherent logic of conditions is “AND” so the “And” condition type is only needed in when being used inside a Not or Or condition. When using And, Not, and Or conditions the conditions to which the logic is being applied all need to be nested under the parent logical condition.

You may want to switch to an Actionable Notification so that you can just accept or cancel the action from the notification.

I appreciate your reply, This was more of a “cheat YAML”, as I built the automation in the visual editor and then changed over to the YAML. I’m still very new to all of this and I’m learning as I go. My goal is to be able to write YAML proficiently for each action. Thanks!