Updated Rule Cookbook (markdown)

Michael Ingraham 2019-09-23 09:29:05 -04:00
parent e7e5a154db
commit b4baa2c713
1 changed files with 191 additions and 215 deletions

@ -50,8 +50,7 @@ Activate long press action with `Switchmode 5` and shorten long press time to 2
Long pressing on switch1 sends `POWER 2` (toggle action) command to the `tasmota02` device
```
SwitchMode 5
SetOption32 20
Backlog SwitchMode 5; SetOption32 20
Rule on switch1#state=3 do publish cmnd/tasmota02/POWER 2 endon
Rule 1
```
@ -191,60 +190,57 @@ Demonstrate the use of variables. Make sure to execute commands `Rule 4`(Disable
* Set a variable
rule needed:
`on event#setvar1 do var1 %value% endon`
rule needed:
`on event#setvar1 do var1 %value% endon`
command:
`event setvar1=1`
command:
`event setvar1=1`
* View a variable
rule needed:
`on event#getvar1 do var1 endon`
rule needed:
`on event#getvar1 do var1 endon`
command:
`event getvar1`
command:
`event getvar1`
* Toggle a variable
rules needed:
rules needed:
```
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#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`
command:
`event togglevar1`
command:
`event togglevar1`
* Show Messages:
rule needed:
`on event#message do publish stat/[topic]/log %value% endon`
rule needed:
`on event#message do publish stat/[topic]/log %value% endon`
command:
`event message=INIT`
command:
`event message=INIT`
* All event commands can be executed from:
console: `event anyname=number`
mqtt: `cmnd/[topic]/event anyname=number`
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`
console: `event anyname=number`
mqtt: `cmnd/[topic]/event anyname=number`
* 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
```
**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`
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-log.
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.
[Back To Top](#top)
@ -298,40 +294,39 @@ Initial config on console:
Rules:
On boot start a watchdog timer to check temp sensor connection.
On boot start a watchdog timer to check temp sensor connection.
`Rule on system#boot do RuleTimer1 70 endon`
An available button is configured as switch to set thermostat ON or OFF
`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
Check temp sensor connection. If fails, set to off and turn off thermostat. Also continue checking
`on Rules#Timer=1 do backlog var1 0; RuleTimer1 70; power1 0 endon`
Resets checking timer if temperature is connected
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
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`
Thermostat can be turned On by:
Thermostat can be turned On by:
* pushing button
* by command on local console: mem1 1
* by command on any other console: publish cmnd/mqttTopic/mem1 1
* or MQTT at: cmnd/mqttTopic/mem1 1
Thermostat can be turned Off by:
Thermostat can be turned Off by:
* pushing button
* by command on local console: mem1 0
* by command on any other console: publish cmnd/mqttTopic/mem1 0
* or MQTT at: cmnd/mqttTopic/mem1 0
To get the status:
To get the status:
* `mem1` <- thermostat status: 0-off 1-enabled - View or set by MQTT cmnd/mqttTopic/mem1
* `mem2` <- setpoint Temp upper limit - View or set by MQTT cmnd/mqttTopic/mem2
* `mem3` <- setpoint Temp lower limit - View or set by MQTT cmnd/mqttTopic/mem3
@ -341,7 +336,9 @@ Everything together:
INITIAL CONFIG: (To note RuleTimer1 must be greater that TelePeriod for expected results)
`backlog SwitchMode1 3; Rule 1; Rule 4; TelePeriod 60; SetOption26 1; SetOption0 0; poweronstate 0; mem1 0; mem2 25; mem3 23; var1 0`
```
backlog SwitchMode1 3; Rule 1; Rule 4; TelePeriod 60; SetOption26 1; SetOption0 0; poweronstate 0; mem1 0; mem2 25; mem3 23; var1 0
```
RULES:
@ -355,8 +352,7 @@ 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
```
TESTS:
TESTS:
* Push the button1. The thermostat changes to ENABLED (mem1=1)
* on console: event temp=20 (now the system receives like a tele message from temperature sensor) and will turn on the relay1 (to heat)
* on console: event temp=26 (the thermostat turn off the heater)
@ -365,9 +361,9 @@ TESTS:
* will resume when using again the event temp
* console mem1 0, DISABLED, console mem1 1, ENABLED
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`
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`
[Back To Top](#top)
@ -381,19 +377,14 @@ In a swimming pool, a filter pump and a solar panel is installed. When the sun i
* Pump should start when solar panel is more than 2 deg warmer than the pool water
* Pump should stop when solar panel is less than 1 deg warmer than the pool water
* Pump should not start if the solar panel is below 25 deg celsius.
* Pump should not start if the solar panel is below 25 deg Celsius.
`t1`: pool temp
`t2`: panel temp
`var1`: in valid panel temp range?
`var2`: off threshold temp for panel
`var3`: on threshold temp for panel
`mem3`: lowest valid panel temp
`t1`: pool temp
`t2`: panel temp
`var1`: in valid panel temp range?
`var2`: off threshold temp for panel
`var3`: on threshold temp for panel
`mem3`: lowest valid panel temp
```
mem3 25
@ -468,23 +459,18 @@ Rule on switch1#state=1 do var1 100 endon on switch1#state=0 do backlog var1 0;
------------------------------------------------------------------------------
#### Time-delayed Auto-off Switch
**Rule:**
**Rule:**
`Rule1 on button1#state do backlog power1 %value%; RuleTimer1 600 endon on Rules#Timer=1 do power1 off endon`
**Result:**
**Result:**
`on button1#state do backlog power1 %value%;`
> On Button press the Light will toggle on/off
` on button1#state do backlog power1 %value%;`
`RuleTimer1 600 endon`
> Additionally RuleTimer1 will begin to countdown 10 minutes
* On Button press the Light will toggle on/off
` RuleTimer1 600 endon`
* Additionally RuleTimer1 will begin to countdown 10 minutes
` on Rules#Timer=1 do power1 off endon`
* After the RuleTimer1 expires the light will be turned off (if you forgot to turn it off)
`on Rules#Timer=1 do power1 off endon`
> After the RuleTimer1 expires the light will be turned off (if you forgot to turn it off)
[Back To Top](#top)
@ -523,54 +509,37 @@ 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 Setting are:**
**The Settings are:**
18 Generic
18 Generic
D1 WS2812
D2 Switch1
LDR on Wemos A0 (activated in user_config_override.h)
D1 WS2812
and type the following statements in the Console:
D2 Switch1
**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
```
Activate Rule1 with one shot detection
`Backlog Rule1 1; Rule1 6`
LDR on Wemos A0 (activated in user_config_override.h)
Optional
`Backlog Rule2 4; Rule3 4`
and I type the following rules 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 1` (activate Rule1)
`Rule1 6` (one shot detection)
optional
`Rule2 4`
`Rule3 4`
**Result:**
`on analog#a0>400`
disable Rule3 and activate Rule2
`on analog#a0>500`
disable Rule2 and activate Rule3
* Rule2 activates the LEDs for RuleTimer1 30 seconds on each trigger from PIR the RuleTimer start again.
`on Rules#Timer=1 do power1 off`
The LEDs turn off after the RuleTimer expires
* Rule3 is active on daylight and pipe the PIR signal in a power1 off signal. The LEDs stay off.
**Result:**
* `on analog#a0>400`
> disable Rule3 and activate Rule2
* `on analog#a0>500`
> disable Rule2 and activate Rule3
* Rule2 activates the LEDs for RuleTimer1 30 seconds on each trigger from PIR the RuleTimer start again.
`on Rules#Timer=1 do power1 off`
> The LEDs turn off after the RuleTimer expires
* Rule3 is active on daylight and pipe the PIR signal in a power1 off signal. The LEDs stay off.
[Back To Top](#top)
@ -586,9 +555,9 @@ Rule 1
Rule on Switch1#state=1 do Timers 0 endon on Switch1#state=0 do Timers 1 endon
```
Switchmode1 1 will make Switch1#state to be 1 when ON and 0 when OFF
[`Switchmode1 1`](Commands#switchmode) will make Switch1#state be 1 when ON and 0 when OFF
If you don't set Switchmode1 or it is equal 0, it will only have Switch1#state=2 (toggle) and the previous rule will not work.
If you don't set `Switchmode1` or it is equal 0, it will only have `Switch1#state=2` (toggle) and the previous rule will not work.
[Back To Top](#top)
@ -599,38 +568,38 @@ If you don't set Switchmode1 or it is equal 0, it will only have Switch1#state=2
The following example is to explain how to catch and use the HOLD feature for buttons.
Behaviour: Disable Button1 Short Press and Toggle Relay1 only when holding the button1 for 2 Seconds.
Behavior: Disable Button1 Short Press and Toggle Relay1 only when holding button1 for 2 Seconds.
Type in the console:
```
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
Rule 1
```
**Commands Explanation**
`ButtonTopic 0` : (default) To not use topics for buttons <br>
`SetOption1 1` : Allow only single, double and hold press button actions <br>
`SetOption32 20` : Set key hold time from 0.1 to 10 seconds (20 = 2 seconds) <br>
`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) <br>
`on button1#state=2 do delay endon` : Do nothing when short pressing the button1 (state = 2 means TOGGLE) <br>
`Rule 1` : To enable rules
`ButtonTopic 0` : (default) To not use topics for buttons
`SetOption1 1` : Allow only single, double and hold press button actions
`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)
`on button1#state=2 do delay endon` : Do nothing when short pressing the button1 (state = 2 means TOGGLE)
`Rule 1` : To enable rules
NOTE: There is not a state number 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.
In the case you do not want the double press feature you can configure your button as switch and also set the SwitchMode that fits you (like SwitchMode 5 to make it behave as a pushbutton) [SWITCH does not have double press ]
In the case you do not want the double press feature you can configure your button as switch and also set `SwitchMode` that fits your use case (such as `SwitchMode 5` to make the switch behave like a pushbutton) [SWITCH does not support double press]
**Another example but using switch instead of button:**
```
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
Rule 1
```
@ -640,7 +609,7 @@ Rule 1
#### Make Sure Light is on at Night
Using Timers, you can set a light to turn on and off to illuminate a street/patio by night. But if the device has no power at the trigger time, then, when it powers up, the light will be off all night. So, as a failsafe, implement a conditional control to be checked at Tasmota Startup.
Using Timers, you can set a light to turn on and off to illuminate a street/patio by night. But if the device has no power at the trigger time, then, when it powers up, the light will be off all night. So, as a fail-safe, implement a conditional control to be checked at Tasmota Startup.
Set Timers to turn on your light at Sunset and Turn off at sunrise.
Use `poweronstate 0` in order to start with lights off when powering up your device.
@ -672,6 +641,7 @@ 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
@ -766,38 +736,33 @@ ii) during evenings it ignores the sensor and turns on at sunset and turns off a
**Approach:**
Used a combination of Clock Timers and Rule to do this.
**Timer 1:** Power ON switch at Sunset
Powers on the switch at sunset with an offset of 20 minutes. Repeats every day.
**Timer 1:** Power ON switch at Sunset
Powers on the switch at sunset with an offset of 20 minutes. Repeats every day.
```
Timer1 {"Arm":1,"Mode":2,"Time":"-00:20","Window":0,"Days":"1111111","Repeat":1,"Output":1,"Action":1}
```
**Timer 2:** Power OFF switch at Night.
Turns power OFF at 23.00hrs. Repeats every day.
**Timer 2:** Power OFF switch at Night.
Turns power OFF at 23.00hrs. Repeats every day.
```
Timer2 {"Arm":1,"Mode":0,"Time":"23:00","Window":0,"Days":"1111111","Repeat":1,"Output":1,"Action":0}
```
**Timer 3:** Trigger Luminance Rule at Sunrise
Start watching the Lux sensor 15 minutes after sunrise.
**Timer 3:** Trigger Luminance Rule at Sunrise
Start watching the Lux sensor 15 minutes after sunrise.
```
Timer3 {"Arm":1,"Mode":1,"Time":"00:15","Window":0,"Days":"1111111","Repeat":1,"Output":1,"Action":3}
```
**Rule 1:** Main Rule to check Luminance
If Luminance is less than 150lx, power ON. If it goes beyond 175lx, power OFF.
**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 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).
**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 1
@ -942,7 +907,7 @@ On:
------------------------------------------------------------------------------
#### Force automatic reconnection to MQTT server via SD DNS
#### Force automatic re-connection to MQTT server via SD DNS
In order to search for the MQTT server using SD-DNS service (a.k.a. Bonjour or Zero Network Configuration) the suggested configuration is to leave the MQTT Host field blank.
@ -953,12 +918,12 @@ The standard behavior of Tasmota is
- in case the connection is successful, retain the IP address and use that in the subsequent connections
The above is not proper, though, in case you have a redundant MQTT (e.g., two MQTT server synchronized).
In such case, when the active MQTT fails for any reason, the expected behavior is to achieve automatic reconnection to the other MQTT server.
In such case, when the active MQTT fails for any reason, the expected behavior is to achieve automatic re-connection to the other MQTT server.
That can be easily configured defining the following rule on the device console:
```
Rule1 1
Rule1 on Mqtt#Disconnected do MqttHost 0 endon
Rule1 1
```
If the MqttHost field already contains an IP, you have to delete it using the web interface or the following MQTT command:
@ -972,17 +937,17 @@ If the MqttHost field already contains an IP, you have to delete it using the we
#### Change distance to percentage
When measuring distance and you have the need to see it in percentage of distance.
In the example 100% is everything below 69cm and 0% is everything above 128cm.
This is used for showing fill percentage of a wood pellets storage.
When measuring distance and you have the need to see it in percentage of distance. In the example 100% is everything below 69cm and 0% is everything above 128cm. This is used for showing fill percentage of a wood pellets storage.
```
Rule
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
Rule1 1
```
[Back To Top](#top)
@ -1014,18 +979,18 @@ When two (or more) switches are defined as input and you want to distinguish the
Output:
```
00:05:03 RUL: SWITCH1#STATE performs "publish stat/wemos-4/RESULT {"POWER1":"1"}"
00:05:03 MQT: stat/wemos-4/RESULT = {"POWER1":"1"}
00:05:08 RUL: SWITCH2#STATE performs "publish stat/wemos-4/RESULT {"POWER2":"1"}"
00:05:08 MQT: stat/wemos-4/RESULT = {"POWER2":"1"}
00:05:08 RUL: SWITCH1#STATE performs "publish stat/wemos-4/RESULT {"POWER1":"0"}"
00:05:08 MQT: stat/wemos-4/RESULT = {"POWER1":"0"}
00:05:11 RUL: SWITCH2#STATE performs "publish stat/wemos-4/RESULT {"POWER2":"0"}"
00:05:11 MQT: stat/wemos-4/RESULT = {"POWER2":"0"}
00:05:12 RUL: SWITCH1#STATE performs "publish stat/wemos-4/RESULT {"POWER1":"1"}"
00:05:12 MQT: stat/wemos-4/RESULT = {"POWER1":"1"}
00:05:16 RUL: SWITCH1#STATE performs "publish stat/wemos-4/RESULT {"POWER1":"0"}"
00:05:16 MQT: stat/wemos-4/RESULT = {"POWER1":"0"}
RUL: SWITCH1#STATE performs "publish stat/wemos-4/RESULT {"POWER1":"1"}"
MQT: stat/wemos-4/RESULT = {"POWER1":"1"}
RUL: SWITCH2#STATE performs "publish stat/wemos-4/RESULT {"POWER2":"1"}"
MQT: stat/wemos-4/RESULT = {"POWER2":"1"}
RUL: SWITCH1#STATE performs "publish stat/wemos-4/RESULT {"POWER1":"0"}"
MQT: stat/wemos-4/RESULT = {"POWER1":"0"}
RUL: SWITCH2#STATE performs "publish stat/wemos-4/RESULT {"POWER2":"0"}"
MQT: stat/wemos-4/RESULT = {"POWER2":"0"}
RUL: SWITCH1#STATE performs "publish stat/wemos-4/RESULT {"POWER1":"1"}"
MQT: stat/wemos-4/RESULT = {"POWER1":"1"}
RUL: SWITCH1#STATE performs "publish stat/wemos-4/RESULT {"POWER1":"0"}"
MQT: stat/wemos-4/RESULT = {"POWER1":"0"}
```
[Back To Top](#top)
@ -1034,36 +999,38 @@ Output:
#### Receiving state of anything that triggers SWITCH more than one time
With analog intercom doorbells you can take out info about ringing from speaker voltage. You can connect GPIO to it via optoisolator and resistor to take out state. But even with those speaker voltage is dropping so it switches the device multiple times.
With analog intercom doorbells you can take out info about ringing from speaker voltage. You can connect GPIO to it via opto-isolator and resistor to take out state. But even with those speaker voltage is dropping so it switches the device multiple times.
```
14:03:00 MQT: cmnd/doorbell/POWER2 = OFF (retained)
14:03:01 MQT: cmnd/doorbell/POWER2 = ON (retained)
14:03:02 MQT: cmnd/doorbell/POWER2 = OFF (retained)
14:03:03 MQT: cmnd/doorbell/POWER2 = ON (retained)
14:03:04 MQT: cmnd/doorbell/POWER2 = OFF (retained)
MQT: cmnd/doorbell/POWER2 = OFF (retained)
MQT: cmnd/doorbell/POWER2 = ON (retained)
MQT: cmnd/doorbell/POWER2 = OFF (retained)
MQT: cmnd/doorbell/POWER2 = ON (retained)
MQT: cmnd/doorbell/POWER2 = OFF (retained)
```
To solve it we can use rules.
```
SwitchTopic 0
Rule1 1
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
Rule1 1
```
description:
- turn off switchtopic as it is necessary to trigger Switch2#state
- enable rule 1
- on system boot set var1 to 0
- on switch2 click (person pushing doorbell) - var1 += 1; trigger event START
- on START - set event BELL equal to var1
- if event#BELL=1 (triggered first time) publish mqtt message ON and trigger RulesTimer1 for 60 seconds
- if event#BELL=0 publish mqtt message OFF
- on RulesTimer1 - reset var1 to 0, and call event#BELL.
- enable rule 1
In this case we have lock for 60 seconds for multiple people calls or to be resistant for speaker voltage drops.
@ -1096,14 +1063,21 @@ Result
By having a device (an [Oil Diffusser](https://blakadder.github.io/templates/oil_diffuser_550ml.html)) that controls all its features through an MCU and reports the states in serial codes to the ESP8266 I had to create some rules to control it using the Web UI or standard Power commands.
```
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
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
```
Power1 controls the device, Power2 turn on and off the light on the device.
Another rule was created to issued commands on boot so the serial interface works every time and to control the built in fan using Event triggers and have its state retained in an MQTT message for Home Assistant.
```
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
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
```
[Back To Top](#top)
@ -1147,8 +1121,8 @@ Rule3 on system#boot do backlog baudrate 9600; seriallog 2; serialsend5 55aa0003
Usage: `SCALE1 15, 0, 100, 0, 1000`
Result: `VAR1 = 150`
#### Transmit sensor value only when a delta is reached
Send only when the sensor value changes by a certain amount.
#### Transmit sensor value only when a delta is reached
Send only when the sensor value changes by a certain amount.
```
Rule1
@ -1174,7 +1148,9 @@ on event#sendtemp do publish stat/mqttTopic/temp %var1% endon
This example switches a connected relays over the software serial on and off.<br>
Write the following rules:
`rule1 on SSerialReceived#Data=on do power1 1 endon on SSerialReceived#Data=off do power1 0 endon`
```
rule1 on SSerialReceived#Data=on do power1 1 endon on SSerialReceived#Data=off do power1 0 endon
```
receiving `on` and `off` results in
@ -1224,20 +1200,20 @@ on event#temp<81 do VAR1 less81 endon
This is the output in the console:
```
19:43:18 CMD: rule
19:43:18 MQT: stat/living/RESULT = {"Rule1":"ON","Once":"ON","StopOnError":"OFF","Free":322,"Rules":"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"}
19:43:24 CMD: event temp=10
19:43:24 MQT: stat/living/RESULT = {"Event":"Done"}
19:43:24 RUL: EVENT#TEMP<81 performs "VAR1 less81"
19:43:24 MQT: stat/living/RESULT = {"Var1":"less81"}
19:43:36 CMD: event temp=100
19:43:36 MQT: stat/living/RESULT = {"Event":"Done"}
19:43:36 RUL: EVENT#TEMP>85 performs "VAR1 more85"
19:43:36 MQT: stat/living/RESULT = {"Var1":"more85"}
19:43:36 RUL: EVENT#TEMP>83 performs "VAR1 more83"
19:43:36 MQT: stat/living/RESULT = {"Var1":"more83"}
19:43:36 RUL: EVENT#TEMP>81 performs "VAR1 more81"
19:43:36 MQT: stat/living/RESULT = {"Var1":"more81"}
CMD: rule
MQT: stat/living/RESULT = {"Rule1":"ON","Once":"ON","StopOnError":"OFF","Free":322,"Rules":"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"}
CMD: event temp=10
MQT: stat/living/RESULT = {"Event":"Done"}
RUL: EVENT#TEMP<81 performs "VAR1 less81"
MQT: stat/living/RESULT = {"Var1":"less81"}
CMD: event temp=100
MQT: stat/living/RESULT = {"Event":"Done"}
RUL: EVENT#TEMP>85 performs "VAR1 more85"
MQT: stat/living/RESULT = {"Var1":"more85"}
RUL: EVENT#TEMP>83 performs "VAR1 more83"
MQT: stat/living/RESULT = {"Var1":"more83"}
RUL: EVENT#TEMP>81 performs "VAR1 more81"
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:
```
@ -1251,20 +1227,20 @@ on event#temp<81 do VAR1 less81 endon
Which will result in the following output:
```
18:00:17 CMD: rule
18:00:17 RSL: RESULT = {"Rule1":"ON","Once":"OFF","StopOnError":"OFF","Free":321,"Rules":"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"}
18:00:25 CMD: event temp=10
18:00:25 RSL: RESULT = {"Event":"Done"}
18:00:25 RUL: EVENT#TEMP<81 performs "VAR1 less81"
18:00:25 RSL: RESULT = {"Var1":"less81"}
18:00:36 CMD: event temp=100
18:00:36 RSL: RESULT = {"Event":"Done"}
18:00:36 RUL: EVENT#TEMP>85 performs "VAR1 more85"
18:00:36 RSL: RESULT = {"Var1":"more85"}
18:01:05 CMD: event temp=83
18:01:05 RSL: RESULT = {"Event":"Done"}
18:01:05 RUL: EVENT#TEMP>81 performs "VAR1 more81"
18:01:05 RSL: RESULT = {"Var1":"more81"}
CMD: rule
RSL: RESULT = {"Rule1":"ON","Once":"OFF","StopOnError":"OFF","Free":321,"Rules":"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"}
CMD: event temp=10
RSL: RESULT = {"Event":"Done"}
RUL: EVENT#TEMP<81 performs "VAR1 less81"
RSL: RESULT = {"Var1":"less81"}
CMD: event temp=100
RSL: RESULT = {"Event":"Done"}
RUL: EVENT#TEMP>85 performs "VAR1 more85"
RSL: RESULT = {"Var1":"more85"}
CMD: event temp=83
RSL: RESULT = {"Event":"Done"}
RUL: EVENT#TEMP>81 performs "VAR1 more81"
RSL: RESULT = {"Var1":"more81"}
```
[Back To Top](#top)
@ -1402,7 +1378,7 @@ Rule1 1
Two Sonoff T1 3-gang light switches can be used at either end of a room by setting up one the master and the other as the slave. The master performs the switching of the power to the lights, while the slave just asks the master to toggle the power state. The master also turns the slave's relays on and off so that the LED indicators follow the master's state.
Using the ```WebSend``` command, the two switches can talk to each other - no need for an MQTT broker. It remains to be seen how reliable this is.
Using the `WebSend` command, the two switches can talk to each other - no need for an MQTT broker. It remains to be seen how reliable this is.
Starting with the slave, the rule to toggle the master is pretty simple: