Disable motion triggered light when turned on manually

I have a dumb ceiling light in my kitchen, which is controlled by a Shelly1. The normal light switch is directly coupled to the Shelly’s relays to have quick response. Operating the Shelly in ‘Detached Mode’ to toggle the light via an home assistant automation delays the actual response of the light compared to when the switch is pressen.

In addition to this, I have a Aquara Motion Sensor, which I use to turn on the light, but as this is not a presence sensor, it does sometimes fail and turns off the light, whilst someone is still in the kitchen (i.e. it is a great automation for when entering the kitchen to grab something, but it is poor for whenever someone is doing something in the kitchen, although my motion triggered automation already has a cool down period). … That said I should probably level up and get a Everything Presence 1

My thinking has been, that whenever the light switch is operated manually to turn on the kitchen light, the motion triggered light automation should be disabled until the light is manually switched off.

Motion Triggered Light Automation
- alias: Light - Motion in Kitchen
  id: 'light_kitchen_motion'
  trigger:
  - platform: state
    entity_id: binary_sensor.motion_kitchen
    from: 'off'
    to: 'on'
  #If motion is detected within the delay, the automation is restarted.
  mode: restart
  max_exceeded: silent
  
  action:
  - choose:
    - conditions:
      #Turn kitchen light on automatically upon detected motion, in case ambient is dark 
      - condition: template
        value_template: "{{ (states('sensor.lightlevel_kitchen') | int < states('input_number.illuminance_cut_off_kitchen') | int) }}"
      sequence:
      - service: light.turn_on
        entity_id: light.kitchen
    default: []
  #Do not turn of the light in case kitchen appliances are running  
  - choose:
    - conditions:
      - condition: state
        entity_id: device_tracker.thermomix
        state: 'home'
      sequence:
      - wait_for_trigger:
          platform: state
          entity_id: device_tracker.thermomix
          from: 'home'
          to: 'not_home'
    default: []
  - wait_for_trigger: #No more motion detected
      platform: state
      entity_id: binary_sensor.motion_kitchen
      from: 'on'
      to: 'off'
    #Wait a little longer before turning light back off  
  - delay: "{{ states('input_number.no_motion_wait_kitchen') | int }}"
  - service: light.turn_off
    entity_id: light.kitchen
Switch Triggered Automation
- alias: Light - Kitchen Light Automation Switch
  id: 'light_kitchen_automation_switch'
  trigger:
    platform: event
    event_type: shelly.click
    event_data:
      device: shelly1-CAFEAFFEDEAF  #Shelly 1 Küche
  
  action:
    #Delay to consider light.kitchen may not be reported on immediatly upon button press
    - delay: 2 #seconds
    - if: #Light has just been turned on by the switch and now evaluates to 'on'
      - condition: state
        entity_id: light.kitchen
        state: 'on'
      then: #Disable motion light automation (i.e. light remains on until manually turned off.)
      - service: automation.turn_off
        entity_id: automation.light_motion_in_kitchen
      else: #Automation is re-enabled, whenever the light is manually turned-off.
      - service: automation.turn_on
        entity_id: automation.light_motion_in_kitchen

However I struggle to get the logic right. The automation keeps enabling the automation, when I expect it to disable it and vice versa.

1 Like

One idea: Create a Toggle helper (input_boolean) to track whether the light was turned on by the motion-detected automation.

You can then check this input_boolean in the motion-stopped automation and only turn off the light if it’s still set (and then remember to clear the input_boolean).

You could also clear this input_boolean whenever you turn on the light with the switch (even if the light is already on, and thus override the auto-light).

Entity controller from HACS does this.