Rules - RTurala/Sonoff-Tasmota GitHub Wiki
Starting with version 5.12.0l Sonoff-Tasmota optionally provides Rules heavily inspired by the ESP Easy implementation.
(To use rules the define USE_RULES
has to be enabled in file user_config_override.h)
(Currently no if
directive or nested rules are supported)
Rules expand the available functionality in Sonoff-Tasmota with user flexibility. It allows for different actions on different button, switch or power responses. Rules are stored in flash and therefore will survive a reboot. Rules act on triggers initiated by events like a System Boot or a Timer being activated.
Using rules many issues can be resolved.
Rules can be added directly from the Tasmota Console or By MQTT or by HTTP as any other Tasmota Command.
To use rules the define USE_RULES has to be enabled in file user_config_override.h
When enabled five new powerful commands are available.
-
Rule
controls the storage of user defined rules up to 511 characters. It also allows for enabling or disabling rules. In some situations, like slow changing sensors (temperature), it allows for enabling or disabling one-shot rule activation. -
RuleTimer
provides up to eight timers to be used as countdown event. -
Event
allows a user to initiate a rule. -
Var
provides temporary storage for up to five variables -
Mem
provides persistent storage for up to five variables
See the commands description for the correct syntax and options.
A rule is using the following syntax:
on <trigger> do <command> endon
Rules can be concatenated and have to be on one line:
on <trigger1> do <command> endon on <trigger2> do <command> endon ...
Spaces after on, around do and before endon are mandatory. A rule is not case sensitive.
Before 6.1.0 20180706, there was one buffer for all rule commands. To store a complete set of commands in the flash memory buffer the rule
keyword can be used:
rule on <trigger1> do <command> endon on <trigger2> do <command> endon ...
Starting with 6.1.0 20180706, there are three buffers for rule commands. Each of those buffers can be switched on and off individually. Instead of rule
use rule[123]
:
rule1 on <trigger1> do <command> endon on <trigger2> do <command> endon ...
A trigger consists of one of either:
<SensorName>#<ValueName>
<SensorName>#<ValueName>=<value>
<SensorName>#<ValueName><<value>
<SensorName>#<ValueName>><value>
Tele-<SensorName>#<ValueName>
Available triggers are:
-
System#Boot
occurs once after mqtt is initialized. Due to command execution it cannot be made available earlier -
Mqtt#Connected
when MQTT is connected -
Mqtt#Disconnected
when MQTT is disconnected -
Wifi#Connected
when Wifi is connected (6.1.1c) -
Wifi#Disconnected
when Wifi is disconnected (6.1.1c) -
Time#Initialized
once when NTP is initialized and time is in sync -
Time#Initialized>120
once when NTP is initialized and time is in sync after 02:00 (6.1.0) -
Time#Set
every hour when NTP makes time in sync -
Time#Minute
every minute -
Time#Minute=241
every day once at 04:01 (= 241 minutes after midnight) (6.1.0) -
Time#Minute|5
every five minutes (6.1.1.6) -
Clock#Timer=3
when global Timer3 is activated -
Rules#Timer=1
when countdown RuleTimer1 expires -
Event#Anyname
when commandEvent Anyname
is executed -
Power1#State
when a power output is changed -
Power1#Boot
occurs after Tasmota starts. -
Button2#State
when a button input is changed -
Switch1#State
when a switch input is changed (note SwitchTopic=0 must be set for this to trigger) -
Switch1#Boot
occurs after Tasmota starts. -
Dimmer#State
when a the value for Dimmer is changed. -
Dimmer#Boot
occurs after Tasmota starts. -
Analog#A0div10
when the A0 input changes by more than 1% it provides a value between 0 and 100
All connected sensors can be a trigger in the form as they are represented in the teleperiod or Status 8 JSON message like:
-
Dsb18b20#Temperature<20
whenever the temperature of sensor DSB18B20 is below 20 degrees -
AM2301-12#Humidity=55.5
whenever the hunidity of sensor AM2301-12 equals 55.5 % -
INA219#Current>0.100
whenever the current drawn is more than 0.1A -
Energy#Power>100
whenever the power used is more than 100W
Devices using RfReceived (Sonoff Bridge) or IrReceived are also supported based on their JSON message like:
-
IrReceived#Data=801
whenever an IR signal for a RC5 remote control button 1 is received (6.1.0) -
RfReceived#RfKey=4
whenever the Sonoff Bridge receives a recognized Rf signal (6.1.0)
To trigger on a sensor at Teleperiod time only you may prefix the sensor with the word tele-
to distinguish it from the above shown trigger. See example 5 below.
<value>
can be any number, text, %var1% - %var5%, %mem1% - %mem5%, %time%, %uptime%, %sunrise% or %sunset% to test the rule on.
A command is any available command including command Backlog. A command parameter may be replaced with variable %value%
to be substituted by the trigger <value>
. See example below.
for one rule several commands can be combined with backlog and separated by ";"
: (see backlog
on <trigger1> do backlog <command1>;<comand2> ... endon
Special commands are the words var1
until var5
. They provide a means to store the trigger <value>
in a variable %var1%
until %var5%
to be used in any rule. All variables will be empty strings on program start. See example 5 below.
In addition to %varx% other special commands are the words mem1
until mem5
. They provide a means to store the trigger <value>
in a variable %mem1%
until %mem5%
to be used in any rule. All variables will be initialized with stored strings from flash on program start. See example 5 below.
Currently no if
directive or nested rules are supported.
The following examples will explain some use cases.
1. Prevent Wemos D1 mini load overcurrent
2. Execute any MQTT message when a button is pressed
3. Execute Several Commands when a Timer Expires
8. Arithmetic Commands to be used with VARs
8.a. Sending the value of a sensor to MQTT only when a delta is reached
8.b. Adjust the value of a sensor and send it by MQTT
10.a. Advanced PIR rules for Staircase
11. Energy Saving Smart Switch
12. Controlling Timers Enable from a switch
13. Toggle a Relay only when holding the button for 2 seconds
14. Rule to Make Sure Light is on at Night
15. Rule to enable a PIR Sensor only at night
16. Using an external button with single press - double press and hold
17. Rule to enable or disable doorbell relay with HTTP call
As a WS2812 24 led ring draws approximately 24x3x20 mA = 1.44A and the Wemos D1 mini powered from a PC's USB port can only provide up to 0.5A it would be nice to have some kind of mechanism in place to limit the amount of current to the WS2812 ledring to 0.1A. This is still enough to light all 24 leds up to color 202020.
Hardware
- Wemos D1 mini
- INA219 I2C sensor
- WS2812 ledring with 24 leds powered by the Wemos D1 mini 5V thru the INA219 sensor
Software
- Sonoff-Tasmota v5.12.0l or up with define USE_RULES enabled
Rule
on INA219#Current>0.100 do Backlog Dimmer 10;Color 10,0,0 endon
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 commandColor 10,0,0
.
When a button is pressed the user has the possibility to send a MQTT message based on FullTopic and ButtonTopic. This MQTT message is going to be received by the MQTT Broker and if there is any other device(s) subscripted to that Topic, it will receive also that message. So this approach can be used for sending messages/commands to MQTT Broker to Home Automation System, and/or sending messages/commands to MQTT Broker to other device(s).
A problem with this solution is that on a Sonoff 4CH all four buttons will be sending the same MQTT topic using only a different Power index number like cmnd/buttontopic/power3 toggle
.
By using a rule a single button can now send any MQTT message allowing much more flexibility.
Hardware
- Sonoff 4CH
Software
- Sonoff-Tasmota v5.12.0l or up with define USE_RULES enabled
- Disable ButtonTopic as it overrides rules for buttons:
ButtonTopic 0
Rule
rule 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".
Result
- When button 1 is pressed the rule kicks in and sends a MQTT message substituting variable
%value%
with the button state likecmnd/ring2/power 2
. When button 2 is pressed a MQTT message likecmnd/strip1/power 2
will be sent.
The default Timer1..16 functionality allows for controlling one output to either off, on, toggle or blink. When rules are enabled the blink option will be replaced by rule functionality allowing much more flexibility.
Hardware
- Sonoff 4CH
Software
- Sonoff-Tasmota v5.12.0l or up with define USE_RULES enabled
- Configure timer5 for rule execution when activated:
timer5 {"Arm":1,"Mode":0,"Time":"16:00","Days":"1111111","Repeat":1,"Action":3}
Rule
on clock#timer=5 do backlog power2 on;power1 off;power3 2 endon
Result
- When the timer expires the rule kicks in and set Power1 to OFF, Power2 to ON and Toggles Power3
If you want to have blink functionality define a rule like on clock#timer=5 do power 3 endon
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
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:
Rule 5
Now the MQTT message will be sent once, and only once, while the condition is met. This is perfect for thermostat on/off depending on temperature, bathroom extractor fan on/off depending on humidity, workshop dust collector on/off depending on whether some dust-producing machine is running.
It meets the 'hard thermostat' requests that have been common.
Using variables allows for storing sensor results to be used in composing a single HA message like used with Domoticz. To prevent flooding Domoticz with messages we only want to send a message at TelePeriod time. This is achieved by prefixing the SensorName with the label tele-
. This example will use a variable storing the temperature to be used together with humidity in one Domoticz MQTT message.
Hardware
- Sonoff TH or Wemos D1 mini
- AM2301 Temperature and Humidity sensor
Software
- Sonoff-Tasmota v5.12.0m or up with define USE_RULES enabled
- 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
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.
Clever Dickies now finally have a way to send Temperatures from multiple DS18B20 to Domoticz.
Connecting a potentiometer to the Analog A0 input and a rule can be used to control the dimmer state of any device.
Hardware
- Wemos D1 mini
- Potentiometer of 2k2 connected to Gnd, A0 and 3V3
- WS2812 led
Software
- Sonoff-Tasmota v5.12.0n or up with define USE_RULES enabled
Rule
on analog#a0div10 do dimmer %value% endon
Result
- Turning the potentiometer the voltage on the analog input will change resulting in a value change of 0 (Off) to 100 for the trigger. Using this value to control the dimmer of the WS2812 will control the brightness of the led(s)
Rule
on analog#a0div10 do publish cmnd/grouplight/dimmer %value% endon
Result
- This time all lights configured with GroupTopic
grouplight
will change their brightness according to the potentiometer position.
NOTE: You might want to execute command SaveData 2
to reduce flash writes ;-)
Demonstrate the use of variables. Make sure to execute commands rule 4
(Disable one-shot detection) first when trying the following example.
- Set a variable
rule needed:
on event#setvar1 do var1 %value% endon
command:
event setvar1=1
- View a variable
rule needed:
on event#getvar1 do var1 endon
command:
event getvar1
- Toggle a variable
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
command:
event togglevar1
- Show Messages:
rule needed:
on event#message do publish stat/[topic]/log %value% endon
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/sonoff/log %value% endon
NOTE
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.
- ADD
ADD1
to ADD5
: Add a value to VARx
Syntax: ADDx value
Usage: ADD1 15
Result: VAR1 = VAR1 + 15
- SUBSTRACT
SUB1
to SUB5
: Substract a value from VARx
Syntax: SUBx value
Usage: SUB1 15
Result: VAR1 = VAR1 - 15
- MULTIPLY
MULT1
to MULT5
: Multiply a value to VARx
Syntax: MULTx value
Usage: MULT1 15
Result: VAR1 = VAR1 * 15
- SCALE A VALUE
SCALE1
to SCALE5
: Scale a value from a low and high limit to another low and high limit and store it in VARx
(directly equivalent to MAP arduino command)
Syntax: SCALEx value, fromLow, fromHigh, toLow, toHigh
where,
value: the number to scale fromLow: the lower bound of the value’s current range fromHigh: the upper bound of the value’s current range toLow: the lower bound of the value’s target range toHigh: the upper bound of the value’s target range
(omitted values are taken as zero)
Usage: SCALE1 15, 0, 100, 0, 1000
Result: VAR1 = 150
When it is needed that a sensor value is only sent when it changes a delta, can be solved applying the following rule example:
rule
on SI7021#temperature>%var1% do backlog var1 %value%; publish stat/sonoff/temp %value%; var2 %value%; add1 2; sub2 2 endon
on SI7021#temperature<%var2% do backlog var2 %value%; publish stat/sonoff/temp %value%; var1 %value%; add1 2; sub2 2 endon
This example adds 2 degrees to the measured temperature and then sends that value to a MQTT topic.
rule
on tele-SI7021#temperature do backlog var1 %value%; add1 2; event sendtemp endon
on event#sendtemp do publish stat/sonoff/temp %var1% endon
As example, to be used on a Sonoff TH10 with Sensor Si7021
This example turn on and off an output based on the temperature value and the upper set point and the lower set point. It waits until is enabled by pressing the button or by mqtt message 1 to mem1. This value is remembered. So if power cycle occurs, will resume operation. The set point values can be changed on the fly by mqtt or console commands If the Temperature sensor disconnects, the outputs will shutdown until the sensor is back again and will resume operation. When the device is power up, the thermostat also waits until the sensor value to resume operation.
Inital Config:
- Available physical button as switch1
- Relay1 will be used the controller
- Rules must be used to control Relay so the pushbutton must only control switch1 and not directly control the relay - For this we use switchmode1 3 as described below and create the necessary rules because the pushbutton control of the relay is only disabled when the rules are in place.
Inital config on console:
-
switchmode1 3
<- Use the switch1 as pushbutton (It will allow us to disable the link between the button and the relay by inserting a rule to dictate what the pushbutton will do - NOTE: Until rules are created the pushbutton will still control the relay!) -
rule 1
<- turn on rules -
rule 4
<- turn off one-shot rule -
teleperiod 60
<- check temp every minute -
setoption26 1
<- use power1 on mqtt messages -
setoption0 0
<- dont save relay status on eeprom -
poweronstate 0
<- start all relays off -
mem1 0
<- thermostat status: 0-off 1-enabled - View or set by MQTT cmnd/sonoff/mem1 -
mem2 25
<- setpoint Temp upper limit - View or set by MQTT cmnd/sonoff/mem2 -
mem3 23
<- setpoint Temp lower limit - View or set by MQTT cmnd/sonoff/mem3 -
var1 0
<- thermostat actual status: 1-OK 0-NOT READY - View by MQTT cmnd/sonoff/var1
Rules:
On boot start a watch dog 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
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
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
Thermostat can be turned On by:
- pushing button
- by command on local console: mem1 1
- by command on any other console: publish cmnd/sonoff/mem1 1
- or MQTT at: cmnd/sonoff/mem1 1
Thermostat can be turned Off by:
- pushing button
- by command on local console: mem1 0
- by command on any other console: publish cmnd/sonoff/mem1 0
- or MQTT at: cmnd/sonoff/mem1 0
To get the status:
-
mem1
<- thermostat status: 0-off 1-enabled - View or set by MQTT cmnd/sonoff/mem1 -
mem2
<- setpoint Temp upper limit - View or set by MQTT cmnd/sonoff/mem2 -
mem3
<- setpoint Temp lower limit - View or set by MQTT cmnd/sonoff/mem3 -
var1
<- thermostat actual status: 1-OK 0-NOT READY - View by MQTT cmnd/sonoff/var1
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
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
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:
- 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)
- on console: event temp=22 (the thermostat turn on the heater)
- wait more than a minute without using the event temp and the thermostat will turn off as there is no temperature value (like a sensor error or disconnection)
- 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
Rule:
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%;
- On Button press the Light in the Staircase will switch on/off
ruletimer1 600 endon
- Additionaly ruletimer1 will begin to countdown 10 minutes
on rules#timer=1 do power1 off endon
- After the ruletimer1 expires the ligth will be switched off (if you forgot to switch it off)
Example works fine on a Wemos D1 Mini with Tasmota 6.2.1. Used as night light with motion sensor or as ambient light on floor or kitchen. I connect an LED Strip WS2812 on D1 and the PIR on D2 and a LDR on A0 (voltage divider with 10k ohm resistor)
PIR example: HR-SC501
The Setting are:
18 Generic
D1 WS2812
D2 Switch1
LDR on Wemos A0 (activated in user_config.h)
and i type the following rules in the konsole:
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.
Example of a switch controlling a light with a condition of a required amount of lux.
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.
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
Assuming that your switch is on GPIO00
and configured as Switch1
:
switchmode1 1
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
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.
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.
Type in the console:
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
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 wil 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. More information at: https://github.com/arendst/Sonoff-Tasmota/wiki/Control-other-devices
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 ]
Another example but using switch instead of button:
switchtopic1 0
switchmode1 5
setoption32 20
rule on switch1#state=3 do power1 2 endon on switch1#state=2 do delay endon
rule 1
Using Timers, you can be set to turn on and off a light for iluminate a street/patio by night. But if the Sonoff Device has no power at the trigger time, then, when it powers up, the light will be off all night. So, as a failsafe, can be implemented a conditional control to be checked at Sonoff Startup with rules.
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 Sonoff.
Set the following rules:
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:
IF %time%>%sunset DO power1 1 / IF %time%<%sunrise DO power1 1
PreInfo:
- PIR HC-SR501
- GPIO14 09 Switch1 (Sonoff Basic)
- Jumper outside ( like this )
- Lat and Lng set in config
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
rule1 1
You can have all 3 actions but only if defining your GPIO as button. In this case the double press will toggle the relay.
There is also an option to swap the actions of the single press and double press.
So,
BUTTON WITH 3 DIFFERENT ACTIONS
- As an example:
[ assuming gpio0 is configured as button1 ]
And that you want:
single press: Turn relay 1 double press: send a mqtt message hold 2 secs: send another mqtt message
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
rule 1
- Another example:
[ assuming gpio0 is configured as button1 ]
And that you want:
single press: send a mqtt message double press: Turn relay 1 hold 2 secs: send another mqtt message
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
rule 1
please note that setoption11 0
.
SWITCHS WITH 2 DIFFERENT ACTIONS
Switchs do not have double press feature
- Example:
[ assuming gpio0 is configured as switch1 and that you have a pushbutton connected ]
And that you want:
single press: do nothing hold 2 secs: toggle relay 1
switchtopic1 0
switchmode1 5
setoption32 20
rule on switch1#state=3 do power1 2 endon on switch1#state=2 do delay endon
rule 1
When you want to send MQTT messages ( we use domoticz in this example ) and choose when you want the relay (GPIO12) on or off, by simply sending HTTP commands to trigger an event.
In this example we're using rules.
Initial Config:
- PushButton Doorbell
- GPIO14 12 Switch4 (Sonoff Basic)
Connect the PushButton to GND and GPIO14 on your sonoff basic.
To be sure put a 4.7k resistor between VCC(3.3v) and GPIO14, this prevents ghost switching (capacitor is optional) See: YouTube
Dont forget to change the IDX value
Commands:
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
rule1 1
Usage:
Now you can call the event using HTTP to turn off the relay ( GPIO12 ).
http://<tasmotaIP>/cm?cmnd=event%20doorbell=0
and to turn on the relay ( GPIO12 ) use this one below.
http://<tasmotaIP>/cm?cmnd=event%20doorbell=1
If your tasmota device is password protected, which is most common then use the following HTTP commands instead.
Make sure you change tasmotaUsername and tasmotaPassword
Off:
http://<tasmotaIP>/cm?&user=<tasmotaUsername>&password=<tasmotaPassword>&cmnd=event%20doorbell=0
On:
http://<tasmotaIP>/cm?&user=<tasmotaUsername>&password=<tasmotaPassword>&cmnd=event%20doorbell=1
- 6.1.1.11:
- %sunset%, %sunrise% added