Light switch to provide automation override

Hi All,

I have two automations to provide motion based light control in my kitchen. What I would like is to be able to manually override the automatic control if the light switch is operated, but only on a per instance basis. ie: if on any particular occasion I walk into the kitchen and the light turns ON automatically, I would like to be able to press the light switch button to have the light stay ON indefinitely rather than still turn OFF as per the kitchen_light_auto_off.yaml automation (when motion stops for 2 minutes) below. Currently if I press the light switch, the light will turn OFF, and I can then turn it back ON by pressing it again, however the auto_off will still occur. Is there a way to change this code so that a manual switch press disables the auto_off for that instance only? My thoughts being that if I force the light to stay ON, I can then manually turn it OFF again later and subsequent motion in the kitchen will turn it ON again. Obvisouly I can turn OFF the kitchen_light_auto_off automation in this case to achieve this but that means using a phone / computer / tablet when all I want is the ease of doing so via the light switch for a simple and quick manual mode.

kitchen_light_auto.yaml

alias: Kitchen light auto ON motion
initial_state: 'on'
trigger:
  - platform: state
    entity_id: binary_sensor.kitchen_multi_sensor_sensor
    to: 'on'
condition:
  condition: or
  conditions:
    - condition: sun
      after: sunset
#    after_offset: '00:00:00'
    - condition: sun
      before: sunrise
      before_offset: '00:30:00'
action:
  - service: homeassistant.turn_on
    entity_id: light.kitchen_light_level
    data:
      brightness: 100

kitchen_light_auto_off.yaml

alias: Kitchen light OFF after 2mins
initial_state: 'on'
trigger:
  - platform: state
    entity_id: binary_sensor.kitchen_multi_sensor_sensor
    to: 'off'
    for:
      minutes: 2
action:
  - service: homeassistant.turn_off
    entity_id: light.kitchen_light_level

any help would be greatly appreciated. Cheers.

I use input booleans for these things, as in this post. Turn the input_boolean on when the automation turns the light on, but not if you turn it on manually. Then you only turn the light off automatically if the input boolean is off.

Whether you can make the switch not turn the light off, that depends on what your switch is - you forgot to tell us.

2 Likes

Thanks for the hint and link. That’s exactly what I needed.

The light switch is actually a momentary push-button hard-wired into the switch input of a z-wave dimmer so it doesn’t have a HA entity at all. The button will operate the light independent of HA and the dimmer simply relays the light’s status to HA. Since the button isn’t a HA entity I don’t see how I could stop a button press while already ON from turning it OFF rather than just forcing ‘manual mode’, but even if I have to turn it OFF and then back ON it will work with your linked example. Thanks!

Hi @Tinkerer, I was just making the code changes for this an had a thought but am yet to check it in action. Surely this code doesn’t prevent the automation from setting the input_boolean to ‘on’ despite the light already being on (in the case that the light was turned on manually) so it would still allow the ‘off’ automation to function…?

this is what I have at the moment:

kitchen_light_auto.yaml

alias: Kitchen light auto ON motion
initial_state: 'on'
trigger:
  - platform: state
    entity_id: binary_sensor.kitchen_multi_sensor_sensor
    to: 'on'
condition:
  condition: or
  conditions:
    - condition: sun
      after: sunset
#    after_offset: '00:00:00'
    - condition: sun
      before: sunrise
      before_offset: '00:30:00'
action:
  - service: homeassistant.turn_on
    entity_id: light.kitchen_light_level
    data:
      brightness: 100
  - service: input_boolean.turn_on
    data:
      entity_id: input_boolean.kitchen_light_motion_activated

kitchen_light_auto_off.yaml

alias: Kitchen light OFF after 2mins
initial_state: 'on'
trigger:
  - platform: state
    entity_id: binary_sensor.kitchen_multi_sensor_sensor
    to: 'off'
    for:
      minutes: 2
condition:
  condition: state
  entity_id: input_boolean.kitchen_light_motion_activated
  state: 'on'
action:
  - service: homeassistant.turn_off
    entity_id: light.kitchen_light_level
  - service: input_boolean.turn_off
    entity_id: input_boolean.kitchen_light_motion_activated

So I’m thinking that I will need to have a condition in the ‘auto_on’ to prevent it running if the light is already ‘on’…?

I have come up with this for my kitchen_light_auto.yaml but haven’t tested yet as I’m not home…

alias: Kitchen light auto ON motion
initial_state: 'on'
trigger:
  - platform: state
    entity_id: binary_sensor.kitchen_multi_sensor_sensor
    to: 'on'
condition:
  condition: and
  conditions:
    - condition: state
      entity_id: light.kitchen_light_level
      state: 'off'
    - condition:
      - condition: or
        conditions:
          - condition: sun
            after: sunset
#    after_offset: '00:00:00'
          - condition: sun
            before: sunrise
            before_offset: '00:30:00'
action:
  - service: homeassistant.turn_on
    entity_id: light.kitchen_light_level
    data:
      brightness: 100
  - service: input_boolean.turn_on
    data:
      entity_id: input_boolean.kitchen_light_motion_activated

should that work?

It seems the above code is wrong in that it fails the config check… I #'ed out the condition: section and it passes but its the first time I’ve tried nested conditions so I’m not sure what I’ve done wrong.

Can someone please show me where I’ve made the mistake?

One too many ‘conditions’ - the line under state: off needs removing and everything below needs to come left 2 spaces :+1:

awesome! thank you. That made it pass, now to test it out :slight_smile:

Well after a bit of playing around trying to dodge the motion sensor while hitting the light switch, I have had mixed results with the below code. At first I thought it was working how I wanted, but then the light would sometimes turn off, other times not turn off when it was supposedly in ‘auto’ mode… can someone please point me in the right direction here?

input_booleans.yaml

kitchen_light_motion_activated:
  name: Kitchen light motion activated
  initial: off
  icon: mdi:run-fast

kitchen_light_auto.yaml

alias: Kitchen light auto ON motion
initial_state: 'on'
trigger:
  - platform: state
    entity_id: binary_sensor.kitchen_multi_sensor_sensor
    to: 'on'
condition:
  condition: and
  conditions:
    - condition: state
      entity_id: light.kitchen_light_level
      state: 'off'
    - condition: or
      conditions:
        - condition: sun
          after: sunset
          after_offset: '00:00:00'
        - condition: sun
          before: sunrise
          before_offset: '00:30:00'
action:
  - service: homeassistant.turn_on
    entity_id: light.kitchen_light_level
    data:
      brightness: 100
  - service: input_boolean.turn_on
    data:
      entity_id: input_boolean.kitchen_light_motion_activated

kitchen_light_auto_off.yaml

alias: Kitchen light OFF after 2mins
initial_state: 'on'
trigger:
  - platform: state
    entity_id: binary_sensor.kitchen_multi_sensor_sensor
    to: 'off'
    for:
      minutes: 2
condition:
  condition: state
  entity_id: input_boolean.kitchen_light_motion_activated
  state: 'on'
action:
  - service: homeassistant.turn_off
    entity_id: light.kitchen_light_level
  - service: input_boolean.turn_off
    entity_id: input_boolean.kitchen_light_motion_activated

My intention is to have the light turn on and off automatically based on motion, however if I want to force the light to stay on I press the light switch off and then back on to put it in ‘manual mode’. My suspicion is that the motion sensor is still putting the light back in ‘auto’ mode when it sees me walking around, even after I have tried to manually override it. As I said, it seems to be hit and miss as to it working or not. Prior to the manual override code, the light worked perfectly in ‘auto’ mode, its just the manual override that I cant seem to get quite right.

any takers?

The trouble is, you need to turn off the input boolean simply when the light turns off, and you’re only turning it off when they’re automatically turned off.

Here’s my writeup on that.

Thanks for the write-up. I have made the changes but wont be able to test until I get home. From what I can tell from your code though, the motion sensor is still able to turn the input_boolean ON when someone walks past it, despite the light being ON manually. Therefore the OFF automation will still be valid. Surely you need to prevent the input_boolean from switching ON if the light was manually turned ON beforehand, otherwise nothing stops the OFF automation from occurring anyway. I get that you have the condition there in the OFF automation, but if the motion sensor can still cause the input_boolean to switch, then this condition is met.

I think that the ON automation needs a condition to ensure that the light is OFF before it can run, that way eliminating the motion sensor from switching the input_boolean if the light was already ON having been manually switched… but then the re-trigger for motion in that room wont work. This is where I was stumped before.

Damn good point - thanks for pointing that out. The turn on automation does need that check, and I’ve updated the blog accordingly.

cool. thats what I had in my original code and have now put it back in. I’ll give it another test. It seems like your example is very much like mine but uses an additional automation. Hopefully this now works. Cheers for the help.

Hi @Tinkerer I have got the code entered but it failed where I have it in the double ** below:

 alias: Kitchen light OFF after 2mins
initial_state: 'on'
trigger:
  - platform: state
    entity_id: binary_sensor.kitchen_multi_sensor_sensor
    to: 'off'
    for:
      minutes: 2
condition:
  - condition: state
    entity_id: input_boolean.kitchen_light_motion_activated
    state: 'on'
  - condition: state
    entity_id: light.kitchen_light_level
    **condition:** 'on' 
action:
  - service: homeassistant.turn_off
    entity_id: light.kitchen_light_level

