From 6a49d1223a7eeb6d02d383b1883363698b5eec0b Mon Sep 17 00:00:00 2001 From: Michael Ingraham <34340210+meingraham@users.noreply.github.com> Date: Mon, 12 Aug 2019 12:01:44 -0400 Subject: [PATCH] Turn On Light Before Dawn and At Dusk --- Rule-Cookbook.md | 64 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/Rule-Cookbook.md b/Rule-Cookbook.md index f6912d62..8b3f370a 100644 --- a/Rule-Cookbook.md +++ b/Rule-Cookbook.md @@ -16,6 +16,7 @@ - [Toggle a Relay only when holding the button for 2 seconds](#toggle-a-relay-only-when-holding-the-button-for-2-seconds) - Using Sunrise and Sunset - [Make sure light is on at night](#make-sure-light-is-on-at-night) + - [Turn On Light Before Dawn and At Dusk](#Turn-On-Light-Before-Dawn-and-At-Dusk) - [Enable a PIR Switch only at night](#enable-a-pir-switch-only-at-night) - [PIR Configuration](Wemos-D1-Mini-and-HC-SR501-PIR-Motion-Sensor#alternative-tasmota-configuration-with-rules-recommended-method) - [Using clock timer to control a luminance-triggered switch only in mornings](#using-clock-timer-to-control-a-luminance-triggered-switch-only-in-mornings) @@ -626,6 +627,69 @@ The previous rules are conditionals that represent the following logic: IF %time%>%sunset DO power1 1 / IF %time%<%sunrise DO power1 1 +[Back To Top](#top) + +------------------------------------------------------------------------------ + +#### Turn On Light Before Dawn and At Dusk +Turn on light at dusk until your nighttime and again in the morning before dawn. + +What if the sun sets after your nighttime, as in during the summer? Then the timer will turn off the light at "night", but then the Sunset timer will turn it on again, so it stays on all night. +``` +Rule1 +on Time#Initialized do event chkSun endon +on Time#Minute=%sunset% do event chkSun endon +on event#chkSun do backlog var1 0; event chkSunrise=%time%; event chkSunset=%time%; event chkmorn=%time%; event chknight=%time%; event setPower endon +on event#chkSunrise<%sunrise% do var1 1 endon +on event#chkSunset>=%sunset% do var1 1 endon +on event#chkmorn<%mem1% do var1 0 endon +on event#chknight>=%mem2% do var1 0 endon +on event#setPower do power1 %var1% endon +``` + +``` +Backlog mem1 360; mem2 1350 +Rule1 1 +``` + +- **Explanation:** + \# When device restarts, calculate if the light should be on or off + `on Time#Initialized do event chkSun endon` + + \# At Sunset, calculate if the light should be on or off + `on Time#Minute=%sunset% do event chkSun endon` + + \# Calculate if the light should be on or off + `on event#chkSun do backlog ` + +     \# Assume off +     `var1 0; ` + +     \# Trigger each event with the current time +     `event chkSunrise=%time%; event chkSunset=%time%; event chkmorn=%time%; event chknight=%time%; event setPower ` + + `endon` + + \# If before sunrise, turn on + `on event#chkSunrise<%sunrise% do var1 1 endon` + + \# If past sunset, turn on + `on event#chkSunset>=%sunset% do var1 1 endon` + + \# But if before Morning time (`mem1`), do not turn on + `on event#chkmorn<%mem1% do var1 0 endon` + + \# Or if after Night time (`mem2`), do not turn on + `on event#chknight>=%mem2% do var1 0 endon` + + \# Perform on/off state + `on event#setPower do power1 %var1% endon` + + \# Set variables for Morning (06h00) and Night (22h30) times + `Backlog mem1 360; mem2 1350` + + \# Turn on the rule set + `Rule1 1` [Back To Top](#top)