lua to highlight code block syntax

Michael Ingraham 2019-10-29 11:47:40 -04:00
parent e830cba5f5
commit 8daf2505e6
1 changed files with 60 additions and 60 deletions

@ -49,7 +49,7 @@
Activate long press action with `Switchmode 5` and shorten long press time to 2 seconds (`Setoption32 20`).
Long pressing on switch1 sends `POWER 2` (toggle action) command to the `tasmota02` device
```
```lua
Backlog SwitchMode 5; SetOption32 20
Rule on switch1#state=3 do publish cmnd/tasmota02/POWER 2 endon
Rule 1
@ -76,7 +76,7 @@ Software
- Disable ButtonTopic as it overrides rules for buttons: `ButtonTopic 0`
Rule
```
```lua
Rule1
on button1#state do publish cmnd/ring2/power %value% endon
on button2#state do publish cmnd/strip1/power %value% endon
@ -120,7 +120,7 @@ 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
```
```lua
on ENERGY#Current>0.100 do publish tool/tablesaw/power 1 endon
on ENERGY#Current<0.100 do publish tool/tablesaw/power 0 endon
```
@ -150,7 +150,7 @@ Software
- Home Automation tool Domoticz configured with a virtual sensor Temp+Hum using Idx 134
Rule
```
```lua
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
```
@ -217,7 +217,7 @@ Demonstrate the use of variables. Make sure to execute commands `Rule 4`(Disable
* Toggle a variable
rules needed:
```
```lua
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
@ -241,7 +241,7 @@ Demonstrate the use of variables. Make sure to execute commands `Rule 4`(Disable
mqtt: `cmnd/[topic]/event anyname=number`
* Everything together:
```
```lua
Rule1
on event#togglevar1 do event toggling1=%var1% endon
on event#toggling1<1 do event setvar1=1 endon
@ -265,7 +265,7 @@ Well at least not as you probably would expect. The `var1` value used by the `po
#### Control device LEDs with Relays
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.
```
```lua
Backlog ledmask 0x0000; setoption13 1; seriallog 0
rule1
@ -317,7 +317,7 @@ 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
```
```lua
Rule1
on switch1#state do backlog event toggling1=%mem1% endon
on event#toggling1=0 do mem1 1 endon
@ -331,7 +331,7 @@ 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
```
```lua
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
@ -360,13 +360,13 @@ Everything together:
INITIAL CONFIG: (Note: RuleTimer1 must be greater that TelePeriod for expected results)
```
```lua
backlog SwitchMode1 3; Rule 1; Rule 4; TelePeriod 60; SetOption26 1; SetOption0 0; poweronstate 0; mem1 0; mem2 25; mem3 23; var1 0
```
RULES:
```
```lua
Rule1
on system#boot do RuleTimer1 70 endon
on Switch1#State do event toggling1=%mem1% endon
@ -381,7 +381,7 @@ Rule1
EXAMPLE RULES WITHOUT TEMP SENSOR TO TEST THE THERMOSTAT RULES
```
```lua
Rule1
on system#boot do RuleTimer1 70 endon
on Switch1#State do event toggling1=%mem1% endon
@ -429,7 +429,7 @@ In a swimming pool, a filter pump and a solar panel is installed. When the sun i
`var3`: on threshold temp for panel
`mem3`: lowest valid panel temp
```
```lua
mem3 25
rule1
@ -482,7 +482,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.
```
```lua
Rule1
on switch1#state=1 do var1 100 endon
on switch1#state=0 do backlog var1 0; power1 off endon
@ -491,7 +491,7 @@ Rule1
_All together to work as a rule:_
```
```lua
Rule 1
Rule1
@ -506,7 +506,7 @@ Rule1
#### Time-delayed Auto-off Switch
**Rule:**
```
```lua
Rule1
on button1#state do backlog power1 %value%; RuleTimer1 600 endon
on Rules#Timer=1 do power1 off endon
@ -529,7 +529,7 @@ Rule1
#### Time-delay After Switch Off
**Rule:**
```
```lua
on switch1#state=1 do backlog
power1 on;
ruletimer1 0
@ -568,7 +568,7 @@ LDR on Wemos A0 (activated in user_config_override.h)
and type the following statements in the Console:
**Rules:**
```
```lua
SwitchMode1 1
Rule1
@ -607,7 +607,7 @@ Optional
Assuming that your switch is on `GPIO00` and configured as `Switch1`:
```
```lua
SwitchMode1 1
Rule1
@ -634,7 +634,7 @@ Behavior: Disable Button1 Short Press and Toggle Relay1 only when holding button
Type in the console:
```
```lua
Backlog ButtonTopic 0; SetOption1 1; SetOption32 20
Rule1
@ -659,7 +659,7 @@ In the case you do not want the double press feature you can configure your butt
**Another example but using switch instead of button:**
```
```lua
Backlog SwitchTopic1 0; SwitchMode1 5; SetOption32 20
Rule1
@ -681,7 +681,7 @@ 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.
Set the following rules:
```
```lua
Rule1
on Time#Initialized do backlog event checksunrise=%time%; event checksunset=%time% endon
on event#checksunset>%sunset% do power1 1 endon
@ -700,7 +700,7 @@ IF %time%>%sunset DO power1 1 / IF %time%<%sunrise DO power1 1
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.
```
```lua
Rule1
on Time#Initialized do event chkSun endon
on Time#Minute=%sunset% do event chkSun endon
@ -717,7 +717,7 @@ Rule2
on event#setPower do power1 %var1% endon
```
```
```lua
Backlog mem1 360; mem2 1350
Becklog Rule1 1; Rule2 1
```
@ -777,7 +777,7 @@ PreInfo:
- Lat and Lng set in config
Commands:
```
```lua
SwitchMode1 1
Rule1
@ -804,25 +804,25 @@ 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.
```
```lua
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.
```
```lua
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.
```
```lua
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.
```
```lua
Rule1
on tele-TSL2561#Illuminance<150 do power1 1 endon
on tele-TSL2561#Illuminance>175 do power1 0 endon
@ -832,7 +832,7 @@ 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).
```
```lua
Rule2
on Clock#Timer=3 do Rule1 1 endon
on Clock#Timer=4 do Rule1 0 endon
@ -858,7 +858,7 @@ There is also an [option](Control-other-devices) to swap the actions of the **si
**double press**: send a mqtt message
**hold 2 secs**: send another mqtt message
```
```lua
Backlog ButtonTopic 0; SetOption1 1; SetOption11 1; SetOption32 20
Rule1
@ -875,7 +875,7 @@ There is also an [option](Control-other-devices) to swap the actions of the **si
**double press**: Turn relay 1
**hold 2 secs**: send another mqtt message
```
```lua
Backlog ButtonTopic 0; SetOption1 1; SetOption11 0; SetOption32 20
Rule1
@ -897,7 +897,7 @@ There is also an [option](Control-other-devices) to swap the actions of the **si
**single press**: Do nothing
**hold 2 secs**: Toggle relay 1
```
```lua
Backlog SwitchTopic1 0; SwitchMode1 5; SetOption32 20
Rule1
@ -913,7 +913,7 @@ There is also an [option](Control-other-devices) to swap the actions of the **si
#### Perform any action on single/double press (for switches AND buttons)
```
```lua
SwitchMode 5
Rule1
@ -951,7 +951,7 @@ Connect the Switch to GND and the GPIO on your device. Be sure put a 4.7k resist
_Dont forget to change the IDX value_
**Commands:**
```
```lua
Backlog SwitchTopic 0; SwitchMode4 2; SetOption0 0; PowerOnState 0
var1 1
@ -999,7 +999,7 @@ The above is not proper, though, in case you have a redundant MQTT (e.g., two MQ
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:
```
```lua
Rule1 on Mqtt#Disconnected do MqttHost 0 endon
Rule1 1
```
@ -1017,7 +1017,7 @@ If the MqttHost field already contains an IP, you have to delete it using the we
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.
```
```lua
Rule1
on tele-SR04#distance do backlog var1 %value%; event checklimit=%value%; event senddistance endon
on event#checklimit>128 do var1 128 endon
@ -1056,7 +1056,7 @@ When two (or more) switches are defined as input and you want to distinguish the
Output:
```
```lua
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"}"
@ -1078,7 +1078,7 @@ MQT: stat/wemos-4/RESULT = {"POWER1":"0"}
#### 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 opto-isolator and resistor to take out state. But even with those speaker voltage is dropping so it switches the device multiple times.
```
```lua
MQT: cmnd/doorbell/POWER2 = OFF (retained)
MQT: cmnd/doorbell/POWER2 = ON (retained)
MQT: cmnd/doorbell/POWER2 = OFF (retained)
@ -1087,7 +1087,7 @@ MQT: cmnd/doorbell/POWER2 = OFF (retained)
```
To solve it we can use rules.
```
```lua
SwitchTopic 0
Rule1
@ -1141,7 +1141,7 @@ 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.
```
```lua
Rule2
on power1#state=1 do serialsend5 55AA00060005020400010213 endon
on power1#state=0 do serialsend5 55AA00060005020400010011 endon
@ -1152,7 +1152,7 @@ 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.
```
```lua
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
@ -1203,7 +1203,7 @@ Rule3
#### Transmit sensor value only when a delta is reached
Send only when the sensor value changes by a certain amount.
```
```lua
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
@ -1212,7 +1212,7 @@ Rule1
#### Adjust the value of a sensor and send it by MQTT
This example adds 2 degrees to the measured temperature and then sends that value to an MQTT topic.
```
```lua
Rule1
on tele-SI7021#temperature do backlog var1 %value%; add1 2; event sendtemp endon
on event#sendtemp do publish stat/mqttTopic/temp %var1% endon
@ -1227,7 +1227,7 @@ Rule1
This example switches a connected relays over the software serial on and off.<br>
Write the following rules:
```
```lua
rule1
on SSerialReceived#Data=on do power1 1 endon
on SSerialReceived#Data=off do power1 0 endon
@ -1235,7 +1235,7 @@ rule1
receiving `on` and `off` results in
```
```lua
MQT: tele/mqttTopic/RESULT = {"SSerialReceived":"on"}
RUL: SSERIALRECEIVED#DATA=ON performs "power1 1"
MQT: stat/mqttTopic/RESULT = {"POWER":"ON"}
@ -1255,7 +1255,7 @@ MQT: stat/mqttTopic/POWER = OFF
``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:**
```
```lua
IF temp > 85 then
VAR1 more85
ELSEIF temp > 83 then
@ -1270,7 +1270,7 @@ ENDIF
```
With the actual rules, if we use a set like the following:
```
```lua
Rule1
on event#temp>85 do VAR1 more85 endon
on event#temp>83 do VAR1 more83 endon
@ -1280,7 +1280,7 @@ Rule1
```
This is the output in the console:
```
```lua
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
@ -1297,7 +1297,7 @@ 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:
```
```lua
Rule
on event#temp>85 do VAR1 more85 break
on event#temp>83 do VAR1 more83 break
@ -1307,7 +1307,7 @@ Rule
```
Which will result in the following output:
```
```lua
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
@ -1333,7 +1333,7 @@ Power sensor reporting thresholds are set by a percentage change in the Power va
This rule also uses the [one-shot feature of rules](#4-usage-of-one-shot-once) to avoid reporting of every small change within a threshold window. The rule (a ON/DO/ENDON rule in this the set) will trigger only once when a threshold is crossed.
```
```lua
Backlog PowerDelta 0; Rule1 0; Rule1 5
Rule1
@ -1346,7 +1346,7 @@ Rule1 1
```
Which translates to:
```
```lua
Rule Pseudo Code
IF ENERGY#Power>=35 // ENERGY#Power GE 35
DO Backlog PowerDelta 10; Status 8
@ -1365,7 +1365,7 @@ ELSE // ENERGY#Power changed (i.e. LE 5)
#### IR Forward
Using one IR receiver and one sender (or both extender) you can simply forward signals from one to another using the following rule
```
```lua
rule1 on IRreceived#Data do publish cmnd/irsideboard/irsend {Protocol:NEC,Bits:32,Data:0x%value%} endon
```
@ -1400,7 +1400,7 @@ rule1 on IRreceived#Data do publish cmnd/irsideboard/irsend {Protocol:NEC,Bits:3
// var3=1 Only When OPENING
// var4=1 Only When CLOSING
```
```lua
Rule1
on Switch1#Boot=1 do backlog delay 99; event Opened endon
on Switch2#Boot=1 do backlog delay 99; event Closed endon
@ -1440,14 +1440,14 @@ For example, a remote control with one button to change speed. This rules simula
//The `<trigger>` can be a a condition or an event sent from another device or home automation hub.
//`<topic>` corresponds to the device transmitting the code (e.g., [YTF IR Bridge](YTF-IR-Bridge)). This could also be modified to send an RF code from a [Sonoff RF Bridge](Sonoff-RF-Bridge-433).
// The `Delay` may not be necessary in your environment or may need to be adjusted according to your device characteristics.
```
```lua
Rule 1
ON Event#tora DO Backlog Publish cmnd/<topic>/IRSend {"Protocol":"NEC","Bits":32,"Data":"0x00FF30CF"}; Delay 10 ENDON
ON <trigger> DO Backlog Event tora; Event tora; Event tora ENDON
```
//Enable the Rule set
```
```lua
Rule1 1
```
@ -1463,7 +1463,7 @@ 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:
```
```lua
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
@ -1474,7 +1474,7 @@ 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:
```
```lua
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