Restore BREAK Example from Rules article

Michael Ingraham 2019-05-25 01:22:19 -04:00
parent c09999df39
commit f7232fcced
1 changed files with 92 additions and 5 deletions

@ -28,7 +28,8 @@
- [Sending the value of a sensor to MQTT only when a delta is reached](#sending-the-value-of-a-sensor-to-mqtt-only-when-a-delta-is-reached)
- [Adjust the value of a sensor and send it by MQTT](#adjust-the-value-of-a-sensor-and-send-it-by-mqtt)
26. [Timer-triggered Low frequency blink (intermittent timer)](#26-Timer-triggered-low-frequency-blink)
27. [Switch relais via serial interface](#27-Switch-relais-via-serial-interface)
27. [Switch relays via serial interface](#27-Switch-relays-via-serial-interface)
28. [Using BREAK to simulate IF..ELSEIF..ELSE..ENDIF](#28-Using-BREAK-to-simulate-IFELSEIFELSEENDIF)
------------------------------------------------------------------------------
#### 1. Use long press action on a switch
@ -760,7 +761,7 @@ on event#pubdata do publish tele/pannrum-temp/SENSOR %var1% endon
#### 20. Rules to distinguish Switch1 and Switch2 (without the use of Relay1 and Relay2)
When two (or more) switches are defined as input and you want to distinguish these in the RESULT topic without the use of Relais, then consider the following rules.
When two (or more) switches are defined as input and you want to distinguish these in the RESULT topic without the use of Relays, then consider the following rules.
```
@ -911,6 +912,10 @@ Rule
Result
- When a user raises brightness to a level using more than 0.1A the rule kicks in and lowers the current by executing command `Dimmer 10` and changes the color to Red with command `Color 10,0,0`.
[Back To Top](#top)
------------------------------------------------------------------------------
#### 24. Using dummy relays to send Serial codes to the MCU
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.
@ -925,6 +930,7 @@ 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
```
[Back To Top](#top)
------------------------------------------------------------------------------
@ -1015,10 +1021,13 @@ At the end of the desired period of operation, the clocktimer times out and runs
These rules were used to run a heater intermittently to keep a room warm in the absence of a thermostat.
------------------------------------------------------------------------------
#### 27. Switch relais via serial interface
[Back To Top](#top)
This example switches a connected relais over the software serial on and off.<br>
------------------------------------------------------------------------------
#### 27. Switch relays via serial interface
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`
@ -1036,4 +1045,82 @@ MQT: stat/sonoff-1585B3/RESULT = {"POWER":"OFF"}
MQT: stat/sonoff-1585B3/POWER = OFF
```
[Back To Top](#top)
------------------------------------------------------------------------------
#### 28. Using BREAK to simulate IF..ELSEIF..ELSE..ENDIF
``BREAK`` is an alternative to ``ENDON``. ``BREAK`` will stop the execution for the triggers that follow. If a trigger that ends with ``BREAK`` fires, then the following triggers of that rule will not be executed. This allows to simulate ``IF..ELSEIF..ELSE..ENDIF``
**Example:**
```
IF temp > 85 then
VAR1 more85
ELSEIF temp > 83 then
VAR1 more83
ELSEIF temp > 81 then
VAR1 more81
ELSEIF temp = 81 then
VAR1 equal81
ELSE
VAR1 less81
ENDIF
```
With the actual rules, if we use a set like the following:
```
Rule1
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:
```
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"}
```
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
```
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"}
```
[Back To Top](#top)