Tutorial Rules

From Let's Control It
Revision as of 18:05, 13 November 2016 by BertB (talk | contribs) (→‎SendToHTTP)
Jump to navigation Jump to search

Introduction

Along with ESP Easy R108, a new feature was enabled, named Rules. Rules can be used to create very simple flows to control devices on your ESP.

The syntax of a rule can be:

on <event>[=,<,>][value] do <action>

or

on <event>[=,<,>][value] do
 <action>
 <action>
endon

Also simple if ... else ... endif statements are possible, but nesting and Boolean logic are not supported. Task values can be obtained in the same way as with the LCD and OLED template with [Device Name#Value Name] Timers can be used to time things.


Some examples:

PIR and LDR

On PIR#Switch do
  if [LDR#Light]<500
    gpio,16,[PIR#Switch]
  endif
endon

In other words: If the PIR switch is set and if the light value < 500, then set GPIO port 16 of the ESP.

SR04 and LDR

on SR04#range<100 do
  if [ldr#lux]<500
    gpio,2,0
    gpio,16,1
  else
    gpio,2,1
    gpio,16,0
  endif
endon

Timer

On System#Boot do    //When the ESP boots, do
  servo,1,12,0
  timerSet,1,10      //Set Timer 1 for the next event in 10 seconds
endon
On Rules#Timer=1 do  //When Timer1 expires, do
  servo,1,12,30
  timerSet,2,1       //Set Timer 2 for the next event in 1 second
endon
On Rules#Timer=2 do  //When Timer2 expires, do
  servo,1,12,0
  timerSet,1,30      //Set Timer1 for the next event in 30 seconds
endon

HTTP call

When you enter this with the correct IP address in the URL of your browser:

http://<ESP-ip>/control?cmd=event,givemesomewater

And have this rule in the addressed ESP:

on givemesomewater do
  gpio,2,1  // open valve
  timerSet 1,600 // 10 minute timer
endon
on Rules#Timer=1 do
  gpio,2,0 // close valve
endon

Provided that you also have the valve etc, the plants will be happy.


SendTo and Publish

With SendTo you can add a Rule to your ESPEasy, capable of sending an event to another unit. This can be useful in cases where you want to take immediate action. There are two flavors: - SendTo to send remote unit control commands using the internal peer to peer UDP messaging - Publish to send remote commands to other ESP using MQTT broker

SendTo: SendTo <unit>,<command>


Imagine you have two ESPEasy modules, ESP#1 and ESP#2 In the Rules section of ESP#1 you have this:

on demoEvent do
  sendTo 2,event,givemesomewater (to use the previous example.
endon

And ESP#2 has the rules according to the previous example (givemesomewater)

If you then enter this with the correct IP address in the URL of your browser:

http://<ESP#1-ip >/control?cmd=event,demoEvent

Then ESP#1 shall send the event 'givemesomewater' to ESP#2.

It is also possible to directly order gpio changes, like:

on demoEvent do
  sendTo 2,GPIO,2,1
endon

Publish

Publish <topic>,<value>

To be created.

Time

With Rules you can also start or stop actions on a given day and time, or even on every day.

On Clock#Time=All,18:25 do // every day at 18:25 hours do ...
 gpio,14,0
endon

Or for a specific day:

On Clock#Time=Sun,18:25 do  // for Sunday, but All, Sun, Mon, Tue, Wed, Thu, Fri, Sat will do.
 gpio,14,0
endon

SendToUDP

SendToHTTP

To send a message to another device, like a command to switch on a light to Domoticz

On System#Boot do    //When the ESP boots, do
  timerSet,1,10      //Set Timer 1 for the next event in 10 seconds
endon

On Rules#Timer=1 do  //When Timer1 expires, do
  SendToHTTP 192.168.0.243,8080,/json.htm?type=command&paramm=switchlight&idx=174&switchcmd=On
endon

Enable Rules

To enable rules, go to Tools/Advanced and check the Rules checkbox.

Advanced.jpg

After clicking Submit, you will find a new page added. Here you can start experimenting with Rules:

Rules1.jpg

The example above shows an experiment with a LED, connected via a resistor of 1k to GPIO12. To be able to read the state of the LED (on or off) a switch input is created with the same GPIO port:

Switch.jpg

After rebooting the ESP, the LED will start blinking 10 seconds on en 10 seconds off.

Enjoy.