Motion sensor to action problem

Helo. I have Philips Hue motion sensor. This sensor is able to turn ON Tuya light. I prepared automation where I would like to turn the light on after movement detection and turn it off after there is nothing moving for at least 30 seconds. This should work only 30 minutes before sunset until 30 minutes after sunrise.

Here is my automation which will turn light on but won´t turn it off. I tried many variations but without success. Please help.

alias: Motion light
description: ""
trigger:
  - type: motion
    platform: device
    device_id: c2fed70c6156d87017fd306e718c43ad
    entity_id: 20025762d69061f4471ab9e331b1bcd2
    domain: binary_sensor
condition:
  - condition: and
    conditions:
      - condition: sun
        before: sunset
        before_offset: "00:30:00"
      - condition: sun
        after: sunrise
        after_offset: "00:30:00"
action:
  - type: turn_on
    device_id: 52a50d56724524d0d71f8c8550fc0945
    entity_id: 05a4e9426a9fb4d964c42d2ceb9d300d
    domain: light
    brightness_pct: 100
  - delay:
      hours: 0
      minutes: 0
      seconds: 30
      milliseconds: 0
  - if:
      - condition: state
        entity_id: binary_sensor.pracovna_motion
        state: "off"
        for:
          hours: 0
          minutes: 0
          seconds: 30
    then:
      - type: turn_off
        device_id: 52a50d56724524d0d71f8c8550fc0945
        entity_id: 05a4e9426a9fb4d964c42d2ceb9d300d
        domain: light
    else:
      - repeat:
          sequence: []
          until:
            - condition: device
              type: is_off
              device_id: 52a50d56724524d0d71f8c8550fc0945
              entity_id: 05a4e9426a9fb4d964c42d2ceb9d300d
              domain: light
mode: restart

Hi there…

Not sure exactly where you go wrong, but i can’t happen to notice a few ‘beginner’ mistakes:

And regarding your automation, have a look at the cookbook tip abt motion activated automations:

In your case, you can add the time condition to it, which would be as simple as:

conditions:
  - condition: sun
    before: sunrise
    before_offset: "00:30:00"
    after: sunset
    after_offset: "00:30:00"

NB: for a condition goes that all of them must be true in order to work, so it is automatically and (and therefor no need to specify it)

Hello and thank you. I found a blueprint which uses light sensor which is ideal for this. I recomend blueprints because you can found plenty of them…