How do I distinguish between an automated (motion sensor) vs button push light "state_changed" event

My logic:

If motion sensor detects motion, if between sunset/sunrise, turn on backdoor light (zwave switch), wait 3 minutes, then turn off light

If the wall switch is manually turned on (from wall or portal), turn on the light and leave it on regardless of any new motion detected until someone turns the light off (either from wall or ha portal).

The problem I’m having setting this up is separating the state_change event from the motion sensor turning the light on vs state_change even from hitting the wall switch. I looked at the events fired pressing the button on the wall, and only a state_change is fired.

So, how can I detect that someone pushed a button vs an event being fired from another automation or from a sensor?

Thx!

Well, at first thought it could be as simple as setting a condition in your automation for the light’s state.

If the light is already on (so turned on either via HA or via the switch), then don’t run the automation.

Have a look at a similar automation of mine

alias: AUTO balcony motion
trigger:
  - platform: state
    entity_id: binary_sensor.balcony_motion
    to: 'on'
    from: 'off'
condition:
  - condition: state
    entity_id: binary_sensor.sun_below_horizon
    state: 'on'
  - condition: state
    entity_id: light.balcony_lamp
    state: 'off'
action:
  - service: light.turn_on
    target:
      entity_id: light.balcony_lamp
    data:
      brightness: 200
  - wait_for_trigger:
      - platform: state
        entity_id: binary_sensor.balcony_motion
        from: 'on'
        to: 'off'
  - delay:
      hours: 0
      minutes: 1
      seconds: 0
      milliseconds: 0
  - service: light.turn_off
    target:
      entity_id: light.balcony_lamp

Thanks! I built on your idea and did this (3 automations):

alias: Backyard Light Switch Off (enable motion detection)
description: ''
trigger:
  - platform: device
    type: turned_off
    device_id: 96764c1e3609446595a8d094548c05d6
    entity_id: switch.backyard_light_switch
    domain: switch
condition: []
action:
  - service: homeassistant.turn_on
    target:
      entity_id: input_boolean.motion_enabled
mode: single
alias: Backyard Light Switch On (disabled motion detection)
description: ''
trigger:
  - platform: device
    type: turned_on
    device_id: 96764c1e3609446595a8d094548c05d6
    entity_id: switch.backyard_light_switch
    domain: switch
condition: []
action:
  - service: homeassistant.turn_off
    target:
      entity_id: input_boolean.motion_enabled
mode: single
alias: Backyard Motion Detection (on for 5 minutes) Sunset > Sunrise
description: ''
trigger:
  - payload: 'true'
    platform: mqtt
    topic: sensors/backdoor/motion
condition:
  - condition: sun
    before: sunrise
    after: sunset
  - condition: state
    entity_id: input_boolean.motion_enabled
    state: 'On'
action:
  - device_id: 96764c1e3609446595a8d094548c05d6
    domain: switch
    entity_id: switch.backyard_light_switch
    type: turn_on
  - data:
      message: Backyard Motion
    service: notify.mobile_app_defcon_badge
  - delay:
      hours: 0
      minutes: 3
      seconds: 0
      milliseconds: 0
  - device_id: 96764c1e3609446595a8d094548c05d6
    domain: switch
    entity_id: switch.backyard_light_switch
    type: turn_off
mode: single