Motion detection on/off mistake

Hi I made an automation with a motion detection that put the lights on or off.
It works but now and then the light goes on and immediate goes off again.
So it is like the automation is doing the 2 triggers after each other. => on =>off.

What did I wrong in my automation?

alias: Motion inkom licht aan/uit
description: ""
triggers:
  - trigger: state
    entity_id:
      - sensor.mw_inkom
    to: Detected
    id: inkom aan
  - trigger: state
    entity_id:
      - sensor.mw_inkom
    to: Clear
    id: inkom uit
    for:
      hours: 0
      minutes: 1
      seconds: 0
conditions: []
actions:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - inkom aan
          - condition: sun
            before: sunrise
            before_offset: "+02:00:00"
            after: sunset
            after_offset: "-01:00:00"
        sequence:
          - type: turn_on
            device_id: 4d9e1e905b3f2ca86c6aa19938ac38d6
            entity_id: da7729a830c90b7e7be8fc6ef04e3b62
            domain: switch
      - conditions:
          - condition: trigger
            id:
              - inkom uit
        sequence:
          - type: turn_off
            device_id: 4d9e1e905b3f2ca86c6aa19938ac38d6
            entity_id: da7729a830c90b7e7be8fc6ef04e3b62
            domain: switch
mode: single

Thanks, I have a look

My detection returns Detected or Clear and not on/off, what do I have to change extra to let that to work?

alias: Motion inkom licht aan/uit
description: ""
triggers:
  - entity_id:
      - sensor.mw_inkom
    to: Detected
    trigger: state
  - entity_id:
      - sensor.mw_inkom
    to: Clear
    for:
      minutes: 1
    trigger: state
actions:
  - target:
      device_id: 4d9e1e905b3f2ca86c6aa19938ac38d6
      entity_id: da7729a830c90b7e7be8fc6ef04e3b62
    action: light.turn_{{ trigger.to_state.state }}
mode: single

The approach in both methods is more or less the same, so that is not your problem. Rewriting it to use your states is possible but Id stick to your old approach. Have you looked at the automation traces and the log to see what caused the off?

I also would advise to steer clear of device actions/triggers/conditions. Stick to light.turn_on and …off with just entity ids.

Just FYI:
On the dashboard it will show detected and clear (as the sensor has class motion).
But under the hood (yaml), the motion sensor is ‘just’ another binary sensor
and will use on and off

So while the visual editor shows:


It’s yaml will show: