Motion sensor lighting with manual override

Motion sensor automation with manual override option.
I have a motion sensor that automatically turns on and after 2 minutes turns off a device called ‘Gardenlights’. When I manually switch on the Gardenlights with the wall switch or Lovelace interface and then walk into the garden, the lights automatically turn off again after 2 minutes because the motion sensor automation kicks in. I don’t want that.
Here’s how to create a manual override so that when the wall switch or lovelace interface is used, the motion sensor automation is disabled. When the wall switch or lovelace interface is used to turn the garden lights off again, the motion sensor automation will be enabled again.
Because the Homeassistant team have created a wonderful UI to create automations, we’re going to use that as much as possible.

First we’ll create a helper to set the override option to on or off.

  • Login to Homeassistant – Setting – Devices & Services – Helpers
  • Create new Helper – Toggle – Name: Gardenlights override – Icon: mdi:toggle-switch-outline and click create.

Next we’ll create an automation to toggle the helper on and off for all possible situations.

  • Select Setting – Automations & Scenes – Create a new automation
  • Click the save button en save with name : Gardenlights override set helper on/off
  • Add trigger - Device - Gardenlights – Trigger: Gardenlights turned on
  • Click on the menu option (3 dots) of this trigger and Select EditID – Give the Trigger ID the name: Gardenlights on
  • Add a second trigger - Device - Gardenlights – Trigger: Gardenlights turned off
  • Click on the menu option (3 dots) of this trigger and Select EditID – Give the Trigger ID the name: Gardenlights off
  • We are not going to use the Conditions section, so skip that
  • STARTON
  • Under the Action section select Add Action and select the Choose option
  • In the Option 1 section select Add Condition and select Triggered by and select the trigger condition : Gardenlights on
  • Still in the same option 1 section, again select Add Condition and select Template
  • A: Copy this in the value template field : {{ trigger.to_state.context.id != none }}
  • Again in the Option 1 section select Add Condition and select Template
  • B: Copy this in the value template field : {{ trigger.to_state.context.parent_id == none }}
  • Again in the Option 1 section select Add Condition and select Template
  • C: Copy this in the value template field : {{ trigger.to_state.context.user_id == none }}
  • Stil in Option 1 section in the Action section select Add Action – Call a service
  • In the service field select Input Boolean:turn on
  • In the Targets section click on Choose Entity and select the helper Gardenlights override
  • Now close (collapse) this Choose section.
  • Repeats all the steps above from STARTON but change the Value Template
  • A: Copy this in the value template field : {{ trigger.to_state.context.id != none }}
  • B: Copy this in the value template field : {{ trigger.to_state.context.parent_id == none }}
  • C: Copy this in the value template field : {{ trigger.to_state.context.user_id != none }}
  • Now repeats all the steps again from STARTON but change the Value Template
  • A: Copy this in the value template field : {{ trigger.to_state.context.id != none }}
  • B: Copy this in the value template field : {{ trigger.to_state.context.parent_id != none }}
  • C: Copy this in the value template field : {{ trigger.to_state.context.user_id == none }}
  • So far we’ll have configured all options for the helper ‘on’ states and now we’ll configure all options for the helper ‘off’ states.
  • STARTOFF
  • In the Action section select Add Action and select the Choose option
  • In the Option 1 section select Add Condition and select Triggered by
  • Select the trigger condition : Gardenlights off
  • Still in the Option 1 section in the Action section select Add Action – Call a service
  • In the service field select Input Boolean:turn off
  • In the Action section select Add Action and select the Choose option
  • In the Option 1 section select Add Condition and select Template
  • A: Copy this in the value template field : {{ trigger.to_state.context.id != none }}
  • Under the Option 1 section select Add Condition and select Template
  • B: Copy this in the value template field : {{ trigger.to_state.context.parent_id == none }}
  • Under the Option 1 section select Add Condition and select Template
  • C: Copy this in the value template field : {{ trigger.to_state.context.user_id == none }}
  • Now repeats all the steps above from STARTOFF but change the Value Template
  • A: Copy this in the value template field : {{ trigger.to_state.context.id != none }}
  • B: Copy this in the value template field : {{ trigger.to_state.context.parent_id == none }}
  • C: Copy this in the value template field : {{ trigger.to_state.context.user_id != none }}
  • Now repeats all the steps again from STARTOFF but change the Value Template
  • A: Copy this in the value template field : {{ trigger.to_state.context.id != none }}
  • B: Copy this in the value template field : {{ trigger.to_state.context.parent_id != none }}
  • C: Copy this in the value template field : {{ trigger.to_state.context.user_id == none }}
  • If all has gone well you’ll end up with 2 triggers (lights on and off) and 6 actions (3 triggerID condition on and 3 triggerID condition off).
  • Next we’ll create an motion sensor automation that only starts when the helper Gardenlights override is off
  • In Homeassistant select Setting – Automations & Scenes – Create Automation
  • Click the save button en save with name : Motion sensor Gardenlights with override
  • Click Add Trigger – Device - Motion sensor Gardenlights on
  • In Trigger section select Sensor Gardenlights started detecting motion
  • In the Action section select Add Action and select the Choose option
  • In the Option 1 section select Add Condition and select State
  • In Entity select the Gardenlights override helper
  • Leave Attribute empty and under State select off
  • Stil in Option 1 section in the Action section select Add Action
  • Select Add Device – Gardenlights – turn on Gardenlights
  • Stil in Option 1 section in the Action section select Add Action
  • Select Wait for time to pass – select 2 minutes
  • Stil in Option 1 section in the Action section select Add Action
  • Select Add Device – Gardenlights – turn off Gardenlights
  • Save and that’s it, all done.
  • To check if the helper is set correctly you can go to Setting – Devices & Services – Helpers and open the Gardenlights override helper.
  • Click on info and there see if the state of the helper (on or off), now start switching the garden lights on and off using various triggers.

