
ESPEasy rules let a board make its own decisions. Instead of sending every reading to a home automation server and waiting for instructions, the firmware can react locally: switching a relay when a temperature passes a threshold, turning on a light when a button is pressed or running a routine every few minutes. Because the logic lives on the device, it keeps working even when your network, broker or server is unavailable.
This article explains how the rules engine works, introduces its building blocks and walks through practical examples you can adapt for your own projects. No previous programming experience is assumed, although the syntax will feel familiar if you have written simple scripts before.
Enabling the Rules Engine
Rules are not active on a fresh installation. Open the Tools tab, go to the Advanced settings page and tick the option to enable rules. Save the settings and a Rules tab appears in the main menu.
The Rules tab provides several rule sets, each stored as a separate file on the board. Splitting rules across sets keeps related logic together and makes larger configurations easier to manage. Each set has a size limit, so very long routines are better divided into smaller, focused blocks.
Many builds include syntax highlighting and auto-completion in the rules editor, which helps you spot mistakes as you type. An online editor with similar highlighting is also available for drafting rules away from the board.
How Rules Think: Events and Actions
Everything in the rules engine is driven by events. An event is a message generated when something happens: the board finishes booting, a sensor delivers a new reading, a button changes state, a timer expires or a message arrives over MQTT. Rules listen for events and run actions in response.
Every rule follows the same basic shape:
On EventName do
action
endon
The engine checks each incoming event against the rules you have written. When a match is found, the actions between do and endon are carried out in order.
Common Event Types
Learning the most useful events gives you a vocabulary for almost any project.
- System#Boot fires once when the board starts, which makes it ideal for initial settings and starting timers.
- Sensor events use the pattern TaskName#ValueName, so a task named Climate with a value called Temperature produces Climate#Temperature whenever a new reading arrives.
- Rules#Timer events fire when a timer you have set reaches zero, identified by the timer number.
- Clock#Time events fire at particular times of day, once the board has obtained the time from a network time server.
- WiFi#Connected and WiFi#Disconnected report changes in the network connection.
- Custom events can be triggered by other rules, by commands sent over HTTP or MQTT and by other nodes.
The log on the Tools tab shows events as they occur, which is invaluable when you are unsure of an event's exact name.
Conditions With If, Else and Endif
Actions often depend on a condition. The rules engine supports if, else and endif blocks, along with comparison operators such as greater than, less than and equal to.
On Climate#Temperature do
if [Climate#Temperature]>28
GPIO,12,1
else
GPIO,12,0
endif
endon
The square brackets refer to the current value of a task. In this example, the rule runs whenever a new temperature reading arrives, switching GPIO 12 on above 28 degrees and off otherwise. A fan or a ventilation relay connected to that pin would then respond automatically.
Adding Hysteresis to Avoid Rapid Switching
The previous example has a practical weakness. If the temperature hovers around 28 degrees, the relay may switch on and off repeatedly, which wears out mechanical contacts and can annoy anyone nearby. A small gap between the on and off thresholds solves the problem.
On Climate#Temperature do
if [Climate#Temperature]>28
GPIO,12,1
endif
if [Climate#Temperature]<26
GPIO,12,0
endif
endon
Now the fan starts above 28 degrees and stops only when the temperature falls below 26. Between those values, the relay keeps its current state. This simple pattern, known as hysteresis, is one of the most useful techniques in home automation.
Working With Timers
Timers allow rules to act after a delay or at regular intervals. A timer is started with the timerSet command, which takes a timer number and a duration in seconds. When the time runs out, a Rules#Timer event fires for that number.
On System#Boot do
timerSet,1,300
endon
On Rules#Timer=1 do
Publish,%sysname%/status,alive
timerSet,1,300
endon
This pair of rules publishes a status message every five minutes. The second rule restarts the timer each time it fires, creating a repeating cycle. Setting a timer to zero cancels it.
Timers are also ideal for delayed actions, such as turning off a light a fixed time after motion was last detected.
On Motion#State=1 do
GPIO,13,1
timerSet,2,120
endon
On Rules#Timer=2 do
GPIO,13,0
endon
Each new detection restarts the two-minute timer, so the light stays on while movement continues and switches off two minutes after it stops.
Scheduling by Time of Day
Clock events allow actions at fixed times. The board needs a time source, normally a network time server configured on the Advanced settings page, together with the correct time zone and daylight saving settings.
On Clock#Time=All,07:00 do
GPIO,14,1
endon
On Clock#Time=All,22:30 do
GPIO,14,0
endon
The word All means the rule applies every day. Specific days can be used instead when a schedule should only run on certain days of the week.
Variables and Placeholders
Rules can store values in variables for later use. The Let command assigns a value to a numbered variable, and the variable can then be read elsewhere in your rules.
On Button#State=1 do
Let,1,[var#1]+1
Publish,%sysname%/presses,[var#1]
endon
This rule counts button presses and publishes the running total. Placeholders such as the system name are replaced automatically, which keeps rules portable between nodes. Event values can also be read inside a rule, which is useful when a custom event carries numbers with it.
Useful Commands Inside Rules
Rules can run almost any command the firmware understands. A few are used constantly.
- GPIO sets a pin high or low, which is how most relays and LEDs are controlled.
- Pulse and LongPulse switch a pin for a set duration, ideal for momentary actions such as opening a gate.
- Publish sends a message to an MQTT topic through the first enabled MQTT controller.
- SendToHTTP calls a web address, which is useful for simple integrations without MQTT.
- TaskValueSet writes a value into a task, often a dummy device used to store state.
- Event triggers a custom event, allowing one rule to call another.
Combining a Button and a Relay
One of the most common projects pairs a physical button with a relay, while still allowing remote control. Create a switch input task named Button and connect the relay to GPIO 12.
On Button#State=1 do
if [Relay#State]=0
GPIO,12,1
else
GPIO,12,0
endif
endon
Here a second task, named Relay, monitors the relay's output pin so that the rule always knows its current state. Each press toggles the relay, and because the rule runs locally, the button responds instantly even if the network is down.
Organising Rules Across Sets
As a configuration grows, structure matters as much as syntax. A sensible approach is to give each rule set a single purpose. The first set might hold start-up actions and timers, the second climate control, the third lighting and the fourth anything related to MQTT messages or custom events. When something misbehaves, you then know exactly where to look.
Comments help too. Any line beginning with two forward slashes is ignored by the engine, so a short note above each block explaining its purpose costs nothing and saves time months later, when the reasoning behind a threshold or a timer is no longer fresh in your mind.
Responding to Commands From Outside
Rules are not limited to events generated on the board. A custom event can be triggered by an HTTP request, an MQTT command or another rule, and it may carry values with it. That allows a home automation platform to ask the node to run a routine, such as a timed watering cycle, while the detailed timing still runs locally.
On Water do
GPIO,15,1
timerSet,3,%eventvalue1%
endon
On Rules#Timer=3 do
GPIO,15,0
endon
Sending the event Water with a value of 600 opens a valve on GPIO 15 for ten minutes, after which the board closes it on its own, even if the original sender has gone offline.
Testing and Debugging Rules
Write rules in small pieces and test each one before adding more. The log on the Tools tab shows events and the commands they trigger, so you can watch your logic unfold in real time. Raising the log level temporarily gives more detail when something does not behave as expected.
Common mistakes include misspelt task or value names, missing endon or endif statements and conditions that compare values in the wrong units. Reading the log line by line usually reveals the problem quickly.
Frequently Asked Questions
Do ESPEasy rules work without an internet connection?
Yes. Rules run entirely on the board. Only time-based rules need a time source, which is usually obtained over the network.
How do I enable rules?
Open Tools, then Advanced, tick the option to enable rules and save. The Rules tab then appears in the menu.
How do I refer to a sensor value in a rule?
Use the task and value names in square brackets, such as [Climate#Temperature].
Can rules switch relays?
Yes. The GPIO command sets a pin high or low, and Pulse or LongPulse can switch it for a fixed duration.
How do I run something every few minutes?
Start a timer at boot with timerSet, act on the matching Rules#Timer event and restart the timer inside that rule.
Why is my rule not firing?
Check the exact event name in the log, confirm rules are enabled and look for missing endon or endif statements.
Conclusion
ESPEasy rules turn a sensor node into a small, self-reliant controller. Events tell the board what has happened, conditions decide what should happen next and commands carry it out. Start with a single, well-tested rule, add hysteresis and timers as your confidence grows, and you will find that many everyday automations can live entirely on the device, working reliably whether or not the rest of your network is available.