Need Help Automating lights with Multisensor6, NanoSwitch and Timer

Hello
I’m new to Home Assistant and for the last week I’ve been learning about how to create automations.
I’ve got a Rpi4 with a aeotec z-wave dongle which controls a multisensor 6 and a nano switch 5 all from aeotec.

The nano switch didn’t fit in the wall behind the switch so I’ve installed it in the ceiling, just before the light.
I have been trying to obtain the following scenario for my setup:

  • Light switch on OR motion detection on = Light on for x minutes
  • After that any motion detection flag/ event from sensor = restarts the same timer (extends duration).
  • And lastly a timer.finished event OR light switch off = timer.cancel and switch light off

I’ve been playing with this for a few days now and couldn’t find an example to fully understand.
Could someone please let me know what I’m doing wrong ? Or at least point me in the right direction?

Thank you.

  trigger:
  - type: motion
    platform: device
    device_id: !secret Hallway_Multisensor6_id
    entity_id: binary_sensor.multisensor_6_motion_detection
    domain: binary_sensor
  - type: no_motion
    platform: device
    device_id: !secret Hallway_Multisensor6_id
    entity_id: binary_sensor.multisensor_6_motion_detection
    domain: binary_sensor
  - platform: device
    type: turned_on
    device_id: !secret Hallway_NanoSwitch_id
    entity_id: switch.nano_switch_gen5_without_meter
    domain: switch
  - platform: device
    type: turned_off
    device_id: !secret Hallway_NanoSwitch_id
    entity_id: switch.nano_switch_gen5_without_meter
    domain: switch
  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.hallway_light
    enabled: true
  condition: []
  action:
  - choose:
    - conditions:
      - condition: or
        conditions:
        - type: is_motion
          condition: device
          device_id: !secret Hallway_Multisensor6_id
          entity_id: binary_sensor.multisensor_6_motion_detection
          domain: binary_sensor
        - condition: device
          type: is_on
          device_id: !secret Hallway_NanoSwitch_id
          entity_id: switch.nano_switch_gen5_without_meter
          domain: switch
      sequence:
      - type: turn_on
        device_id: !secret Hallway_NanoSwitch_id
        entity_id: switch.nano_switch_gen5_without_meter
        domain: switch
      - event: timer.cancel
        event_data:
          entity_id: timer.hallway_light
      - event: timer.start
        event_data:
          entity_id: timer.hallway_light
    - conditions:
      - condition: or
        conditions:
        - condition: state
          entity_id: timer.hallway_light
          state: idle
        - condition: device
          type: is_off
          device_id: !secret Hallway_NanoSwitch_id
          entity_id: switch.nano_switch_gen5_without_meter
          domain: switch
      sequence:
      - type: turn_off
        device_id: !secret Hallway_NanoSwitch_id
        entity_id: switch.nano_switch_gen5_without_meter
        domain: switch
    - conditions:
      - condition: and
        conditions:
        - condition: state
          entity_id: binary_sensor.multisensor_6_motion_detection
          state: 'off'
        - condition: device
          type: is_on
          device_id: !secret Hallway_NanoSwitch_id
          entity_id: switch.nano_switch_gen5_without_meter
          domain: switch
      sequence:
      - event: timer.start
        event_data:
          entity_id: timer.hallway_light
  mode: single