so I changed that to state: and it passes. You may want to update the blog entry :slight_smile:

Dammit. Must remember to stop editing blog posts when mostly asleep.

Thanks again.

Did the solution posted by @Tinkerer end up working smoothly for you. I’ve tricked my garage light tonight that has a motion sensor but wondering how this works if the motion sensor trips before you get to the physical light switch?

It didn’t work reliably so I ended up changing my control philosophy for motion controlled lights. I now have Xiaomi Wireless Buttons which (via automations) control the respective light to be in a particular mode: ON, OFF, AUTO (motion controlled). When in ON mode, the light is set to full brightness and the motion sensor has no effect, likewise with OFF mode being light off. AUTO mode allows the light to be motion controlled (for both turning on and off) and sets Circadian Lighting for that light to ON so that the brightness and colour temp are set correctly for the time of day

Thanks for that, though that might be the case. Is your code for your Xiaomi button scenario available as I have a few of those too! :slight_smile:

I use packages so this is my kitchen_light.yaml

#  All code relating to the functioning of the kitchen light

input_number:
  kitchen_light_auto_off_time:
    name: Kitchen light timer
    min: 1
    max: 9
    step: 1
    icon: mdi:camera-timer
    unit_of_measurement: min


automation:
  - alias: 'Kitchen Remote Button 1'
    initial_state: 'on'
    trigger:
      - platform: event
        event_type: xiaomi_aqara.click
        event_data:
          entity_id: binary_sensor.switch_158d0002241b37
          click_type: single
      - platform: event
        event_type: xiaomi_aqara.click
        event_data:
          entity_id: binary_sensor.switch_158d0002241b37
          click_type: double
      - platform: event
        event_type: xiaomi_aqara.click
        event_data:
          entity_id: binary_sensor.switch_158d0002241b37
          click_type: long_click_press

    action:
      - service_template: >
          {% if trigger.event.data.click_type == 'single' %}
            script.kitchen_button_single_click
          {% elif trigger.event.data.click_type == 'double' %}
            script.kitchen_button_double_click
          {% elif trigger.event.data.click_type == 'long_click_press' %}
            script.kitchen_button_long_press
          {% endif %}
          


  - alias: 'Kitchen light auto ON motion'
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: binary_sensor.kitchen_multi_sensor_sensor
        to: 'on'
    condition:      # look at changing this to use room light level only
      condition: or
      conditions:
      - condition: sun
        after: sunset
        after_offset: '-00:10:00'
      - condition: sun
        before: sunrise
        before_offset: '-01:00:00'
      - condition: template
        value_template: '{{ states.sensor.kitchen_multi_sensor_luminance.state | int < 10 }}'
    action:
      - service: script.turn_off
        entity_id: script.kitchen_light_auto_off_timer
      - service: light.turn_on
        data:
          entity_id: light.kitchen_light_level



  - alias: 'Kitchen light auto off'
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: binary_sensor.kitchen_multi_sensor_sensor
    condition:
      condition: or
      conditions:
      - condition: sun
        after: sunset
        after_offset: '-00:10:00'
      - condition: sun
        before: sunrise
        before_offset: '-01:00:00'
    action:
      - service: script.turn_on
        entity_id: script.kitchen_light_auto_off_timer


script:
  kitchen_button_single_click:
    alias: Kitchen button single click
    sequence:
    - service: switch.turn_off
      data:
        entity_id: switch.circadian_lighting_kitchen_living_circadian_lighting
    - service: light.turn_on
      data:
        entity_id: light.kitchen_light_level
        brightness: 255
    - service: automation.turn_off
      data:
        entity_id: 
          - automation.kitchen_light_auto_on_motion
          - automation.kitchen_light_auto_off
  
  kitchen_button_double_click:
    alias: Kitchen button double click
    sequence:
    - service: light.turn_off
      data: 
        entity_id: light.kitchen_light_level
    - service: automation.turn_off
      data:
        entity_id: 
          - automation.kitchen_light_auto_on_motion
          - automation.kitchen_light_auto_off
  
  kitchen_button_long_press:
    alias: Kitchen button long press
    sequence:
    - service: switch.turn_on
      data:
        entity_id: switch.circadian_lighting_kitchen_living_circadian_lighting
    - service: automation.turn_on
      data:
        entity_id: 
          - automation.kitchen_light_auto_on_motion
          - automation.kitchen_light_auto_off
  
  kitchen_light_auto_off_timer:
    alias: Kitchen light off after preset time
    sequence:
    - delay: '00:0{{ states.input_number.kitchen_light_auto_off_time.state | int }}:00'
    - service: homeassistant.turn_off
      data:
        entity_id: light.kitchen_light_level
1 Like