From 6ff83b7acca705bbeca565783eb9c14129118f81 Mon Sep 17 00:00:00 2001
From: "Suren A. Chilingaryan" <csa@suren.me>
Date: Thu, 13 Jun 2024 05:41:46 +0400
Subject: Allow some delay for Aqara sensors to propogate luminance after
 motion started (in motion_switch blueprint). Use luminance of presence sensor
 in kitchen automation

---
 blueprints/automation/motion/motion_switch.yaml | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

(limited to 'blueprints')

diff --git a/blueprints/automation/motion/motion_switch.yaml b/blueprints/automation/motion/motion_switch.yaml
index 67e62b6..dd4111e 100644
--- a/blueprints/automation/motion/motion_switch.yaml
+++ b/blueprints/automation/motion/motion_switch.yaml
@@ -188,8 +188,9 @@ action:
                 conditions:
                   - condition: template
                     value_template: '{{ test == "sensor" }}'
-                  - condition: template
-                    value_template: '{{ (states(illuminance_entity) | float) < lux_threshold }}'
+# This will be checked inside to allow short wait
+#                  - condition: template
+#                    value_template: '{{ (states(illuminance_entity) | float) < lux_threshold }}'
               - condition: and
                 conditions:
                   - condition: template
@@ -224,6 +225,17 @@ action:
                     seconds: 2
                   continue_on_timeout: false
 
+# On motion sensors, illuminance is updated only on motion. We need to give some time to propagate
+          - if:
+              - condition: template
+                value_template: '{{ test == "sensor" }}'
+              - condition: template
+                value_template: '{{ (states(illuminance_entity) | float) >= lux_threshold }}'
+            then:
+              - wait_template: '{{ (states(illuminance_entity) | float) < lux_threshold }}'
+                timeout: '00:00:02'  # Timeout after 1 minute
+                continue_on_timeout: false
+
           - choose: []
             default: !input "on_click"
           - service: switch.turn_on
-- 
cgit v1.2.3