Best Practices for Syncing Scene Save and Restore with Motion Automation

I have an automation that controls outdoor lighting based on motion detection, illuminance (lux) level, and a timer. The lights should only turn on when it is sufficiently dark (lux < 15), and they will return to the previous state after a defined period (40 seconds). A new movement shall reset the timer.

I have a problem that one or more lights sometimes (couple of times every evening) have the brightness at 100. My best guess is that the states are not saved in the scene before the lights are turned on by the automation.

I also have a boolean that says when the action is in progress. (input_boolean.scene_garage_light_save_in_progress). So that the scene is not saved while the 100 brightness is on.

How can I make sure that the states are saved before going further and turn on lights?
I have, for debugging purposes, added a 4 seconds delay. But the issue still occurs.

alias: Rörelsebelysning med lux, timer och kontroll av brytare
description: ""
triggers:
  - entity_id:
      - binary_sensor.hue_outdoor_bjorken_motion
      - binary_sensor.hue_motion_sensor_2_occupancy
      - input_boolean.virtuell_rorelsesensor
    to: "on"
    trigger: state
conditions:
  - condition: numeric_state
    entity_id: sensor.hue_outdoor_bjorken_illuminance
    below: 15
actions:
  - data:
      message: Rörelse upptäckt - Automation startad
      level: info
    action: system_log.write
  - choose:
      - conditions:
          - condition: state
            entity_id: input_boolean.scene_garage_light_save_in_progress
            state: "off"
        sequence:
          - data:
              message: Scen sparas - Input_boolean är 'off'
              level: info
            action: system_log.write
          - data:
              scene_id: before_motion
              snapshot_entities:
                - light.muren
                - light.takfot_entre_both
                - light.pollare_entre
                - light.garage_tuar
                - light.garage_stralkastare
                - light.switch_entre_pollare_aqara_t1_light
            action: scene.create
          - delay: "00:00:04"
          - data:
              message: Scen 'before_motion' sparad
              level: info
            action: system_log.write
          - target:
              entity_id: input_boolean.scene_garage_light_save_in_progress
            action: input_boolean.turn_on
            data: {}
          - data:
              message: >-
                Input_boolean 'scene_garage_light_save_in_progress' satt till
                'on'
              level: info
            action: system_log.write
  - target:
      entity_id:
        - light.muren
        - light.garage_stralkastare
    data:
      brightness_pct: 100
      rgb_color:
        - 255
        - 252
        - 234
    action: light.turn_on
  - data:
      message: >
        "RGB-lampor tända: - muren: brightness {{ state_attr('light.muren',
        'brightness') }}, color {{ state_attr('light.muren', 'rgb_color') }} -
        strålkastare: brightness {{ state_attr('light.garage_stralkastare',
        'brightness') }}, color {{ state_attr('light.garage_stralkastare',
        'rgb_color') }}"
      level: info
    action: system_log.write
  - target:
      entity_id:
        - light.takfot_entre_both
        - light.pollare_entre
        - light.garage_tuar
        - light.switch_entre_pollare_aqara_t1_light
    data:
      brightness_pct: 100
      kelvin: 2200
    action: light.turn_on
  - data:
      message: >
        "Kelvin-lampor tända: {% for light in ['light.takfot_entre_both',
        'light.pollare_entre', 'light.garage_tuar',
        'light.switch_entre_pollare_aqara_t1_light'] %} - {{ light }}:
        brightness {{ state_attr(light, 'brightness') }}, color_temp {{
        state_attr(light, 'color_temp') }} {% endfor %}"
      level: info
    action: system_log.write
  - target:
      entity_id: timer.light_timer_motion_bjorken2
    data:
      duration: "00:00:40"
    action: timer.start
  - data:
      message: Timer startad eller återställd till 40 sekunder
      level: info
    action: system_log.write
  - target:
      entity_id: input_boolean.virtuell_rorelsesensor
    action: input_boolean.turn_off
    data: {}
mode: restart

alias: Rörelsebelysning - Återställ belysning efter timer
description: ""
triggers:
  - event_type: timer.finished
    event_data:
      entity_id: timer.light_timer_motion_bjorken2
    trigger: event
actions:
  - data:
      message: Timer avslutad - Startar återställning av scen
      level: info
    action: system_log.write
  - data:
      message: >
        "Lampornas tillstånd före scenåterställning: {% for light in
        ['light.muren', 'light.garage_stralkastare', 'light.takfot_entre_both',
        'light.pollare_entre', 'light.garage_tuar',
        'light.switch_entre_pollare_aqara_t1_light'] %} - {{ light }}: state {{
        states(light) }}, brightness {{ state_attr(light, 'brightness') }},
        color {{ state_attr(light, 'rgb_color') or state_attr(light,
        'color_temp') }} {% endfor %}"
    action: system_log.write
  - data:
      entity_id: scene.before_motion
    action: scene.turn_on
  - data:
      message: Scen 'before_motion' återställd
      level: info
    action: system_log.write
  - delay: "00:00:01"
  - choose:
      - conditions:
          - condition: state
            entity_id: light.garage_stralkastare
            state: "on"
        sequence:
          - data:
              message: >-
                VARNING: Strålkastaren är fortfarande tänd efter
                scenåterställning!
              level: warning
            action: system_log.write
          - choose:
              - conditions:
                  - condition: state
                    entity_id: input_boolean.motion_notification
                    state: "on"
                sequence:
                  - action: notify.mobile_app_slip16pro
                    data:
                      title: "Varning: Strålkastare fortfarande på"
                      message: >-
                        Strålkastaren är fortfarande tänd efter
                        scenåterställning. Kontrollera belysningen.
  - data:
      message: >
        "Lampornas tillstånd efter scenåterställning: {% for light in
        ['light.muren', 'light.garage_stralkastare', 'light.takfot_entre_both',
        'light.pollare_entre', 'light.garage_tuar',
        'light.switch_entre_pollare_aqara_t1_light'] %} - {{ light }}: state {{
        states(light) }}, brightness {{ state_attr(light, 'brightness') }},
        color {{ state_attr(light, 'rgb_color') or state_attr(light,
        'color_temp') }} {% endfor %}"
    action: system_log.write
  - target:
      entity_id: input_boolean.scene_garage_light_save_in_progress
    action: input_boolean.turn_off
    data: {}
  - data:
      message: Input_boolean 'scene_garage_light_save_in_progress' satt till 'off'
      level: info
    action: system_log.write
mode: single

Hi S,
Welcome to the forum. Let’s see if I can be of any assistance.

First off, you have the turn on automation in mode restart and a static scene name. That means if you get another trigger, it will overwrite your scene parameters with the current conditions.

My 'best practice on something like this,assuming it’s not expected to be on for multiple hours, is have it all 1 automation, have the automation run in single mode, use a random name for the screen create on the fly and reset the scene to saved at the end of the same automation.
Creating Unique Names for scene.create, persistant_notification, or Anything Else.