Tip

In the Automation Editor’s upper right hand corner is its overflow menu.

  1. Click it and select ‘Edit in YAML’
  2. Select and copy all of the displayed YAML and paste it into the forum.
  3. Select what you pasted and click the </> icon in the forum editor’s menu. This step displays the YAML correctly (see FAQ guideline 11).

This is the standard procedure for sharing automations and scripts. A user can simply copy the YAML you posted and paste it into a new, empty automation on their system. They can then switch from ‘Edit in YAML’ to ‘Edit in visual editor’ to display it in visual mode.

That’s far more convenient, and less error prone, than following dozens of steps to create an automation.

Or even better…create a blueprint (but that would be asking too much from a first time poster🤔)

1 Like

Thanks for posting this @pkp. It looks like your automation override is what I’m looking for but I’m confused at the step that says to “Repeat all the steps above from STARTON but change the Value Template”. The template values all appear to be the same for the second & third STARTON sections.

Perhaps you can copy and paste the YAML code so I can see the full example.

Thanks

@pkp Thanks for taking the time to post the above.

I was going to create a new post for a similar question as I couldn’t find any posts with the exact scenario I’m trying to achieve, but thought I would ask here first. I think I might be able to use something similar to what has been described here but would appreciate some guidance.

I have a Philips Hue smart bulb in our small WC room. This is currently being activated by a Philips Motion Sensor after I created a Helper based on motion detected, giving me an input_boolean to show ‘Toilet Occupancy’.
I have an automation which turns on the light when this input_boolean is toggled to on, provided the light level in the room is lower than 40lux, so avoid unnecessarily coming on during the day unless it’s quite dark. Then after no movement is detected for 2 minutes, the input_boolean changes to off, and this triggers the automation to turn the light off.
All works well, except we still have a dumb wall light switch. Sometimes my other half will enter the WC but doesn’t want the light on for whatever reason, so has to use the wall switch to turn it off. Obviously this renders the system useless as she rarely flicks the switch back on again upon leaving the WC (why would you I suppose).

So I’ve bought an Aqara Zigbee wireless smart switch which has a single button and can register either a single, double, hold, or release state in HA from Z2M. I’m hoping I can make the dumb wall switch redundant and leave it connected, and fit the small Aqara button over the top/on a blanking plate, so that it can also be used to turn the light both on or off manually.

Not having much experience writing automations, I am trying to come up with a way of handling all scenarios. I’ve found so many ‘motion lights’ posts and examples online but don’t know how to write an automation to cater for all possible scenarios.

  1. If the Aqara button is pressed, it will probably be to turn off the light that came on automatically due to motion being detected.
  2. However, it could also be used to turn the light ON if it’s above 40lux and someone just wants to have the light on for whatever reason.
  3. If the button is used to turn the light either on or off, it needs to override the motion sensor from triggering the light to go on or off.
  4. after the button has been used manually for turning the light on or off, how does the motion detector then get reactivated ready for that automation to start working as normal again.
  5. If someone leaves the light on after using the button, and the motion sensor is overridden, how do I cater for turning the light off automatically at some point, without it ending up being left on all day?

I think those scenarios cover most possible eventualities, but I’m completely stuck on how to put a set of rules within an automation to cater for this. Appreciate any help, and of course, happy to create a new thread if this one isn’t suitable.

