Skip to content

Instantly share code, notes, and snippets.

@curzon01
Created August 9, 2020 12:35
Show Gist options
  • Save curzon01/6981775312b9a2bd408d763c4fb9db44 to your computer and use it in GitHub Desktop.
Save curzon01/6981775312b9a2bd408d763c4fb9db44 to your computer and use it in GitHub Desktop.
Tasmota Rule to control roof window & shutter with optional rain sensor
# ======================================================================
# Tasmota Roof window + shutter (needs Tasmota >= v8.4 with Shutter)
# ======================================================================
# Uses 4 relais for two motors (1. motor is window, 2. motor is shutter)
# and 4 b buttons (2 up/down) for wallbuttons
# Optional input signal on ADC can be used for a rain sensor
# ======================================================================
#
# Wiring:
# ------
# GPIO16: Button1 Window Open
# GPIO5: Button2 Window Close
# GPIO4: Button3 Shutter Open
# GPIO0: Button4 Shutter Close
# GPIO13: Relay1i Window Open
# GPIO12: Relay2i Window Close
# GPIO14: Relay3i Shutter Open
# GPIO2: Relay4i Shutter Close
# ADC0: Range (GND switch with pull-up)
# Setup
# ======================================================================
Reset 2
Backlog Template {"NAME":"DFF","GPIO":[20,255,32,255,19,18,0,0,30,29,31,255,17],"FLAG":6,"BASE":18};Module 0
Backlog SetOption1 0;SetOption11 0;SetOption13 0;SetOption21 1;SetOption32 40;SetOption34 200;SetOption38 6;SetOption40 0;SetOption42 90;SetOption43 0;SetOption80 1;SetOption93 1;PowerRetain 0;SwitchMode1 3;SwitchMode2 3;SwitchMode3 3;SwitchMode4 3;PulseTime1 0;PulseTime2 0;PulseTime3 0;PulseTime4 0;Altitude 112;Interlock 1,2 3,4;Interlock ON
Backlog AdcParam 6,0,1023,0,100;ShutterRelay1 1;ShutterRelay2 3
WebButton1 Fenster ▲
WebButton2 Fenster ▼
WebButton3 Jalousie ▲
WebButton4 Jalousie ▼
Backlog Topic OG_sz_DFF;GroupTopic1 dff;GroupTopic2 OG_nn_DFF;GroupTopic3 ";GroupTopic4 "
Backlog ShutterMotorDelay1 0.0;ShutterOpenDuration1 59.5;ShutterCloseDuration1 59.5;ShutterEnableEndStopTime1 1
Backlog ShutterMotorDelay2 0.0;ShutterOpenDuration2 19.8;ShutterCloseDuration2 19.8;ShutterEnableEndStopTime2 1
Backlog ShutterButton1 1 100 50 74 - - - - - -;ShutterButton1 2 0 50 24 - - - - - -
; ShutterButton2 3 100 50 74 - - - - - -;ShutterButton2 4 0 50 24 - - - - - -
# Rain sensor function
======================
# The rain sensor controls the automatic closing of the window when it
# starts to rain. Optionally, the window can be opened again automatically
# after rain:
#
# * Close window shutter if rain signal (can be overruled by "RainSet") is set
# * Restores last window open position when it stops raining ("RainRestoreEnable").
# * In case of light rain, "RainRestoreDelay" could prevent the constant
# opening and closing when "RainRestoreEnable" is enabled.
# * Window can be optional close locked on rain using "RainLock"
# * When window is manual opened during rain, rain sensor and
# "RainRestoreEnable" is ignored until next "RainIgnoreDuration" seconds
# or when window is manually fully closed again. This overrules the rain
# sensor status for "RainIgnoreDuration" seconds because e.g. user has
# manuall slightly opened the window.
# TODO: Close window only if position greater than given value
# "RainWindowMinPosition"
# Implemented Events
# --------------------
# Settings can be changed using command "Event <x>":
# RainStatus
# returns current rain and window status as STATUS99
# RainSet=x
# overrules rain sensor rain
# 0=dry
# 1=rain
# RainSensor=x
# rain sensor
# 0 = disabled
# 1 = enabled
# RainLock=x
# window locked when raining
# 0 = unlocked
# 1 = locked
# RainRestoreEnable=x
# restore window position after rain
# 0 = do not restore
# 1 = restore
# RainRestoreDelay=x
# delay in [sec] before window position will be restored
# (prevents flapping)
# RainRestorePosition=x
# set different restore position if window is closed
# RainIgnore=x
# Set x=1 when window is manuall opened during rain.
# Can also be set using this event
# 0 = Do not ignore rain sensor
# 1 = Ignore rain/restore settings for "RainIgnoreDuration" [sec]
# RainIgnoreDuration=x
# "RainIgnore" duration in [sec]
#
# Rules & Vars
----------------------
# Dev
Backlog Rule1 ";Rule2 ";Rule3 ";Rule1 0;Rule2 0;Rule3 0;
Mem1 0;Mem2 0;Mem3 0;Mem4 0;Mem5 0;Mem6 1;Mem7 0;Mem8 1;Mem9 15;Mem10 0;Mem11 60;Mem12 0;Mem13 ";Mem14 ";Mem15 Publish2;Mem16 stat/tasmota
# Main
Backlog Rule1 ";Rule2 ";Rule3 ";Rule1 0;Rule2 0;Rule3 0;
Mem1 0;Mem2 0;Mem3 0;Mem4 0;Mem5 0;Mem6 1;Mem7 0;Mem8 1;Mem9 300;Mem10 0;Mem11 28800;Mem12 0;Mem13 ";Mem14 ";Mem15 Publish2;Mem16 stat/tasmota
# Runtime var and resources
# ----------------------
# Ruletimer1: Rain restore delay timer
# Ruletimer2: Rain ignore delay timer
# Var1: Rain sensor read status (1=rain, 0=dry)
# Var2: Current window position
#
# Mem1: Rain sensor status (1=rain, 0=dry)
# Mem2: Current window position after stop (0-100)
# Mem3: Window restore delay (1=running, 0=none)
# Mem4: Locked status (1=Window locked, 0=Window unlocked)
# Mem5: Window restore position (0-100)
# Mem12: Previous window position after stop (0-100)
# Settings var
# ----------------------
# Mem6: Rain sensor enable (1=enabled, 0=disabled)
# Mem7: Window lock enable (1=window locked on rain, 0=window unlocked on rain)
# Mem8: Window restore enable (1=enabled, 0=disabled)
# Mem9: Window restore delay [sec]
# Mem10: Ignore rain sensor & restore (1=ignore, 0=do not ignore)
# Mem11: Ignore rain sensor & restore duration [sec] or -1 for infinitely
# Mem15: Publish command
# Mem16: Status topic prefix (project-home/stat/tasmota)
# Setup
Backlog Rule1 0;Rule2 0;Rule3 0;Rule2 ";Rule3 ";Rule1 "
Rule1
ON System#Boot DO Backlog Event WLOCK=%Mem4%;ShutterPosition1 ENDON
ON Analog#Range>=50 DO Var1 0 ENDON
ON Analog#Range<50 DO Var1 1 ENDON
ON Var1#State==0 DO Mem1 0 ENDON
ON Var1#State!=0 DO IF (%Mem6%!=0 AND %Mem10%==0) Mem1 1 ELSE Mem1 0 ENDIF ENDON
ON Event#RainSet==0 DO Mem1 0 ENDON
ON Event#RainSet==1 DO Mem1 1 ENDON
ON Event#RainSensor==0 DO Mem6 0 ENDON
ON Event#RainSensor==1 DO Backlog Mem6 1;IF (%Mem10%==0) Mem1 %Var1% ENDIF ENDON
ON Event#RainLock==0 DO Mem7 0 ENDON
ON Event#RainLock==1 DO Mem7 1 ENDON
ON Event#RainRestoreEnable==0 DO Mem8 0 ENDON
ON Event#RainRestoreEnable==1 DO Mem8 1 ENDON
ON Event#RainIgnore==0 DO Event IGNORE=0 ENDON
ON Event#RainIgnore==1 DO Event IGNORE=1 ENDON
ON Shutter1#Button2=0 DO Restart 1 ENDON
ON Shutter2#Button2=0 DO Restart 1 ENDON
ON Wifi#Disconnected DO Restart 1 ENDON
Rule2
ON Mem1#State!=0 DO Backlog Event PSTORE;IF (%Mem10%==0) Event WCLOSE;Event WLOCK=%Mem7% ENDIF ENDON
ON Mem1#State==0 DO Backlog Event WLOCK=0;IF (%Mem8%!=0 AND %Mem10%==0) Event RDELAY=%Mem9% ENDIF ENDON
ON Mem2#State DO Backlog IF (%Mem1%==0 OR %Mem12%==0 OR %Mem2%!=0) Event PSTORE ENDIF;Event RainStatus ENDON
ON Mem7#State DO Backlog IF (%Mem1%!=0) Event WLOCK=%Mem7% ENDIF;Event RainStatus ENDON
ON Event#PSTORE DO IF (%Mem3%==0) Mem5 %Mem2% ENDIF ENDON
ON Event#RDELAY DO IF (%value%>0) Mem3 1;IF (%Mem5%!=%Mem2%) RuleTimer1 %value% ELSE Event RESTORE ENDIF ELSE Event RESTORE ENDIF ENDON
ON Event#WCLOSE DO Backlog RuleTimer1 0;ShutterPosition1 0 ENDON
ON Event#WLOCK DO Backlog Mem4 %value%;ShutterLock1 %value% ENDON
ON Event#IGNORE DO Backlog Mem10 %value%;IF (%value%!=0) IF (%Mem12%==0) Ruletimer2 %Mem11% ENDIF ELSE Ruletimer2 0 ENDIF ENDON
ON Event#RESTORE DO Backlog Mem3 0;IF (%Mem2%==0 AND %Mem8%!=0 AND %Mem10%==0) ShutterPosition1 %Mem5% ENDIF ENDON
ON Rules#Timer=1 DO Event RESTORE ENDON
ON Rules#Timer=2 DO Event IGNORE=0 ENDON
Rule3
ON Shutter1#Position DO Var2 %value% ENDON
ON Shutter1#Direction DO IF (%value%==0) Mem12 %Mem2%;Mem2 %Var2%;IF (%Mem1%!=0 AND %Var2%!=0) Event IGNORE=1 ENDIF;IF (%Var2%==0) Event IGNORE=0 ENDIF ENDIF ENDON
ON Event#RainRestoreDelay DO IF (%value%>=1 AND %value%<=3600) Mem9 %value% ENDIF ENDON
ON Event#RainRestorePosition DO IF (%value%>=0 AND %value%<=100) Mem5 %value% ENDIF ENDON
ON Event#RainIgnoreDuration DO IF (%value%>=-1 AND %value%<=65535) Mem11 %value% ENDIF ENDON
ON Event#RainStatus DO %Mem15% %Mem16%/%Topic%/STATUS99 {"StatusRN":{"Time":"%TIMESTAMP%","Sensor":{"Enabled":%Mem6%,"Input":%Var1%,"Status":%Mem1%},"Ignore":{"Delay":%Mem11%,"Status":%Mem10%},"Lock":{"Enabled":%Mem7%,"Status":%Mem4%},"Restore":{"Enabled":%Mem8%,"Delay":%Mem9%,"Position":%Mem5%}}} ENDON
ON Mem6#State DO Event RainStatus ENDON
ON Mem8#State DO Event RainStatus ENDON
ON Mem9#State DO Event RainStatus ENDON
ON Mem10#State DO Event RainStatus ENDON
ON Mem11#State DO Event RainStatus ENDON
ON Mem4#State DO Event RainStatus ENDON
ON Mem5#State DO Event RainStatus ENDON
ON Mem1#State DO Event RainStatus ENDON
Backlog Rule1 5;Rule1 1;Rule2 4;Rule2 1;Rule3 4;Rule3 1;ShutterPosition1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment