Trigger a light using both HA and binary sensor

I would like to trigger a light using both HA and binary sensor (PIR). The objective is to allow PIR sensor to turn on and off the light automatically, but allow HA to override the sensor trigger. E.g. If I turn on the light from HA, binary sensor will be disabled until I turn the light off from HA.

I came up something like this:

globals:
  - id: right_side_triggered
    type: bool
    initial_value: 'false'

binary_sensor:
  - platform: gpio
    pin: 12
    name: "Right Side PIR Sensor"
    device_class: motion
    id: right_side_pir
    on_press:
      then:
        - if:
            condition:
              and:
                - light.is_off: my_light
                - lambda: |-
                    return !id(right_side_triggered);
            then:
              - globals.set:
                  id: right_side_triggered
                  value: 'true'
              - light.turn_on: my_light
              - while:
                  condition:
                    binary_sensor.is_on: right_side_pir
                  then:
                    - delay: 5s
              - light.turn_off: my_light
              - globals.set:
                  id: right_side_triggered
                  value: 'false'

The challenge I’m facing right now is I won’t be able to tell who turned on the light when the light is already on. For example, PIR sensor triggered and light turned on. Then I go to the HA and turn on the light and the light will be turn off by the sensor.

Is there a way to set a flag/var when light is turned on by HA? Thanks.

This is how I do exactly what you described. I have a Sonoff Basic with a PIR to control a light. If the PIR senses movement, it turns on the light and after two minutes of not sensing any movement, it turns off the light.

But of course I can turn the light on/off at will from HA by simply having this on a glance card. If I tap on the icon it toggles the light switch.

- entity: switch.kitchen_sink_relay
    name: Sink
    tap_action:
      action: toggle

I hope this helps.

substitutions:
  devicename: kitchen_sink
  devicename_friendly: Kitchen Sink
  ip_address: 192.168.2.193
  deviceplatform: ESP8266 
  deviceboard: esp01_1m 
  
packages:
  wifi: !include common/wifi_setup2.yaml  #TODO CHANGE THIS FOR FDR
  logger: !include common/logger.yaml
  api: !include common/api.yaml
  ota: !include common/ota.yaml
  device_base: !include common/device_base.yaml

binary_sensor:

    #PIR
  - platform: gpio
    pin: 
      number: GPIO3 #RX pin on Sonoff basic
      #inverted: true
      
    name: ${devicename} PIR Sensor
    id: my_motion
    device_class: motion    
    on_press:
      - switch.turn_on: ${devicename}_relay
      
  - platform: template
    name: delayed_switch
    lambda: 'return id(my_motion).state;'
    internal: true
    filters:
      - delayed_off: 2min
    on_release:
      - switch.turn_off: ${devicename}_relay

  - platform: gpio
    pin:
      number: GPIO13
      mode: INPUT_PULLUP
      #inverted: True
    name: ${devicename} Button
    internal: true
    on_press:
     then:
     - switch.toggle: ${devicename}_relay

  


switch:

  - platform: gpio
    name: ${devicename} relay
    pin: GPIO12
    id: ${devicename}_relay
    restore_mode: RESTORE_DEFAULT_ON
    icon: mdi:ceiling-light
    on_turn_on:
    - logger.log: "Kitchen Light Turned On!"
    on_turn_off:
    - logger.log: "Kitchen Light Turned Off!"


Thanks @laca75tn. How do you prevent the light being turned off when light is turned on through HA and then PIR sensor is triggered?

That’s a good question. So, you are asking what happens if I turn on the light manually through HA and then the PIR is triggered, right?

I am thinking that in that case you will have to write a script to turn the light on when the PIR senses movement, waits for X minutes, and then turns off the light.

And you call the script only when the light is off.