Doesn't switch off if triggered manually

I have created an automation to turn off the switch when the time expires. In addition, there is a motion sensor so that each time motion is detected the countdown starts again. When I turn the switch on manually and movement is not detected, the countdown does not start and the switch does not turn off. What is my mistake? Please help.

alias: Hallway New Automation
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.ikea_tradfri_motion_sensor_motion
    from: "off"
    to: "on"
  - platform: state
    entity_id:
      - switch.sonoff_zbminil2_1_switch
    from: "off"
    to: "on"
condition: []
action:
  - if:
      - condition: and
        conditions:
          - condition: time
            after: "05:00:00"
            before: "23:00:00"
            enabled: true
          - condition: numeric_state
            entity_id: sensor.lumi_motion_ac02_1_illuminance
            below: 50
    then:
      - alias: Turn on the light
        data: {}
        action: switch.turn_on
        target:
          entity_id: switch.sonoff_zbminil2_1_switch
  - alias: Wait until there is no motion
    wait_for_trigger:
      - platform: state
        entity_id:
          - binary_sensor.ikea_tradfri_motion_sensor_motion
        from: "on"
        to: "off"
  - alias: Time to leave the light on after last motion is detected
    delay:
      hours: 0
      minutes: 3
      seconds: 0
      milliseconds: 0
  - alias: Turn off the light
    data: {}
    action: switch.turn_off
    target:
      entity_id: switch.sonoff_zbminil2_1_switch
mode: restart
max_exceeded: silent

I think your problem is the wait_for_trigger, which is waiting for the motion sensor to change to 'off' — which it’s not going to do if there’s already no motion. You could add a timeout, but this (IMHO better) rewrite should do it (obviously untested):

  • turns on immediately when motion detected if it’s daytime and dark
  • turns off when the later of these two happens:
    • no motion for 3 mins
    • light has been on for 3 mins

Re-written to my preferred structure of keeping logic and timing out of the action block. I never understand people’s aversion to using the condition block…

alias: Hallway New Automation
description: ""
trigger:
  - platform: state
    entity_id: binary_sensor.ikea_tradfri_motion_sensor_motion
    to: 'on'
    id: 'on'
  - platform: state
    entity_id: binary_sensor.ikea_tradfri_motion_sensor_motion
    to: 'off'
    for: "00:03:00"
    id: 'off'
  - platform: state
    entity_id: switch.sonoff_zbminil2_1_switch
    to: 'on'
    for: "00:03:00"
    id: 'off'
condition:
  - or:
    - and:
      - "{{ trigger.id == 'off' }}"
      - condition: state
        entity_id: binary_sensor.ikea_tradfri_motion_sensor_motion
        state: 'off'
        for: "00:03:00"
      - condition: state
        entity_id: switch.sonoff_zbminil2_1_switch
        state: 'on'
        for: "00:03:00"
    - and:
      - "{{ trigger.id == 'on' }}"
      - condition: time
        after: "05:00:00"
        before: "23:00:00"
      - condition: numeric_state
        entity_id: sensor.lumi_motion_ac02_1_illuminance
        below: 50
action:
  - action: switch.turn_{{ trigger.id }}
    target:
      entity_id: switch.sonoff_zbminil2_1_switch

Thank you.

Perhaps because it is sometimes easy to get lost in condition blocks. It is a bit more difficult to grasp perspective.