Compare commits

..

No commits in common. "master" and "v1.6.0" have entirely different histories.

2 changed files with 146 additions and 248 deletions

View File

@ -1,37 +0,0 @@
# Mike's Motion Activated Scenes
by Mike Peralta
This is another "motion activated light" automation blueprint for [Home Assistant](https://www.home-assistant.io/).
It uses scenes rather than toggling to allow for robust behavior and attempts to adjust its delay dynamically based on activity.
It features several stages after motion is no longer detected:
* Initial delay (nothing happens)
* Notice: Configurable scene + delay
* Warning: Configurable scene + delay
* Off
The initial delay starts off at a user configured number of seconds. Each time the user interrupts the off-sequence with motion,
the delay gets multiplied by a user configured multiplier, with a user configurable limit.
When the user allows the off-sequence to complete (the off-scene activates), the delay resets to the user configured baseline.
This allows a room to stay on longer when there is frequent activity, but shorter when activity is infrequent.
The automation also obeys a toggle helper to enable/disable motion activation. The user can configure which scene to use when when disabled.
This automation can retry a scene multiple times in a row to help overcome buggy/slow networks or lights that fail to set brightness/color correctly when first turned on.
## Disclaimer
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
By using this software you agree to the disclaimer.

View File

@ -122,21 +122,6 @@ blueprint:
entity:
domain: scene
scene_repeat_count:
name: "Scene repeat/retry count"
description: >
When activating a scene, try this many times in a row.
This is sometimes useful when the network seems buggy or laggy,
or for certain lights that fail to correctly apply brightness/color when first turning on.
default: 1
selector:
number:
min: 1
max: 60
step: 1
mode: box
on_just_before_off_seconds:
name: "\"On\" just before \"Off\""
@ -152,7 +137,7 @@ blueprint:
number:
min: 1
max: 86400
step: 0.1
step: 1
mode: box
@ -160,7 +145,7 @@ blueprint:
name: "Enabled helper"
description: >
Helper variable that will control whether motion activation is enabled or disabled.
This will allow the automation to correctly select the "On" or "Off" scene, based on whether motion is currently detected when disabled.
This will allow this blueprint to correctly select the "On" or "Off" scene, based on whether motion is currently detected when disabled.
An example would be a "Toggle" helper.
default: # Allows no selection
selector:
@ -197,23 +182,21 @@ blueprint:
variables:
motion_sensor: !input motion_sensor
delay_seconds_helper: !input delay_seconds_helper
delay_seconds_default: !input delay_seconds_default
delay_seconds_multiplier: !input delay_seconds_multiplier
delay_seconds_max: !input delay_seconds_max
notice_seconds: !input notice_seconds
warning_seconds: !input warning_seconds
on_just_before_off_seconds: !input on_just_before_off_seconds
enabled_helper: !input enabled_helper
disabled_scene: !input disabled_scene
debug_mode: !input debug_mode
motion_sensor: !input motion_sensor
delay_seconds_helper: !input delay_seconds_helper
delay_seconds_default: !input delay_seconds_default
delay_seconds_multiplier: !input delay_seconds_multiplier
delay_seconds_max: !input delay_seconds_max
notice_seconds: !input notice_seconds
warning_seconds: !input warning_seconds
on_just_before_off_seconds: !input on_just_before_off_seconds
enabled_helper: !input enabled_helper
disabled_scene: !input disabled_scene
debug_mode: !input debug_mode
scene_repeat_count: !input scene_repeat_count
scene_repeat_delay_milliseconds: 500
trigger_variables:
enabled_helper: !input enabled_helper
enabled_helper: !input enabled_helper
############
@ -246,16 +229,14 @@ trigger:
action:
# Action #0
- alias: "Initial debug logging"
- alias: "Debug log whether the enabled_helper was set"
if:
- condition: template
value_template: "{{ debug_mode == true }}"
then:
- alias: "Debug log whether the enabled_helper was set"
if:
- condition: template
value_template: "{{ not enabled_helper == None }}"
- if:
- condition: template
value_template: "{{ not enabled_helper == None }}"
then:
- service: logbook.log
data:
@ -267,26 +248,45 @@ action:
name: "Enabled helper"
message: "Enabled helper is not set."
- alias: "Debug log the enabled_helper name"
service: logbook.log
# Action #1
- alias: "Debug log the enabled_helper name"
if:
- condition: template
value_template: "{{ debug_mode == true }}"
then:
- service: logbook.log
data:
name: "Enabled helper"
message: "Enabled helper name is: {{ enabled_helper }}"
- alias: "Debug log the current delay_seconds value"
service: logbook.log
# Action #2
- alias: "Debug log the current delay_seconds value"
if:
- condition: template
value_template: "{{ debug_mode == true }}"
then:
- service: logbook.log
data:
name: "Delay seconds"
message: "Delay seconds is: {{ states[delay_seconds_helper].state }}"
- alias: "Debug log the on_just_before_off_seconds value"
service: logbook.log
data:
name: "Debug: On just before off seconds"
message: "On just before off is: {{ on_just_before_off_seconds }} "
# Action #3
- alias: "Debug log the on_just_before_off_seconds value"
if:
- condition: template
value_template: "{{ debug_mode == true }}"
then:
service: logbook.log
data:
name: "Debug: On just before off seconds"
message: "On just before off is: {{ on_just_before_off_seconds }} "
# Take actions based on whether the "Enabled helper" was just toggled on or off
# Action #1
# Action #4
- if:
- condition: trigger
id:
@ -295,34 +295,25 @@ action:
then:
# Debug log
- if:
- condition: template
value_template: "{{ debug_mode == true }}"
- alias: "Debug log - Enabled Helper On/Off"
if:
- condition: template
value_template: "{{ debug_mode == true }}"
then:
- alias: "Debug log that the enabled helper is toggled"
service: logbook.log
- service: logbook.log
data:
name: "Debug: Trigger"
message: "Enabled helper was toggled, and has triggered this automation"
- alias: "Debug log the disabled scene"
service: logbook.log
- alias: "Debug log - Disabled scene"
if:
- condition: template
value_template: "{{ debug_mode == true }}"
then:
- service: logbook.log
data:
name: "Debug: Disabled scene"
message: "Disabled scene is currently set to: {{ disabled_scene }}"
# If the enabled helper is OFF, we should start by resetting
# the countdown timer to default
- alias: "If the enabled helper is OFF, reset countdown timer to default"
if:
- condition: trigger
id:
- Enabled Helper is off
then:
- alias: "Reset countdown timer when enabled helper is switched OFF."
service: input_number.set_value
data:
entity_id: !input delay_seconds_helper
value: "{{ delay_seconds_default }}"
# In "auto" mode, activate the ON or OFF scene based on whether motion is currently detected
# Also use "auto" mode if motion activation is ENABLED.
@ -336,92 +327,66 @@ action:
entity_id: !input motion_sensor
state: "on"
then:
- alias: "Activate the ON scene"
repeat:
count: "{{ scene_repeat_count }}"
sequence:
- service: scene.turn_on
target:
entity_id: !input scene_on
- delay:
milliseconds: "{{ scene_repeat_delay_milliseconds }}"
- parallel:
- alias: "Activate the ON scene"
service: scene.turn_on
target:
entity_id: !input scene_on
else:
- alias: "Activate the OFF scene"
repeat:
count: "{{ scene_repeat_count }}"
sequence:
- service: scene.turn_on
target:
entity_id: !input scene_off
- delay:
milliseconds: "{{ scene_repeat_delay_milliseconds }}"
- stop: "Enabled helper was on or auto mode was on; Determined scene to activate based on current motion."
- parallel:
- alias: "Activate the OFF scene"
service: scene.turn_on
target:
entity_id: !input scene_off
- stop: ""
# At this point, the enabled helper is assumed to be OFF
# In disabled_scene::ON mode, just activate the ON scene
- alias: "disabled_scene::on mode: just activate the ON scene"
# In ON mode, just activate the ON scene
- alias: "on mode: just activate the ON scene"
if:
- condition: template
value_template: "{{ disabled_scene == 'on' }}"
then:
- repeat:
count: "{{ scene_repeat_count }}"
sequence:
- service: scene.turn_on
target:
entity_id: !input scene_on
- delay:
milliseconds: "{{ scene_repeat_delay_milliseconds }}"
- stop: "Enabled helper is off and disabled scene was ON; Set ON scene"
- parallel:
- service: scene.turn_on
target:
entity_id: !input scene_on
- stop: ""
# In disabled_scene::OFF mode, just activate the OFF scene
- alias: "disabled_scene::off mode: just activate the OFF scene"
# In OFF mode, just activate the OF scene
- alias: "off mode: just activate the ON scene"
if:
- condition: template
value_template: "{{ disabled_scene == 'off' }}"
then:
- repeat:
count: "{{ scene_repeat_count }}"
sequence:
- service: scene.turn_on
target:
entity_id: !input scene_off
- delay:
milliseconds: "{{ scene_repeat_delay_milliseconds }}"
- stop: "Enabled helper is off and disabled scene was OFF; Set OFF scene"
- parallel:
- service: scene.turn_on
target:
entity_id: !input scene_off
- stop: ""
# In disabled_scene::notice mode, just activate the NOTICE scene
- alias: "disabled_scene::notice mode: just activate the NOTICE scene"
# In notice mode, just activate the NOTICE scene
- alias: "notice mode: just activate the NOTICE scene"
if:
- condition: template
value_template: "{{ disabled_scene == 'notice' }}"
then:
- repeat:
count: "{{ scene_repeat_count }}"
sequence:
- service: scene.turn_on
target:
entity_id: !input scene_notice
- delay:
milliseconds: "{{ scene_repeat_delay_milliseconds }}"
- stop: "Enabled helper is off and disabled scene was NOTICE; Set NOTICE scene"
- parallel:
- service: scene.turn_on
target:
entity_id: !input scene_notice
- stop: ""
# In disabled_scene::warning mode, just activate the WARNING scene
- alias: "disabed_scene::warning mode: just activate the WARNING scene"
# In warning mode, just activate the WARNING scene
- alias: "warning mode: just activate the WARNING scene"
if:
- condition: template
value_template: "{{ disabled_scene == 'warning' }}"
then:
- repeat:
count: "{{ scene_repeat_count }}"
sequence:
- service: scene.turn_on
target:
entity_id: !input scene_warning
- delay:
milliseconds: "{{ scene_repeat_delay_milliseconds }}"
- stop: "Enabled helper is off and disabled scene was WATNING; Set WARNING scene"
- parallel:
- service: scene.turn_on
target:
entity_id: !input scene_warning
- stop: ""
# Don't do anything after processing the enabled_helper triggers
- stop: "Done handling enabled helper"
@ -430,7 +395,6 @@ action:
# Take actions based on whether Motion is detected
# Action #2
- alias: "Refuse to do anything if the enabled-helper says we're OFF."
if:
- condition: template
@ -459,21 +423,16 @@ action:
name: "Motion On"
message: "Motion was detected."
- alias: "Activate the ON scene"
repeat:
count: "{{ scene_repeat_count }}"
sequence:
- service: scene.turn_on
target:
entity_id: !input scene_on
- delay:
milliseconds: "{{ scene_repeat_delay_milliseconds }}"
- parallel:
- alias: "Activate the ON scene"
service: scene.turn_on
target:
entity_id: !input scene_on
# Motion is not detected
else:
# Debug log
- alias: "Debug Log that the motion-off sequence will run, due to motion no longer detected."
if:
- condition: template
@ -484,10 +443,11 @@ action:
name: "Motion Off"
message: "Initiating motion-off sequence, because motion no longer detected."
# Initial period where nothing happens
- alias: "Delay appropriately before doing anything"
wait_template: "{{ states[motion_sensor].state == 'on' or (enabled_helper != None and is_state(enabled_helper, 'off') ) }}"
timeout: "{{ states[delay_seconds_helper].state }}"
wait_template: "{{ states[motion_sensor].state == 'on' }}"
timeout: "{{ states[delay_seconds_helper].state or (enabled_helper != None and is_state(enabled_helper, 'off') ) }}"
- alias: "Quit now if the enabled-helper went OFF"
if:
@ -496,10 +456,9 @@ action:
- condition: template
value_template: "{{ is_state( enabled_helper, 'off' ) }}"
then:
stop: "Interrupted initial delay because the enabled helper turned OFF."
stop: "Quitting early because the enabled-helper went OFF."
- alias: "Motion detected again during initial delay; Interrupt initial delay"
if:
- if:
- condition: state
entity_id: !input motion_sensor
state: "on"
@ -527,17 +486,11 @@ action:
data:
name: "Begin notice period"
message: "Begin notice period; Waiting while motion is still off."
- alias: "Activate Scene: Notice"
repeat:
count: "{{ scene_repeat_count }}"
sequence:
- service: scene.turn_on
target:
entity_id: !input scene_notice
- delay:
milliseconds: "{{ scene_repeat_delay_milliseconds }}"
- parallel:
- alias: "Activate Scene: Notice"
service: scene.turn_on
target:
entity_id: !input scene_notice
- alias: "Delay during the notice period"
wait_template: "{{ states[motion_sensor].state == 'on' or (enabled_helper != None and is_state('enabled_helper', 'off') ) }}"
timeout: "{{ notice_seconds }}"
@ -549,21 +502,19 @@ action:
- condition: template
value_template: "{{ is_state( enabled_helper, 'off' ) }}"
then:
stop: "Interrupting NOTICE period because the enabled-helper went OFF."
stop: "Quitting early because the enabled-helper went OFF."
- alias: "If motion interrupted the NOTICE period, activate the ON scene"
if:
- condition: state
entity_id: !input motion_sensor
state: "on"
- if:
- condition: state
entity_id: !input motion_sensor
state: "on"
then:
- alias: "Activate Scene: On"
service: scene.turn_on
target:
entity_id: !input scene_on
- parallel:
- alias: "Activate Scene: On"
service: scene.turn_on
target:
entity_id: !input scene_on
- alias: "Increase countdown timer when user interrupts the notice period."
service: input_number.set_value
data:
@ -616,21 +567,16 @@ action:
data:
name: "Begin warning period"
message: "Begin warning period; Waiting while motion is still off."
- alias: "Activate Scene: Warning"
repeat:
count: "{{ scene_repeat_count }}"
sequence:
- service: scene.turn_on
target:
entity_id: !input scene_warning
- delay:
milliseconds: "{{ scene_repeat_delay_milliseconds }}"
- parallel:
- alias: "Activate Scene: Warning"
service: scene.turn_on
target:
entity_id: !input scene_warning
- alias: "Delay during the warning period"
wait_template: "{{ states[motion_sensor].state == 'on' or (enabled_helper != None and is_state('enabled_helper', 'off') ) }}"
timeout: "{{ warning_seconds }}"
- alias: "Quit now if the enabled-helper went OFF"
if:
- condition: template
@ -638,31 +584,25 @@ action:
- condition: template
value_template: "{{ is_state( enabled_helper, 'off' ) }}"
then:
stop: "Interrupting the WARNING sequence because the enabled-helper went OFF."
stop: "Quitting early because the enabled-helper went OFF."
- alias: "If motion interrupts the WARNING sequence, just activate the ON scene"
if:
- condition: state
entity_id: !input motion_sensor
state: "on"
- if:
- condition: state
entity_id: !input motion_sensor
state: "on"
then:
- alias: "Activate Scene: On"
repeat:
count: "{{ scene_repeat_count }}"
sequence:
- service: scene.turn_on
target:
entity_id: !input scene_on
- delay:
milliseconds: "{{ scene_repeat_delay_milliseconds }}"
- parallel:
- alias: "Activate Scene: On"
service: scene.turn_on
target:
entity_id: !input scene_on
- alias: "Double countdown timer when user interrupts the warning period."
service: input_number.set_value
data:
entity_id: !input delay_seconds_helper
value: "{{ states[delay_seconds_helper].state | int * delay_seconds_multiplier }}"
- alias: "Limit delay seconds to its defined maximum"
if:
- condition: template
@ -680,7 +620,6 @@ action:
data:
name: "Limiting delay seconds"
message: "Delay limited to max: {{ states[delay_seconds_helper].state }}"
- alias: "Log new delay seconds"
if:
- condition: template
@ -733,15 +672,11 @@ action:
name: "Won't run on-before-off"
message: "Won't run on-before-off because seconds was less than 1"
- alias: "Activate Scene: Off (Done)"
repeat:
count: "{{ scene_repeat_count }}"
sequence:
- service: scene.turn_on
target:
entity_id: !input scene_off
- delay:
milliseconds: "{{ scene_repeat_delay_milliseconds }}"
- parallel:
- alias: "Activate Scene: Off (Done)"
service: scene.turn_on
target:
entity_id: !input scene_off
- alias: "Reset countdown timer after turning off lights."
service: input_number.set_value