How to fix this light automation

Hi,

I hope someone can help me with my light automation. It’s almost good, i think.

alias: 'Motion: Gang 1e'
description: ''
trigger:
  - platform: state
    entity_id: binary_sensor.overloop_1e_motion
    to: 'on'
    from: 'off'
  - platform: state
    entity_id: binary_sensor.trap_beneden_motion
    to: 'on'
    from: 'off'
condition: []
action:
  - service: light.turn_on
    target:
      entity_id: light.gang_1e_verdieping
    data:
      brightness_pct: '{{ state_attr("switch.adaptive_lighting_gang_1e", "brightness_pct")}}'
      rgb_color:
        - '{{ (range(0, 255)|random) }}'
        - '{{ (range(0, 255)|random) }}'
        - '{{ (range(0, 255)|random) }}'
  - service: light.turn_on
    target:
      entity_id: light.gang_1e_verdieping_2
    data:
      brightness_pct: '{{ state_attr("switch.adaptive_lighting_gang_1e", "brightness_pct")}}'
      rgb_color:
        - '{{ (range(0, 255)|random) }}'
        - '{{ (range(0, 255)|random) }}'
        - '{{ (range(0, 255)|random) }}'
  - service: light.turn_on
    target:
      entity_id: light.gang_1e_verdieping_3
    data:
      brightness_pct: '{{ state_attr("switch.adaptive_lighting_gang_1e", "brightness_pct")}}'
      rgb_color:
        - '{{ (range(0, 255)|random) }}'
        - '{{ (range(0, 255)|random) }}'
        - '{{ (range(0, 255)|random) }}'
  - delay:
      hours: 0
      minutes: 2
      seconds: 0
      milliseconds: 0
  - service: light.turn_off
    data:
      transition: 1
    target:
      entity_id:
        - light.gang_1e_verdieping
        - light.gang_1e_verdieping_2
        - light.gang_1e_verdieping_3
mode: restart

So, this automation triggers 3 lights based on 2 motion sensors and gives the lights a random color and the brightness of the adaptive lighting sensor. That works. The issue : If the motion sensor turns on, you walk by, and 3 seconds later you walk by again, the motion sensor again flips from off to on and thus triggering this automation again, changing the lights colors. I thought to be smart to add a condition : If lights are off, then run the automation, but that breaks the entire flow because it’s in restart mode, so than the condition fails and the lights are never turned off.
Any tips?

why don’t you either change the mode to something other than restart or you could add a condition that the automation hasn’t been triggered for x amount of time before it can run the actions again?

I have two motion sensors and 1 switch to trigger basement bar lighting. This is how I did it.

  alias: Bar Lights - Turn on and off based on stairwell light and motion sensors.
  description: Turn on and off all the lights in the bar area by motion or switch
  trigger:
    - id: 'bar_on'
      platform: state
      entity_id: binary_sensor.bar_stairwell_motion_sensor_motion
      from: 'off'
      to: 'on'
    - id: 'bar_on'
      platform: state
      entity_id: binary_sensor.bar_main_motion_sensor_motion
      from: 'off'
      to: 'on'
    - id: 'bar_on'
      platform: state
      entity_id: switch.stairwell_light
      from: 'off'
      to: 'on'
    - id: 'bar_off'
      platform: state
      entity_id: binary_sensor.bar_main_motion_sensor_motion
      from: 'on'
      to: 'off'
      for: '00:01:30'
    - id: 'bar_off'
      platform: state
      entity_id: switch.stairwell_light
      from: 'on'
      to: 'off'
    - id: 'bar_off1'
      platform: state
      entity_id: binary_sensor.bar_stairwell_motion_sensor_motion
      from: 'on'
      to: 'off'
      for: '00:01:30'
  condition: []
  action:
  - choose:
    - conditions:
        - "{{ trigger.id == 'bar_on' }}"
      sequence:
        - service: switch.turn_on
          target:
            entity_id:
            - switch.area_cans
            - switch.bar_pool_table_light
            - switch.rope_lights
            - switch.bar_hall_to_media
            - switch.bar_landing
            - switch.stairwell_light
        - service: light.turn_on
          target:
            entity_id:
            - light.bar_pendant_light
            - light.bar_back_bar_cans
          data:
            brightness_pct: 50
    - conditions:
        - "{{ trigger.id == 'bar_off' }}"
      sequence:
        - service: switch.turn_off
          target:
            entity_id:
            - switch.area_cans
            - switch.bar_pool_table_light
            - switch.rope_lights
            - switch.bar_hall_to_media
            - switch.bar_landing
            - switch.stairwell_light
        - service: light.turn_off
          target:
            entity_id:
            - light.bar_back_bar_cans
            - light.bar_pendant_light
    - conditions:
        - "{{ trigger.id == 'bar_off1' }}"
        - "{{ states('binary_sensor.bar_main_motion_sensor_motion') == 'off' }}"
      sequence:
        - service: switch.turn_off
          target:
            entity_id:
            - switch.area_cans
            - switch.bar_pool_table_light
            - switch.rope_lights
            - switch.bar_hall_to_media
            - switch.bar_landing
            - switch.stairwell_light
        - service: light.turn_off
          target:
            entity_id:
            - light.bar_back_bar_cans
            - light.bar_pendant_light
    default: []
  mode: single

As long as there is motion the lights will stay on. My motion detectors are shellies so there is a one minute delay automatically built in to my motion detection. If both your motion sensors are visible in the room you will need to add that condition.