From 65a542484db228ece102620b75f5f4762bf4eb9f Mon Sep 17 00:00:00 2001 From: mike Date: Wed, 27 Dec 2023 21:59:46 -0800 Subject: [PATCH] Switch strategy: No longer "on before off" but instead just retry each scene a configurable number of times. --- mikes-motion-activated-scenes.yaml | 231 ++++++++++++++++++----------- 1 file changed, 142 insertions(+), 89 deletions(-) diff --git a/mikes-motion-activated-scenes.yaml b/mikes-motion-activated-scenes.yaml index 38cdfc7..9147191 100644 --- a/mikes-motion-activated-scenes.yaml +++ b/mikes-motion-activated-scenes.yaml @@ -122,30 +122,45 @@ blueprint: entity: domain: scene - - on_just_before_off_seconds: - name: "\"On\" just before \"Off\"" + scene_repeat_count: + name: "Scene repeat/retry count" description: > - When the "Off" scene is about to be shown, you may wish to quickly show the "On" scene for a number of seconds just before. - This is sometimes helpful to mitigate an issue where the "On" scene doesn't apply all colors and settings correctly from an "Off" state. - Set this to a value greater than 0 to enable. - A value of 0 will disable this feature. - Note that sometimes, a value that is too short (e.g., 1) may also cause issues. - + Whenever activating a scene, try this many times at 1-second intervals. + 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: 86400 + max: 60 step: 1 mode: box +# on_just_before_off_seconds: +# name: "\"On\" just before \"Off\"" +# description: > +# When the "Off" scene is about to be shown, you may wish to quickly show the "On" scene for a number of seconds just before. +# This is sometimes helpful to mitigate an issue where the "On" scene doesn't apply all colors and settings correctly from an "Off" state. +# Set this to a value greater than 0 to enable. +# A value of 0 will disable this feature. +# Note that sometimes, a value that is too short (e.g., 1) may also cause issues. +# +# default: 1 +# selector: +# number: +# min: 1 +# max: 86400 +# step: 1 +# mode: box + + enabled_helper: name: "Enabled helper" description: > Helper variable that will control whether motion activation is enabled or disabled. - This will allow this blueprint to correctly select the "On" or "Off" scene, based on whether motion is currently detected when disabled. + This will allow the automation 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: @@ -189,11 +204,12 @@ variables: 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 +# 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 trigger_variables: enabled_helper: !input enabled_helper @@ -262,11 +278,11 @@ action: 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 }} " +# - 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 }} " # Take actions based on whether the "Enabled helper" was just toggled on or off # Action #1 @@ -278,26 +294,21 @@ action: then: # Debug log - - alias: "Debug log - Enabled Helper On/Off" - if: - - condition: template - value_template: "{{ debug_mode == true }}" + - if: + - condition: template + value_template: "{{ debug_mode == true }}" then: - - service: logbook.log + - alias: "Debug log that the enabled helper is toggled" + service: logbook.log data: name: "Debug: Trigger" message: "Enabled helper was toggled, and has triggered this automation" - - alias: "Debug log - Disabled scene" - if: - - condition: template - value_template: "{{ debug_mode == true }}" - then: - - service: logbook.log + - alias: "Debug log the disabled scene" + service: logbook.log data: name: "Debug: Disabled scene" message: "Disabled scene is currently set to: {{ disabled_scene }}" - # 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. - alias: "auto mode: Activate ON scene, if motion is currently detected, otherwise activate the OFF scene" @@ -327,20 +338,30 @@ action: - condition: template value_template: "{{ disabled_scene == 'on' }}" then: - - service: scene.turn_on - target: - entity_id: !input scene_on + - repeat: + count: "{{ scene_repeat_count }}" + sequence: + - service: scene.turn_on + target: + entity_id: !input scene_on + - delay: + seconds: 1 - stop: "" - # In OFF mode, just activate the OF scene + # In OFF mode, just activate the OFF scene - alias: "off mode: just activate the ON scene" if: - condition: template value_template: "{{ disabled_scene == 'off' }}" then: - - service: scene.turn_on - target: - entity_id: !input scene_off + - repeat: + count: "{{ scene_repeat_count }}" + sequence: + - service: scene.turn_on + target: + entity_id: !input scene_off + - delay: + seconds: 1 - stop: "" # In notice mode, just activate the NOTICE scene @@ -349,9 +370,14 @@ action: - condition: template value_template: "{{ disabled_scene == 'notice' }}" then: - - service: scene.turn_on - target: - entity_id: !input scene_notice + - repeat: + count: "{{ scene_repeat_count }}" + sequence: + - service: scene.turn_on + target: + entity_id: !input scene_notice + - delay: + seconds: 1 - stop: "" # In warning mode, just activate the WARNING scene @@ -360,9 +386,14 @@ action: - condition: template value_template: "{{ disabled_scene == 'warning' }}" then: - - service: scene.turn_on - target: - entity_id: !input scene_warning + - repeat: + count: "{{ scene_repeat_count }}" + sequence: + - service: scene.turn_on + target: + entity_id: !input scene_warning + - delay: + seconds: 1 - stop: "" # Don't do anything after processing the enabled_helper triggers @@ -402,14 +433,20 @@ action: message: "Motion was detected." - alias: "Activate the ON scene" - service: scene.turn_on - target: - entity_id: !input scene_on + repeat: + count: "{{ scene_repeat_count }}" + sequence: + - service: scene.turn_on + target: + entity_id: !input scene_on + - delay: + seconds: 1 # 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 @@ -420,7 +457,6 @@ 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' }}" @@ -465,9 +501,14 @@ action: message: "Begin notice period; Waiting while motion is still off." - alias: "Activate Scene: Notice" - service: scene.turn_on - target: - entity_id: !input scene_notice + repeat: + count: "{{ scene_repeat_count }}" + sequence: + - service: scene.turn_on + target: + entity_id: !input scene_notice + - delay: + seconds: 1 - alias: "Delay during the notice period" wait_template: "{{ states[motion_sensor].state == 'on' or (enabled_helper != None and is_state('enabled_helper', 'off') ) }}" @@ -548,15 +589,19 @@ action: message: "Begin warning period; Waiting while motion is still off." - alias: "Activate Scene: Warning" - service: scene.turn_on - target: - entity_id: !input scene_warning + repeat: + count: "{{ scene_repeat_count }}" + sequence: + - service: scene.turn_on + target: + entity_id: !input scene_warning + - delay: + seconds: 1 - 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 @@ -566,8 +611,6 @@ action: then: stop: "Quitting early because the enabled-helper went OFF." - - - if: - condition: state entity_id: !input motion_sensor @@ -575,9 +618,14 @@ action: then: - alias: "Activate Scene: On" - service: scene.turn_on - target: - entity_id: !input scene_on + repeat: + count: "{{ scene_repeat_count }}" + sequence: + - service: scene.turn_on + target: + entity_id: !input scene_on + - delay: + seconds: 1 - alias: "Double countdown timer when user interrupts the warning period." service: input_number.set_value @@ -626,39 +674,44 @@ action: # Finally, we decide to actually turn off the lights with the "Off" scene. # Maybe turn on the "On" scene just before the "Off" scene, if the user enabled this option - - if: - - condition: template - value_template: "{{ on_just_before_off_seconds > 0 }}" - then: - - alias: "Log the number of on-before-off seconds" - if: - - condition: template - value_template: "{{ debug_mode == true }}" - then: - service: logbook.log - data: - name: "On-Before-Off seconds" - message: "Will turn on for {{ on_just_before_off_seconds }} seconds before off." - - service: scene.turn_on - target: - entity_id: !input scene_on - - alias: "Wait a second to set the \"On\" scene." - delay: - seconds: "{{ on_just_before_off_seconds }}" - else: - - if: - - condition: template - value_template: "{{ debug_mode == true }}" - then: - - service: logbook.log - data: - name: "Won't run on-before-off" - message: "Won't run on-before-off because seconds was less than 1" +# - if: +# - condition: template +# value_template: "{{ on_just_before_off_seconds > 0 }}" +# then: +# - alias: "Log the number of on-before-off seconds" +# if: +# - condition: template +# value_template: "{{ debug_mode == true }}" +# then: +# service: logbook.log +# data: +# name: "On-Before-Off seconds" +# message: "Will turn on for {{ on_just_before_off_seconds }} seconds before off." +# - service: scene.turn_on +# target: +# entity_id: !input scene_on +# - alias: "Wait a second to set the \"On\" scene." +# delay: +# seconds: "{{ on_just_before_off_seconds }}" +# else: +# - if: +# - condition: template +# value_template: "{{ debug_mode == true }}" +# then: +# - service: logbook.log +# data: +# 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)" - service: scene.turn_on - target: - entity_id: !input scene_off + repeat: + count: "{{ scene_repeat_count }}" + sequence: + - service: scene.turn_on + target: + entity_id: !input scene_off + - delay: + seconds: 1 - alias: "Reset countdown timer after turning off lights." service: input_number.set_value