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.