description: ""
trigger:
  - platform: device
    type: turned_on
    device_id: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    entity_id: Gardenlights
    domain: light
    id: Gardenlights on
  - platform: device
    type: turned_off
    device_id: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    entity_id: Gardenlights
    domain: light
    id: Gardenlights off
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: Gardenlights on
          - condition: template
            value_template: "{{ trigger.to_state.context.id != none }}"
          - condition: template
            value_template: "{{ trigger.to_state.context.parent_id == none }}"
          - condition: template
            value_template: "{{ trigger.to_state.context.user_id == none }}"
        sequence:
          - service: input_boolean.turn_on
            data: {}
            target:
              entity_id: input_boolean.gardenlights_override
  - choose:
      - conditions:
          - condition: trigger
            id: Gardenlights on
          - condition: template
            value_template: "{{ trigger.to_state.context.id != none }}"
          - condition: template
            value_template: "{{ trigger.to_state.context.parent_id == none }}"
          - condition: template
            value_template: "{{ trigger.to_state.context.user_id != none }}"
        sequence:
          - service: input_boolean.turn_on
            data: {}
            target:
              entity_id: input_boolean.gardenlights_override
  - choose:
      - conditions:
          - condition: trigger
            id: Gardenlights on
          - condition: template
            value_template: "{{ trigger.to_state.context.id != none }}"
          - condition: template
            value_template: "{{ trigger.to_state.context.parent_id != none }}"
          - condition: template
            value_template: "{{ trigger.to_state.context.user_id == none }}"
        sequence:
          - service: input_boolean.turn_on
            data: {}
            target:
              entity_id: input_boolean.gardenlights_override
  - choose:
      - conditions:
          - condition: trigger
            id: Gardenlights off
          - condition: template
            value_template: "{{ trigger.to_state.context.id != none }}"
          - condition: template
            value_template: "{{ trigger.to_state.context.parent_id == none }}"
          - condition: template
            value_template: "{{ trigger.to_state.context.user_id == none }}"
        sequence:
          - service: input_boolean.turn_off
            data: {}
            target:
              entity_id: input_boolean.gardenlights_override
  - choose:
      - conditions:
          - condition: trigger
            id: Gardenlights off
          - condition: template
            value_template: "{{ trigger.to_state.context.id != none }}"
          - condition: template
            value_template: "{{ trigger.to_state.context.parent_id == none }}"
          - condition: template
            value_template: "{{ trigger.to_state.context.user_id != none }}"
        sequence:
          - service: input_boolean.turn_off
            data: {}
            target:
              entity_id: input_boolean.gardenlights_override
  - choose:
      - conditions:
          - condition: trigger
            id: Gardenlights off
          - condition: template
            value_template: "{{ trigger.to_state.context.id != none }}"
          - condition: template
            value_template: "{{ trigger.to_state.context.parent_id != none }}"
          - condition: template
            value_template: "{{ trigger.to_state.context.user_id == none }}"
        sequence:
          - service: input_boolean.turn_off
            data: {}
            target:
              entity_id: input_boolean.gardenlights_override
mode: single
description: ""
trigger:
  - type: motion
    platform: device
    device_id: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    entity_id: Sensor Gardenlights
    domain: binary_sensor
condition: []
action:
  - choose:
      - conditions:
          - condition: state
            entity_id: input_boolean.gardenlights_override
            state: "off"
        sequence:
          - type: turn_on
            device_id: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
            domain: light
          - delay:
              hours: 0
              minutes: 2
              seconds: 0
              milliseconds: 0
          - type: turn_off
            device_id: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
            domain: light
mode: single

This is awesome.

What if I want the light to be dimmed (like 10%) when there is no movement.

If movement is detected brightness will increase to a set value and then, after 2 min it will be dimmed back to 10% again.

Still want to be able to override motion sensor when light is turned on manually.

Can anyone help me accomplish this?

I"m confused. The 6 trigger conditions (3 for on and 3 for off) are all identical. Am I supposed to make each one do something different? Please advise

What about just making a condition to not run the automation if the lights are already on?

Pseudo config for this:

trigger:
    motion_sensor == detected
condition:
    garden_light == off     < --- "abort if the light is alraedy on"
action:
    set: garden_light = on
    wait: motion_sensor == clear
    set: garden_light = off

I am aware that it will not work if there is already activity in the garden, but I guess it’s most often not the case and it would make the whole setup extremely simple.

This is How i modified for my configuaration, Two Lights - Study Lamp to Work only Late Night and Study Tube Light to work through the day.

  1. Automation for Override:
alias: Study Motion Override
description: ""
trigger:
  - platform: state
    entity_id:
      - switch.study_lamp
      - switch.study_tube_light
    from: "off"
    to: "on"
    id: Study Motion Override ON
  - platform: state
    entity_id:
      - switch.study_lamp
      - switch.study_tube_light
    from: "on"
    to: "off"
    id: Study Motion Override OFF
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: Study Motion Override ON
          - condition: template
            value_template: "{{ trigger.to_state.context.id != none }}"
          - condition: template
            value_template: "{{ trigger.to_state.context.parent_id == none }}"
          - condition: template
            value_template: "{{ trigger.to_state.context.user_id == none }}"
        sequence:
          - service: input_boolean.turn_on
            data: {}
            target:
              entity_id: input_boolean.study_motion_override
  - choose:
      - conditions:
          - condition: trigger
            id: Study Motion Override ON
          - condition: template
            value_template: "{{ trigger.to_state.context.id != none }}"
          - condition: template
            value_template: "{{ trigger.to_state.context.parent_id == none }}"
          - condition: template
            value_template: "{{ trigger.to_state.context.user_id != none }}"
        sequence:
          - service: input_boolean.turn_on
            data: {}
            target:
              entity_id:
                - input_boolean.study_motion_override
  - choose:
      - conditions:
          - condition: trigger
            id: Study Motion Override ON
          - condition: template
            value_template: "{{ trigger.to_state.context.id != none }}"
          - condition: template
            value_template: "{{ trigger.to_state.context.parent_id != none }}"
          - condition: template
            value_template: "{{ trigger.to_state.context.user_id == none }}"
        sequence:
          - service: input_boolean.turn_on
            data: {}
            target:
              entity_id:
                - input_boolean.study_motion_override
  - choose:
      - conditions:
          - condition: trigger
            id: Study Motion Override OFF
          - condition: template
            value_template: "{{ trigger.to_state.context.id != none }}"
          - condition: template
            value_template: "{{ trigger.to_state.context.parent_id == none }}"
          - condition: template
            value_template: "{{ trigger.to_state.context.user_id == none }}"
        sequence:
          - service: input_boolean.turn_off
            data: {}
            target:
              entity_id:
                - input_boolean.study_motion_override
  - choose:
      - conditions:
          - condition: trigger
            id: Study Motion Override OFF
          - condition: template
            value_template: "{{ trigger.to_state.context.id != none }}"
          - condition: template
            value_template: "{{ trigger.to_state.context.parent_id == none }}"
          - condition: template
            value_template: "{{ trigger.to_state.context.user_id != none }}"
        sequence:
          - service: input_boolean.turn_off
            data: {}
            target:
              entity_id:
                - input_boolean.study_motion_override
  - choose:
      - conditions:
          - condition: trigger
            id: Study Motion Override OFF
          - condition: template
            value_template: "{{ trigger.to_state.context.id != none }}"
          - condition: template
            value_template: "{{ trigger.to_state.context.parent_id != none }}"
          - condition: template
            value_template: "{{ trigger.to_state.context.user_id == none }}"
        sequence:
          - service: input_boolean.turn_off
            data: {}
            target:
              entity_id:
                - input_boolean.study_motion_override
mode: single
  1. Automation for Motion Sensor
alias: Study Motion Lights Detection
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.esphome_demo_pir_sensor
    from: "off"
    to: "on"
condition: []
action:
  - choose:
      - conditions:
          - condition: state
            entity_id: input_boolean.study_motion_override
            state: "off"
          - condition: time
            after: "07:00:00"
            before: "23:00:00"
            weekday:
              - mon
              - tue
              - wed
              - thu
              - fri
              - sat
              - sun
        sequence:
          - service: switch.turn_on
            data: {}
            target:
              entity_id: switch.study_tube_light
          - delay:
              hours: 0
              minutes: 15
              seconds: 0
              milliseconds: 0
          - service: switch.turn_off
            data: {}
            target:
              entity_id: switch.study_tube_light
      - conditions:
          - condition: state
            entity_id: input_boolean.study_motion_override
            state: "off"
          - condition: time
            after: "23:01:00"
            before: "06:59:00"
            weekday:
              - mon
              - tue
              - wed
              - thu
              - fri
              - sat
              - sun
        sequence:
          - service: switch.turn_on
            data: {}
            target:
              entity_id: switch.study_lamp
          - delay:
              hours: 0
              minutes: 5
              seconds: 0
              milliseconds: 0
          - service: switch.turn_off
            data: {}
            target:
              entity_id: switch.study_lamp
mode: single