diff --git a/Rule-Cookbook.md b/Rule-Cookbook.md index 540820b1..b7186be7 100644 --- a/Rule-Cookbook.md +++ b/Rule-Cookbook.md @@ -72,11 +72,15 @@ Hardware - Sonoff 4CH Software -- Tasmota with USE_RULES enabled +- Tasmota compiled with `#define USE_RULES` - Disable ButtonTopic as it overrides rules for buttons: `ButtonTopic 0` Rule -- `Rule1 on button1#state do publish cmnd/ring2/power %value% endon on button2#state do publish cmnd/strip1/power %value% endon` +``` +Rule1 + on button1#state do publish cmnd/ring2/power %value% endon + on button2#state do publish cmnd/strip1/power %value% endon +``` (You will likely need to enable this rule if it's the first time you've used them) "Rule1 on". @@ -96,7 +100,7 @@ Hardware - Sonoff 4CH Software -- Tasmota with `USE_RULES` enabled +- Tasmota compiled with `#define USE_RULES` - Configure timer5 for rule execution when activated: `timer5 {"Arm":1,"Mode":0,"Time":"16:00","Days":"1111111","Repeat":1,"Action":3}` - Rule @@ -116,7 +120,10 @@ If you want to have blink functionality define a rule like `on clock#Timer=5 do The rule command once option provides the possibility to trigger only once on a slow change while the change is still within the bounds of the test. Rule -- `on ENERGY#Current>0.100 do publish tool/tablesaw/power 1 endon on ENERGY#Current<0.100 do publish tool/tablesaw/power 0 endon` +``` +on ENERGY#Current>0.100 do publish tool/tablesaw/power 1 endon +on ENERGY#Current<0.100 do publish tool/tablesaw/power 0 endon +``` This creates a rule to publish MQTT commands whenever a Sonoff POW has current passing through it. Used as is, it will publish MQTT commands repeatedly, over and over, while current is >0.100 ... but, executing another command: @@ -139,11 +146,14 @@ Hardware - AM2301 Temperature and Humidity sensor Software -- Tasmota with USE_RULES enabled +- Tasmota compiled with `#define USE_RULES` - Home Automation tool Domoticz configured with a virtual sensor Temp+Hum using Idx 134 Rule -- `on tele-am2301-12#temperature do var1 %value% endon on tele-am2301-12#humidity do publish domoticz/in {"idx":134,"svalue":"%var1%;%value%;1"} endon` +``` +on tele-am2301-12#temperature do var1 %value% endon +on tele-am2301-12#humidity do publish domoticz/in {"idx":134,"svalue":"%var1%;%value%;1"} endon +``` Result - As a result of the `tele-` prefix the rules will be checked at TelePeriod time for sensor AM2301-12 Temperature and Humidity. The first rule will use the Temperature stored in `%value%` and save it in `%var1%` for future use. The second rule will use the Humidity stored in `%value%` and the Temperature stored in `%var1%` to compose a single MQTT message suitable for Domoticz. @@ -164,7 +174,7 @@ Hardware - WS2812 LED Software -- Tasmota with USE_RULES enabled +- Tasmota compiled with `#define USE_RULES` Rule - `on analog#a0div10 do dimmer %value% endon` @@ -233,12 +243,12 @@ Demonstrate the use of variables. Make sure to execute commands `Rule 4`(Disable * Everything together: ``` Rule1 - on event#togglevar1 do event toggling1=%var1% endon - on event#toggling1<1 do event setvar1=1 endon - on event#toggling1>0 do event setvar1=0 endon - on event#setvar1 do var1 %value% endon - on event#getvar1 do var1 endon - on event#message do publish stat/mqttTopic/log %value% endon + on event#togglevar1 do event toggling1=%var1% endon + on event#toggling1<1 do event setvar1=1 endon + on event#toggling1>0 do event setvar1=0 endon + on event#setvar1 do var1 %value% endon + on event#getvar1 do var1 endon + on event#message do publish stat/mqttTopic/log %value% endon ``` **NOTE** @@ -259,9 +269,9 @@ If a device has more than one relay and LEDs on different GPIOs (not connected t Backlog ledmask 0x0000; setoption13 1; seriallog 0 rule1 -on power1#state do power4 %value% endon -on power2#state do power5 %value% endon -on power3#state do power6 %value% endon + on power1#state do power4 %value% endon + on power2#state do power5 %value% endon + on power3#state do power6 %value% endon rule1 1 ``` @@ -309,9 +319,9 @@ On boot start a watchdog timer to check temp sensor connection. An available button is configured as switch to set thermostat ON or OFF ``` Rule1 -on switch1#state do backlog event toggling1=%mem1% endon -on event#toggling1=0 do mem 1 endon -on event#toggling1=1 do mem 0 endon + on switch1#state do backlog event toggling1=%mem1% endon + on event#toggling1=0 do mem 1 endon + on event#toggling1=1 do mem 0 endon ``` Check temp sensor connection. If fails, set to off and turn off thermostat. Also continue checking @@ -321,7 +331,11 @@ Resets checking timer if temperature is connected `on tele-SI7021#temperature do backlog var1 1; RuleTimer1 30; event ctrl_ready=1; event temp_demand=%value% endon` Thermostat control - upper limit and lower limit and enabled -`on on event#ctrl_ready>%mem1% do var1 0 endon on event#temp_demand>%mem2% do power1 0 endon on event#temp_demand<%mem3% do power1 %var1% endon` +``` +on on event#ctrl_ready>%mem1% do var1 0 endon +on event#temp_demand>%mem2% do power1 0 endon +on event#temp_demand<%mem3% do power1 %var1% endon +``` Thermostat can be turned On by: @@ -354,30 +368,30 @@ RULES: ``` Rule1 -on system#boot do RuleTimer1 70 endon -on Switch1#State do event toggling1=%mem1% endon -on event#toggling1=0 do mem 1 endon -on event#toggling1=1 do mem 0 endon -on Rules#Timer=1 do backlog var1 0; RuleTimer1 70; power1 0 endon -on tele-SI7021#temperature do backlog var1 1; RuleTimer1 70; event ctrl_ready=1; event temp_demand=%value% endon -on event#ctrl_ready>%mem1% do var1 0 endon -on event#temp_demand>%mem2% do power1 0 endon -on event#temp_demand<%mem3% do power1 %var1% endon + on system#boot do RuleTimer1 70 endon + on Switch1#State do event toggling1=%mem1% endon + on event#toggling1=0 do mem 1 endon + on event#toggling1=1 do mem 0 endon + on Rules#Timer=1 do backlog var1 0; RuleTimer1 70; power1 0 endon + on tele-SI7021#temperature do backlog var1 1; RuleTimer1 70; event ctrl_ready=1; event temp_demand=%value% endon + on event#ctrl_ready>%mem1% do var1 0 endon + on event#temp_demand>%mem2% do power1 0 endon + on event#temp_demand<%mem3% do power1 %var1% endon ``` EXAMPLE RULES WITHOUT TEMP SENSOR TO TEST THE THERMOSTAT RULES ``` Rule1 -on system#boot do RuleTimer1 70 endon -on Switch1#State do event toggling1=%mem1% endon -on event#toggling1=0 do mem 1 endon -on event#toggling1=1 do mem 0 endon -on Rules#Timer=1 do backlog var1 0; RuleTimer1 70; power1 0 endon -on event#temp do backlog var1 1; RuleTimer1 70; event ctrl_ready=1; event temp_demand=%value% endon -on event#ctrl_ready>%mem1% do var1 0 endon -on event#temp_demand>%mem2% do power1 0 endon -on event#temp_demand<%mem3% do power1 %var1% endon + on system#boot do RuleTimer1 70 endon + on Switch1#State do event toggling1=%mem1% endon + on event#toggling1=0 do mem 1 endon + on event#toggling1=1 do mem 0 endon + on Rules#Timer=1 do backlog var1 0; RuleTimer1 70; power1 0 endon + on event#temp do backlog var1 1; RuleTimer1 70; event ctrl_ready=1; event temp_demand=%value% endon + on event#ctrl_ready>%mem1% do var1 0 endon + on event#temp_demand>%mem2% do power1 0 endon + on event#temp_demand<%mem3% do power1 %var1% endon ``` TESTS: @@ -391,7 +405,8 @@ TESTS: TIMERS: * With the above the timers can be used to control mem1 and add a schedule to when the thermostat will be enabled - `Rule2 on Clock#Timer=1 do mem 1 endon on Clock#Timer=2 do mem 0 endon` + `Rule2 on Clock#Timer=1 do mem 1 endon +on Clock#Timer=2 do mem 0 endon` [Back To Top](#top) @@ -416,6 +431,7 @@ In a swimming pool, a filter pump and a solar panel is installed. When the sun i ``` mem3 25 + rule1 on DS18B20-1#temperature do event t1=%value% @@ -445,15 +461,12 @@ rule1 on event#t2<%var2% do power1 0; endon + rule1 1 ``` -To test the rule without having the sensors in place, simply enter the events for `t1` and `t2` in the console: - -``` -event t1=21 -event t2=30 -``` +To test the rule without having the sensors in place, simply enter the events for `t1` and `t2` in the console: +`Backlog event t1=21;event t2=30` And watch the relay turn on and off based on the values. @@ -471,18 +484,20 @@ When the switch is on, the light will turn on but only when you have less than 1 ``` Rule1 -on switch1#state=1 do var1 100 endon -on switch1#state=0 do backlog var1 0; power1 off endon -on APDS9960#Ambient<%var1% do power1 on endon + on switch1#state=1 do var1 100 endon + on switch1#state=0 do backlog var1 0; power1 off endon + on APDS9960#Ambient<%var1% do power1 on endon ``` _All together to work as a rule:_ ``` Rule 1 -Rule on switch1#state=1 do var1 100 endon -on switch1#state=0 do backlog var1 0; power1 off endon -on APDS9960#Ambient<%var1% do power1 on endon + +Rule1 + on switch1#state=1 do var1 100 endon + on switch1#state=0 do backlog var1 0; power1 off endon + on APDS9960#Ambient<%var1% do power1 on endon ``` [Back To Top](#top) @@ -491,7 +506,11 @@ on APDS9960#Ambient<%var1% do power1 on endon #### Time-delayed Auto-off Switch **Rule:** -`Rule1 on button1#state do backlog power1 %value%; RuleTimer1 600 endon on Rules#Timer=1 do power1 off endon` +``` +Rule1 + on button1#state do backlog power1 %value%; RuleTimer1 600 endon + on Rules#Timer=1 do power1 off endon +``` **Result:** `on button1#state do backlog power1 %value%;` @@ -540,8 +559,7 @@ I connect an LED Strip WS2812 on D1 and the PIR on D2 and a LDR on A0 (voltage d PIR example: HR-SC501 -**The Settings are:** - +**The Settings are:** 18 Generic D1 WS2812 D2 Switch1 @@ -552,10 +570,19 @@ and type the following statements in the Console: **Rules:** ``` SwitchMode1 1 -Rule1 on analog#a0<400 do backlog Rule3 0; Rule2 1 endon on analog#a0>500 do backlog Rule2 0; Rule3 1 endon -Rule2 on switch1#state do backlog power1 1; RuleTimer1 30 endon on Rules#Timer=1 do power1 off endon -Rule3 on switch1#state do power1 off endon + +Rule1 + on analog#a0<400 do backlog Rule3 0; Rule2 1 endon + on analog#a0>500 do backlog Rule2 0; Rule3 1 endon + +Rule2 + on switch1#state do backlog power1 1; RuleTimer1 30 endon + on Rules#Timer=1 do power1 off endon + +Rule3 + on switch1#state do power1 off endon ``` + Activate Rule1 with one shot detection `Backlog Rule1 1; Rule1 6` @@ -583,7 +610,9 @@ Assuming that your switch is on `GPIO00` and configured as `Switch1`: ``` SwitchMode1 1 -Rule1 on Switch1#state=1 do Timers 0 endon on Switch1#state=0 do Timers 1 endon +Rule1 + on Switch1#state=1 do Timers 0 endon + on Switch1#state=0 do Timers 1 endon Rule1 1 ``` @@ -608,7 +637,9 @@ Type in the console: ``` Backlog ButtonTopic 0; SetOption1 1; SetOption32 20 -Rule1 on button1#state=3 do power1 2 endon on button1#state=2 do delay endon +Rule1 + on button1#state=3 do power1 2 endon + on button1#state=2 do delay endon Rule1 1 ``` @@ -631,7 +662,9 @@ In the case you do not want the double press feature you can configure your butt ``` Backlog SwitchTopic1 0; SwitchMode1 5; SetOption32 20 -Rule1 on switch1#state=3 do power1 2 endon on switch1#state=2 do delay endon +Rule1 + on switch1#state=3 do power1 2 endon + on switch1#state=2 do delay endon Rule1 1 ``` @@ -650,9 +683,9 @@ Set the following rules: ``` Rule1 -on Time#Initialized do backlog event checksunrise=%time%; event checksunset=%time% endon -on event#checksunset>%sunset% do power1 1 endon -on event#checksunrise<%sunrise% do power1 1 endon + on Time#Initialized do backlog event checksunrise=%time%; event checksunset=%time% endon + on event#checksunset>%sunset% do power1 1 endon + on event#checksunrise<%sunrise% do power1 1 endon ``` The previous rules are conditionals that represent the following logic: @@ -669,19 +702,19 @@ 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 Time#Minute=%mem2% do event chkSun endon -on Time#Minute=%sunrise% do event chkSun endon -on Time#Minute=%mem1% do event chkSun endon + on Time#Initialized do event chkSun endon + on Time#Minute=%sunset% do event chkSun endon + on Time#Minute=%mem2% do event chkSun endon + on Time#Minute=%sunrise% do event chkSun endon + on Time#Minute=%mem1% do event chkSun endon Rule2 -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 + 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 ``` ``` @@ -748,9 +781,9 @@ Commands: SwitchMode1 1 Rule1 -on Switch1#state=1 do backlog event checksunrise=%time%; event checksunset=%time% endon -on event#checksunrise<%sunrise% do power1 1 endon -on event#checksunset>%sunset% do power1 1 endon + on Switch1#state=1 do backlog event checksunrise=%time%; event checksunset=%time% endon + on event#checksunrise<%sunrise% do power1 1 endon + on event#checksunset>%sunset% do power1 1 endon Rule1 1 ``` @@ -790,14 +823,20 @@ Timer3 {"Arm":1,"Mode":1,"Time":"00:15","Window":0,"Days":"1111111","Repeat":1," **Rule 1:** Main Rule to check Luminance If Luminance is less than 150lx, power ON. If it goes beyond 175lx, power OFF. ``` -Rule1 on tele-TSL2561#Illuminance<150 do power1 1 endon on tele-TSL2561#Illuminance>175 do power1 0 endon +Rule1 + on tele-TSL2561#Illuminance<150 do power1 1 endon + on tele-TSL2561#Illuminance>175 do power1 0 endon + Rule1 1 ``` **Rule 2:** Trigger Rule1 only in the Mornings This ensures that Rule1 is triggered when Timer3 starts (in the morning) and stops when Timer1 starts (in the evenings). ``` -Rule2 on Clock#Timer=3 do Rule1 1 endon on Clock#Timer=4 do Rule1 0 endon +Rule2 + on Clock#Timer=3 do Rule1 1 endon + on Clock#Timer=4 do Rule1 0 endon + Rule2 1 ``` @@ -822,9 +861,11 @@ There is also an [option](Control-other-devices) to swap the actions of the **si ``` Backlog ButtonTopic 0; SetOption1 1; SetOption11 1; SetOption32 20 - Rule on button1#state=3 do publish cmnd/topicHOLD/power 2 endon on button1#state=2 do publish cmnd/topicDOUBLEPRESS/power 2 endon + Rule1 + on button1#state=3 do publish cmnd/topicHOLD/power 2 endon + on button1#state=2 do publish cmnd/topicDOUBLEPRESS/power 2 endon - Rule 1 + Rule1 1 ``` - Another example: @@ -837,9 +878,11 @@ There is also an [option](Control-other-devices) to swap the actions of the **si ``` Backlog ButtonTopic 0; SetOption1 1; SetOption11 0; SetOption32 20 - Rule on button1#state=3 do publish cmnd/topicHOLD/power 2 endon on button1#state=2 do publish cmnd/topicSINGLEPRESS/power 2 endon + Rule1 + on button1#state=3 do publish cmnd/topicHOLD/power 2 endon + on button1#state=2 do publish cmnd/topicSINGLEPRESS/power 2 endon - Rule 1 + Rule1 1 ``` _**Note:**_ `SetOption11 0` @@ -857,9 +900,11 @@ There is also an [option](Control-other-devices) to swap the actions of the **si ``` Backlog SwitchTopic1 0; SwitchMode1 5; SetOption32 20 - Rule on Switch1#State=3 do Power1 2 endon on Switch1#State=2 do Delay endon + Rule1 + on Switch1#State=3 do Power1 2 endon + on Switch1#State=2 do Delay endon - Rule 1 + Rule1 1 ``` [Back To Top](#top) @@ -872,10 +917,10 @@ There is also an [option](Control-other-devices) to swap the actions of the **si SwitchMode 5 Rule1 -on switch1#state==2 do add1 1 endon -on switch1#state==2 do power1 2 endon -on var1#state!=0 do backlog delay 6;var1 0 endon -on var1#state==2 do publish cmnd/othertasmota/POWER toggle endon + on switch1#state==2 do add1 1 endon + on switch1#state==2 do power1 2 endon + on var1#state!=0 do backlog delay 6;var1 0 endon + on var1#state==2 do publish cmnd/othertasmota/POWER toggle endon Rule1 on ``` @@ -912,11 +957,11 @@ Backlog SwitchTopic 0; SwitchMode4 2; SetOption0 0; PowerOnState 0 var1 1 Rule1 -on event#doorbell do var1 %value% endon -on switch4#state=1 do publish domoticz/in {"idx":11,"nvalue":1} endon -on switch4#state=1 do power1 %var1% endon -on switch4#state=0 do publish domoticz/in {"idx":11,"nvalue":0} endon -on switch4#state=0 do power1 0 endon + on event#doorbell do var1 %value% endon + on switch4#state=1 do publish domoticz/in {"idx":11,"nvalue":1} endon + on switch4#state=1 do power1 %var1% endon + on switch4#state=0 do publish domoticz/in {"idx":11,"nvalue":0} endon + on switch4#state=0 do power1 0 endon Rule1 1 ``` @@ -974,11 +1019,11 @@ When measuring distance and you have the need to see it in percentage of distanc ``` Rule1 -on tele-SR04#distance do backlog var1 %value%; event checklimit=%value%; event senddistance endon -on event#checklimit>128 do var1 128 endon -on event#checklimit<69 do var1 68 endon -on event#senddistance do backlog SCALE1 %var1%, 128, 69, 0, 100; event pubdata endon -on event#pubdata do publish tele/pannrum-temp/SENSOR %var1% endon + on tele-SR04#distance do backlog var1 %value%; event checklimit=%value%; event senddistance endon + on event#checklimit>128 do var1 128 endon + on event#checklimit<69 do var1 68 endon + on event#senddistance do backlog SCALE1 %var1%, 128, 69, 0, 100; event pubdata endon + on event#pubdata do publish tele/pannrum-temp/SENSOR %var1% endon Rule1 1 ``` @@ -1044,13 +1089,14 @@ MQT: cmnd/doorbell/POWER2 = OFF (retained) To solve it we can use rules. ``` SwitchTopic 0 + Rule1 -on System#Boot var1 0 endon -on Switch2#State do backlog add1 1; event START endon -on event#START do event BELL=%var1% endon -on event#BELL=1.000 do backlog publish cmnd/bell/power on; RuleTimer1 60 endon -on event#BELL=0 do publish cmnd/bell/power off endon -on Rules#Timer=1 do backlog var1 0; event BELL=0 endon + on System#Boot var1 0 endon + on Switch2#State do backlog add1 1; event START endon + on event#START do event BELL=%var1% endon + on event#BELL=1.000 do backlog publish cmnd/bell/power on; RuleTimer1 60 endon + on event#BELL=0 do publish cmnd/bell/power off endon + on Rules#Timer=1 do backlog var1 0; event BELL=0 endon Rule1 1 ``` @@ -1079,7 +1125,7 @@ Hardware - WS2812 LEDring with 24 LEDs powered by the Wemos D1 mini 5V thru the INA219 sensor Software -- Tasmota with USE_RULES enabled +- Tasmota compiled with `#define USE_RULES` Rule - `on INA219#Current>0.100 do Backlog Dimmer 10;Color 10,0,0 endon` @@ -1097,10 +1143,10 @@ By having a device (an [Oil Diffusser](https://blakadder.github.io/templates/oil ``` Rule2 -on power1#state=1 do serialsend5 55AA00060005020400010213 endon -on power1#state=0 do serialsend5 55AA00060005020400010011 endon -on power2#state=1 do serialsend5 55AA00060005060400010217 endon -on power2#state=0 do serialsend5 55AA00060005060400010015 endon + on power1#state=1 do serialsend5 55AA00060005020400010213 endon + on power1#state=0 do serialsend5 55AA00060005020400010011 endon + on power2#state=1 do serialsend5 55AA00060005060400010217 endon + on power2#state=0 do serialsend5 55AA00060005060400010015 endon ``` Power1 controls the device, Power2 turn on and off the light on the device. @@ -1108,9 +1154,9 @@ Another rule was created to issued commands on boot so the serial interface work ``` Rule3 -on system#boot do backlog baudrate 9600; seriallog 2; serialsend5 55aa000300010306 endon -on event#high do backlog serialsend5 55AA00060005650400010175; publish2 stat/diffuser/FAN high endon -on event#low do backlog serialsend5 55AA00060005650400010074; publish2 stat/diffuser/FAN low endon + on system#boot do backlog baudrate 9600; seriallog 2; serialsend5 55aa000300010306 endon + on event#high do backlog serialsend5 55AA00060005650400010175; publish2 stat/diffuser/FAN high endon + on event#low do backlog serialsend5 55AA00060005650400010074; publish2 stat/diffuser/FAN low endon ``` [Back To Top](#top) @@ -1159,8 +1205,8 @@ Send only when the sensor value changes by a certain amount. ``` Rule1 -on SI7021#temperature>%var1% do backlog var1 %value%; publish stat/mqttTopic/temp %value%; var2 %value%; add1 2; sub2 2 endon -on SI7021#temperature<%var2% do backlog var2 %value%; publish stat/mqttTopic/temp %value%; var1 %value%; add1 2; sub2 2 endon + on SI7021#temperature>%var1% do backlog var1 %value%; publish stat/mqttTopic/temp %value%; var2 %value%; add1 2; sub2 2 endon + on SI7021#temperature<%var2% do backlog var2 %value%; publish stat/mqttTopic/temp %value%; var1 %value%; add1 2; sub2 2 endon ``` #### Adjust the value of a sensor and send it by MQTT @@ -1168,8 +1214,8 @@ This example adds 2 degrees to the measured temperature and then sends that valu ``` Rule1 -on tele-SI7021#temperature do backlog var1 %value%; add1 2; event sendtemp endon -on event#sendtemp do publish stat/mqttTopic/temp %var1% endon + on tele-SI7021#temperature do backlog var1 %value%; add1 2; event sendtemp endon + on event#sendtemp do publish stat/mqttTopic/temp %var1% endon ``` [Back To Top](#top) @@ -1182,7 +1228,9 @@ This example switches a connected relays over the software serial on and off.
85 do VAR1 more85 endon -on event#temp>83 do VAR1 more83 endon -on event#temp>81 do VAR1 more81 endon -on event#temp=81 do VAR1 equal81 endon -on event#temp<81 do VAR1 less81 endon + on event#temp>85 do VAR1 more85 endon + on event#temp>83 do VAR1 more83 endon + on event#temp>81 do VAR1 more81 endon + on event#temp=81 do VAR1 equal81 endon + on event#temp<81 do VAR1 less81 endon ``` This is the output in the console: @@ -1251,11 +1299,11 @@ MQT: stat/living/RESULT = {"Var1":"more81"} So, all the triggers where TEMP>100, are firing. With the ``BREAK`` statement the rule set can be changed to: ``` Rule -on event#temp>85 do VAR1 more85 break -on event#temp>83 do VAR1 more83 break -on event#temp>81 do VAR1 more81 endon -on event#temp=81 do VAR1 equal81 endon -on event#temp<81 do VAR1 less81 endon + on event#temp>85 do VAR1 more85 break + on event#temp>83 do VAR1 more83 break + on event#temp>81 do VAR1 more81 endon + on event#temp=81 do VAR1 equal81 endon + on event#temp<81 do VAR1 less81 endon ``` Which will result in the following output: @@ -1288,11 +1336,11 @@ This rule also uses the [one-shot feature of rules](#4-usage-of-one-shot-once) t ``` Backlog PowerDelta 0; Rule1 0; Rule1 5 -Rule1 -ON ENERGY#Power>=35 DO Backlog PowerDelta 10; Status 8 BREAK -ON ENERGY#Power>=15 DO Backlog PowerDelta 25; Status 8 BREAK -ON ENERGY#Power>5 DO Backlog PowerDelta 35; Status 8 BREAK -ON ENERGY#Power<=5 DO Backlog PowerDelta 100 ENDON +Rule1 + ON ENERGY#Power>=35 DO Backlog PowerDelta 10; Status 8 BREAK + ON ENERGY#Power>=15 DO Backlog PowerDelta 25; Status 8 BREAK + ON ENERGY#Power>5 DO Backlog PowerDelta 35; Status 8 BREAK + ON ENERGY#Power<=5 DO Backlog PowerDelta 100 ENDON Rule1 1 ``` @@ -1354,28 +1402,28 @@ rule1 on IRreceived#Data do publish cmnd/irsideboard/irsend {Protocol:NEC,Bits:3 ``` Rule1 - on Switch1#Boot=1 do backlog delay 99; event Opened endon - on Switch2#Boot=1 do backlog delay 99; event Closed endon - on EVENT#OPEN do power1 %var2% endon - on EVENT#CLOSE do power1 %var1% endon - on EVENT#STOP do backlog power1 %var3%; power1 %var4%; event PState=STOP endon - on Switch1#State=1 do event Opened endon - on Switch2#State=1 do event Closed endon - on Switch1#State=0 do event Closing endon - on Switch2#State=0 do event Opening endon + on Switch1#Boot=1 do backlog delay 99; event Opened endon + on Switch2#Boot=1 do backlog delay 99; event Closed endon + on EVENT#OPEN do power1 %var2% endon + on EVENT#CLOSE do power1 %var1% endon + on EVENT#STOP do backlog power1 %var3%; power1 %var4%; event PState=STOP endon + on Switch1#State=1 do event Opened endon + on Switch2#State=1 do event Closed endon + on Switch1#State=0 do event Closing endon + on Switch2#State=0 do event Opening endon Rule2 - on event#Opened do backlog var 1; var2 0; var3 0; var4 0; ruletimer1 0; event PState=OPEN endon - on event#Closed do backlog var1 0; var2 1; var3 0; var4 0; ruletimer1 0; event PState=CLOSE endon - on event#Opening do backlog var1 0; var2 0; var3 1; var4 0; ruletimer1 15; event PState=OPENING endon - on event#Closing do backlog var1 0; var2 0; var3 0; var4 1; ruletimer1 15; event PState=CLOSING endon + on event#Opened do backlog var 1; var2 0; var3 0; var4 0; ruletimer1 0; event PState=OPEN endon + on event#Closed do backlog var1 0; var2 1; var3 0; var4 0; ruletimer1 0; event PState=CLOSE endon + on event#Opening do backlog var1 0; var2 0; var3 1; var4 0; ruletimer1 15; event PState=OPENING endon + on event#Closing do backlog var1 0; var2 0; var3 0; var4 1; ruletimer1 15; event PState=CLOSING endon Rule3 - on counter#c1>1000 do event PObstr=0 endon - on counter#c1<1000 do event PObstr=1 endon - on event#PObstr do publish stat/GarageDoor/OBSTR %value% endon - on event#PState do publish stat/GarageDoor/STATE %value% endon - on rules#timer=1 do event PState=STOP endon + on counter#c1>1000 do event PObstr=0 endon + on counter#c1<1000 do event PObstr=1 endon + on event#PObstr do publish stat/GarageDoor/OBSTR %value% endon + on event#PState do publish stat/GarageDoor/STATE %value% endon + on rules#timer=1 do event PState=STOP endon ``` //Turn on Rules @@ -1394,8 +1442,8 @@ For example, a remote control with one button to change speed. This rules simula // The `Delay` may not be necessary in your environment or may need to be adjusted according to your device characteristics. ``` Rule 1 -ON Event#tora DO Backlog Publish cmnd//IRSend {"Protocol":"NEC","Bits":32,"Data":"0x00FF30CF"}; Delay 10 ENDON -ON DO Backlog Event tora; Event tora; Event tora ENDON + ON Event#tora DO Backlog Publish cmnd//IRSend {"Protocol":"NEC","Bits":32,"Data":"0x00FF30CF"}; Delay 10 ENDON + ON DO Backlog Event tora; Event tora; Event tora ENDON ``` //Enable the Rule set @@ -1416,21 +1464,29 @@ Using the `WebSend` command, the two switches can talk to each other - no need f Starting with the slave, the rule to toggle the master is pretty simple: ``` -Rule1 ON Button1#State DO WebSend [192.168.0.74] POWER1 TOGGLE ENDON - ON Button2#State DO WebSend [192.168.0.74] POWER2 TOGGLE ENDON - ON Button3#State DO WebSend [192.168.0.74] POWER3 TOGGLE ENDON +Rule1 + ON Button1#State DO WebSend [192.168.0.74] POWER1 TOGGLE ENDON + ON Button2#State DO WebSend [192.168.0.74] POWER2 TOGGLE ENDON + ON Button3#State DO WebSend [192.168.0.74] POWER3 TOGGLE ENDON + Rule1 1 ``` Note that having a rule for the Button#State disables the power toggling of the slave's relay(s). This is desirable because we want the master to control the slave's relay state(s) according to its own as follows: ``` -Rule1 ON Power1#state=1 DO WebSend [192.168.0.144] POWER1 1 ENDON - ON Power1#state=0 DO WebSend [192.168.0.144] POWER1 0 ENDON -Rule2 ON Power2#state=1 DO WebSend [192.168.0.144] POWER2 1 ENDON - ON Power2#state=0 DO WebSend [192.168.0.144] POWER2 0 ENDON -Rule3 ON Power3#state=1 DO WebSend [192.168.0.144] POWER3 1 ENDON - ON Power3#state=0 DO WebSend [192.168.0.144] POWER3 0 ENDON +Rule1 + ON Power1#state=1 DO WebSend [192.168.0.144] POWER1 1 ENDON + ON Power1#state=0 DO WebSend [192.168.0.144] POWER1 0 ENDON + +Rule2 + ON Power2#state=1 DO WebSend [192.168.0.144] POWER2 1 ENDON + ON Power2#state=0 DO WebSend [192.168.0.144] POWER2 0 ENDON + +Rule3 + ON Power3#state=1 DO WebSend [192.168.0.144] POWER3 1 ENDON + ON Power3#state=0 DO WebSend [192.168.0.144] POWER3 0 ENDON + Backlog Rule1 1; Rule2 1; Rule3 1 ```