Automation which turns dimmer switch on is acting like toggle

I have the following automation set up to turn on a dimmer switch when motion is detected. But for some reason when it detects motion a second time it turns the dimmer switch off. Why would light.turn_on act like a toggle?

  - id: Turn on Front Light for 20min on motion
    alias: 'Motion Stairs-Front Light On 20min'
    trigger:
      platform: state
      entity_id: binary_sensor.fibaro_motion_front_motion
      to: 'on'
    action:
      - service: light.turn_on
        data:
          entity_id: light.front_dimmer
          brightness: 100
      - delay: '00:20:00'
      - service: light.turn_off
        data:
          entity_id: light.front_dimmer

If the delay is still running when the automation is called a second time it cancels the delay and continues execution.

A better option is to have two automations. One to turn the light turn on. One to turn the light turn off.

  - id: turn_on_front_light_on_motion
    alias: 'Motion Stairs-Front Light On'
    trigger:
      platform: state
      entity_id: binary_sensor.fibaro_motion_front_motion
      to: 'on'
    action:
      - service: light.turn_on
        data:
          entity_id: light.front_dimmer
          brightness: 100
  - id: turn_off_front_light_after_20_minutes
    alias: 'Motion Stairs-Front Light Off after 20 minutes'
    trigger:
      platform: state
      entity_id: binary_sensor.fibaro_motion_front_motion
      to: 'off'
      for:
        minutes: 20
    action:
      - service: light.turn_off
        data:
          entity_id: light.front_dimmer
          brightness: 100

@tom_l thanks for the explanation. I would have expected the timer to be cancelled but the remaining portion to either run before the new triggered actions or not run at all. Either would have worked. I don’t think it was the trigger that cancels the timer but rather the second timer request. So basically on first motion you get

  • light on
  • delay pending…
    on second motion you get
  • light on
  • delay pending…
    -first delay canceled
  • first turn off action taken (leaving the light off)

I was able to fix this by adding the following to my rule.

    condition:
      - condition: state
        entity_id: light.front_dimmer
        state: 'off'

This prevents the trigger from happening until the delay finishes and the light is turned off.

Your idea would have worked for the second motion but it would resulted in the second time the motion stops the light would still turn off. It would not wait 20 minutes as I wanted.

Your help still got me to a solution so many thanks!

This is not true.

      entity_id: binary_sensor.fibaro_motion_front_motion
      to: 'off'
      for:
        minutes: 20

Did you try it?

No because your explaination made me realize how to fix my single rule. I missed the for: 20 in your rule so you are correct. Your two rules would work also, sorry about that. Thanks again for your help.