Updated Rule Cookbook (markdown)

Michael Ingraham 2019-09-23 09:42:02 -04:00
parent b4baa2c713
commit ff79ed4d8f
1 changed files with 55 additions and 22 deletions

@ -76,7 +76,7 @@ Software
- Disable ButtonTopic as it overrides rules for buttons: `ButtonTopic 0` - Disable ButtonTopic as it overrides rules for buttons: `ButtonTopic 0`
Rule Rule
- `Rule 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) (You will likely need to enable this rule if it's the first time you've used them)
"Rule1 on". "Rule1 on".
@ -96,11 +96,11 @@ Hardware
- Sonoff 4CH - Sonoff 4CH
Software Software
- Tasmota with USE_RULES enabled - Tasmota with `USE_RULES` enabled
- Configure timer5 for rule execution when activated: `timer5 {"Arm":1,"Mode":0,"Time":"16:00","Days":"1111111","Repeat":1,"Action":3}` - Configure timer5 for rule execution when activated:
`timer5 {"Arm":1,"Mode":0,"Time":"16:00","Days":"1111111","Repeat":1,"Action":3}`
Rule - Rule
- `on clock#Timer=5 do backlog power2 on;power1 off;power3 2 endon` `Rule1 on clock#Timer=5 do backlog power2 on;power1 off;power3 2 endon`
Result Result
- When the timer expires the rule kicks in and set Power1 to OFF, Power2 to ON and Toggles Power3 - When the timer expires the rule kicks in and set Power1 to OFF, Power2 to ON and Toggles Power3
@ -232,13 +232,19 @@ Demonstrate the use of variables. Make sure to execute commands `Rule 4`(Disable
* Everything together: * Everything together:
``` ```
Rule 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 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
``` ```
**NOTE** **NOTE**
Note that the following won't work: Note that the following won't work:
`Rule on event#setvar1 do backlog var1 %value%; power1 %var1% endon` `Rule1 on event#setvar1 do backlog var1 %value%; power1 %var1% endon`
Well at least not as you probably would expect. The `var1` value used by the `power1` command will be the value present before the `backlog` command is executed. This is so, because the rule will replace `%var1%` BEFORE the `backlog` commands are put in the `backlog` command stream. Well at least not as you probably would expect. The `var1` value used by the `power1` command will be the value present before the `backlog` command is executed. This is so, because the rule will replace `%var1%` BEFORE the `backlog` commands are put in the `backlog` command stream.
@ -250,11 +256,14 @@ Well at least not as you probably would expect. The `var1` value used by the `po
If a device has more than one relay and LEDs on different GPIOs (not connected to the relay) you need to use rules to display current relay status on LEDs. This example is a 3 gang wall switch. Instead of LEDs you need to assign 3 dummy relays that will be controlled when the real relays are switched to reflect their status. If a device has more than one relay and LEDs on different GPIOs (not connected to the relay) you need to use rules to display current relay status on LEDs. This example is a 3 gang wall switch. Instead of LEDs you need to assign 3 dummy relays that will be controlled when the real relays are switched to reflect their status.
``` ```
seriallog 0 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
rule1
on power1#state do power4 %value% endon
on power2#state do power5 %value% endon
on power3#state do power6 %value% endon
rule1 1 rule1 1
ledmask 0x0000
setoption13 1
``` ```
Note: This method doubles the number of flash writes. [Link to the device](https://blakadder.github.io/templates/DS-102_3.html) Note: This method doubles the number of flash writes. [Link to the device](https://blakadder.github.io/templates/DS-102_3.html)
@ -299,6 +308,7 @@ On boot start a watchdog timer to check temp sensor connection.
An available button is configured as switch to set thermostat ON or OFF An available button is configured as switch to set thermostat ON or OFF
``` ```
Rule1
on switch1#state do backlog event toggling1=%mem1% endon on switch1#state do backlog event toggling1=%mem1% endon
on event#toggling1=0 do mem 1 endon on event#toggling1=0 do mem 1 endon
on event#toggling1=1 do mem 0 endon on event#toggling1=1 do mem 0 endon
@ -343,13 +353,31 @@ backlog SwitchMode1 3; Rule 1; Rule 4; TelePeriod 60; SetOption26 1; SetOption0
RULES: 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 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
``` ```
EXAMPLE RULES WITHOUT TEMP SENSOR TO TEST THE THERMOSTAT RULES EXAMPLE RULES WITHOUT TEMP SENSOR TO TEST THE THERMOSTAT RULES
``` ```
Rule 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 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
``` ```
TESTS: TESTS:
@ -442,6 +470,7 @@ Example of a switch controlling a light with a condition of a required amount of
When the switch is on, the light will turn on but only when you have less than 100 lux in that room. While if the switch is off the light will be off. When the switch is on, the light will turn on but only when you have less than 100 lux in that room. While if the switch is off the light will be off.
``` ```
Rule1
on switch1#state=1 do var1 100 endon on switch1#state=1 do var1 100 endon
on switch1#state=0 do backlog var1 0; power1 off endon on switch1#state=0 do backlog var1 0; power1 off endon
on APDS9960#Ambient<%var1% do power1 on endon on APDS9960#Ambient<%var1% do power1 on endon
@ -451,7 +480,9 @@ _All together to work as a rule:_
``` ```
Rule 1 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 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
``` ```
[Back To Top](#top) [Back To Top](#top)
@ -551,8 +582,10 @@ Assuming that your switch is on `GPIO00` and configured as `Switch1`:
``` ```
SwitchMode1 1 SwitchMode1 1
Rule 1
Rule 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
``` ```
[`Switchmode1 1`](Commands#switchmode) will make Switch1#state be 1 when ON and 0 when OFF [`Switchmode1 1`](Commands#switchmode) will make Switch1#state be 1 when ON and 0 when OFF
@ -575,9 +608,9 @@ Type in the console:
``` ```
Backlog ButtonTopic 0; SetOption1 1; SetOption32 20 Backlog ButtonTopic 0; SetOption1 1; SetOption32 20
Rule 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
Rule 1 Rule1 1
``` ```
**Commands Explanation** **Commands Explanation**
@ -587,7 +620,7 @@ Rule 1
`SetOption32 20` : Set key hold time from 0.1 to 10 seconds (20 = 2 seconds) `SetOption32 20` : Set key hold time from 0.1 to 10 seconds (20 = 2 seconds)
`Rule on button1#state=3 do power1 2 endon` : When holding the button1 for 2 seconds it will toggle relay 1 (state = 3 means HOLD) `Rule on button1#state=3 do power1 2 endon` : When holding the button1 for 2 seconds it will toggle relay 1 (state = 3 means HOLD)
`on button1#state=2 do delay endon` : Do nothing when short pressing the button1 (state = 2 means TOGGLE) `on button1#state=2 do delay endon` : Do nothing when short pressing the button1 (state = 2 means TOGGLE)
`Rule 1` : To enable rules `Rule1 1` : To enable rules
NOTE: There is no state value for "double press" for Buttons. It is designed that double press will toggle the relay. See [Control Other Devices](control-other-devices) for more information. NOTE: There is no state value for "double press" for Buttons. It is designed that double press will toggle the relay. See [Control Other Devices](control-other-devices) for more information.
@ -598,9 +631,9 @@ In the case you do not want the double press feature you can configure your butt
``` ```
Backlog SwitchTopic1 0; SwitchMode1 5; SetOption32 20 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) [Back To Top](#top)