Help with condition light was off

Hi
Sorry but struggling with the following:
I have an input_boolean (front_camera_motion) that is set ‘on’ whenever my external camera detects motion.
I’m trying to just turn on a light for 5 minutes, and then only turn it off if it wasn’t on in the first place.
Also after 5 minutes i will reset the camera motion flag to off to start the cycle again.

My code looks like:

  • alias: do_front_camera_processing
    initial_state: True
    hide_entity: False
    trigger:
    platform: state
    entity_id: input_boolean.front_camera_motion
    from: ‘off’
    to: ‘on’
    action:

#-- motion detected!!!
#-- capture master bedroom light current state. is it on or off? store into input_number.front_light_state
#-- Not sure this is the correct command. Trying to store the current state of the master bedroom light.

  • service: input_number.set_value
    data:
    entity_id: input_number.front_light_state
    value:{{ (states.light.master_bedroom) | int }}

#-- turn light on regardless… i.e. if it was already on then no change

  • service: light.turn_on
    entity_id: light.master_bedroom

#-- now trigger in 5 mins

  • alias: front lights off
    trigger:
    platform: state
    entity_id: input_boolean.front_camera_motion
    to: ‘on’
    for:
    minutes: 5
    action:
    condition:

#-- first reset the camera motion detection flag
service: input_boolean.turn_off
entity_id: input_boolean.front_camera_motion

#-- next this is the section i’m struggling with. so if input_number.front_light_state = 0 then light was originally off?? and now want to turn it off (within this same action section), but how to code?
#-- if condition light was originally off then turn light off

Please can you help with the above or suggest an alternative approach?

Many thanks Mike

Can you edit your post and use the </> button around your code sections. The way you posted it removes all the correct white space. Also, maybe you can use my automation for motion detecting light as a guide:

automation section:

- alias: Hall Motion Detected During Night
  trigger:
    - platform: state
      entity_id: binary_sensor.hallway_ms_sensor_32_0
      to: 'on'
  condition:
    - condition: time
      after: '23:00:00'
      before: '07:00:00'
  action:
    - service: homeassistant.turn_on
      entity_id: script.hall_motion_night
    
- alias: Hall Motion Detected During Day
  trigger:
    - platform: state
      entity_id: binary_sensor.hallway_ms_sensor_32_0
      to: 'on'
  condition:
    - condition: time
      after: '07:00:00'
      before: '23:00:00'
  action:
    - service: homeassistant.turn_on
      entity_id: script.hall_motion_day

scripts:

Light timer:

alias: Hall Light Timer
sequence:
  - delay:
      minutes: 5
  - service: light.turn_off
    data:
      entity_id: light.hall_d_level_10_0

Motion automation day:

alias: Hall Motion Day
sequence:
  # Cancel ev. old timers
  - service: script.turn_off
    data:
      entity_id: script.hall_light_timer
  - service: light.turn_on
    data:
      entity_id: light.hall_d_level_10_0
      brightness: 255
  # Set new timer
  - service: script.turn_on
    data:
      entity_id: script.hall_light_timer

Motion automation night:

alias: Hall Motion Night
sequence:
  # Cancel ev. old timers
  - service: script.turn_off
    data:
      entity_id: script.hall_light_timer
  - service: light.turn_on
    data:
      entity_id: light.hall_d_level_10_0
      brightness: 10
  # Set new timer
  - service: script.turn_on
    data:
      entity_id: script.hall_light_timer

Thanks, but i got it working per my script.

Sharing

- alias: dorearcameracond
  initial_state: True
  hide_entity: False
  trigger:
    platform: state
    entity_id: input_boolean.rear_camera_motion
    from: 'off'
    to: 'on'
  action:
#- capture light current state
  - service: input_number.set_value
    data_template:
        entity_id: input_number.rear_light_state
        value: '{% if states.light.living_room.state == "off" %}0{% else %}1{% endif %}'

#-  turn off, turn light on regardless
  - service: light.turn_off
    entity_id: light.living_room
  - service: light.turn_on
    entity_id: light.living_room
  - service: tts.google_say
    data:
        entity_id: "media_player.living_room"
        message: 'Rear Camera Triggered.'
##
#- trigger in 2 mins
##
- alias: rear lights off
  trigger:
    platform: state
    entity_id: input_boolean.rear_camera_motion
    to: 'on'
    for:
      minutes: 2
  action:
  - service: input_boolean.turn_off
    entity_id: input_boolean.rear_camera_motion
#
  - service: input_number.set_value
    data_template:
          entity_id: input_number.rear_light_state
          value: '{% if states.input_number.rear_light_state.state|int == 0|int %}-1{% else %}3{% endif %}'  

#- turn off light if was on
- alias: rear lights off2
  trigger:
    platform: numeric_state
    entity_id: input_number.rear_light_state
    below: 0
  action:
  - service: light.turn_off
    entity_id: light.living_room
  - service: input_number.set_value
    data_template:
        entity_id: input_number.rear_light_state
        value: 4