RULE help

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
mra2ko
Normal user
Posts: 80
Joined: 06 Dec 2021, 08:32
Location: Czech Republic

RULE help

#1 Post by mra2ko » 26 Feb 2023, 22:48

Hi,
I would like change on my LCD 2,4" two pages. One minutes rule 1 and one minue rule 2, pls how to set rules, timers and etc... ?
Rule will similar, but inputs different...
Anyone edit my rule... ?
Thank


On System#Boot do
tftcmd,clear,
timerSet,1,1
endon
On Rules#Timer=1 do
tftcmd,clear
tft,txtfull,5,5,4,white,black,%systm_hm%
tft,txtfull,80,55,2,white,black,out
tft,txtfull,10,80,6,red,black,"[mqtt2#Value8]"
tft,txtfull,166,85,2,white,black,°
tft,txtfull,180,85,4,white,black,C
tft,txtfull,17,185,2,white,black,well
tft,txtfull,10,210,3,white,black,"[mqtt3#Value1]cm"
timerSet,1,60

endon

User avatar
Ath
Normal user
Posts: 3416
Joined: 10 Jun 2018, 12:06
Location: NL

Re: RULE help

#2 Post by Ath » 26 Feb 2023, 22:59

mra2ko wrote: 26 Feb 2023, 22:48 I would like change on my LCD 2,4" two pages. One minutes rule 1 and one minue rule 2, pls how to set rules, timers and etc... ?
Rule will similar, but inputs different...
Something like this:

Code: Select all

On System#Boot do
  Let,1,0 // Select 1st page 0
  AsyncEvent,Rules#Timer=1 // Initial page display
  LoopTimerSet,1,60 // Schedule a page-switch every minute
Endon

On Rules#Timer=1 do
  If %v1%=0
    Let,1,1 // Next up: 2nd page
    tftcmd,clear
    tft,txtfull,5,5,4,white,black,%systm_hm%
    tft,txtfull,80,55,2,white,black,out
    tft,txtfull,10,80,6,red,black,"[mqtt2#Value8]" 
    tft,txtfull,166,85,2,white,black,°
    tft,txtfull,180,85,4,white,black,C
    tft,txtfull,17,185,2,white,black,well
    tft,txtfull,10,210,3,white,black,"[mqtt3#Value1]cm"
  Else
    Let,1,0 // Next up: 1st page
    tftcmd,clear
    tft,txtfull,5,5,4,white,black,%systm_hm%
    tft,txtfull,80,55,2,white,black,out
    // etc. data for the second page
  Endif
Endon
/Ton (PayPal.me)

mra2ko
Normal user
Posts: 80
Joined: 06 Dec 2021, 08:32
Location: Czech Republic

Re: RULE help

#3 Post by mra2ko » 27 Feb 2023, 11:02

Work well, thnak ! But maybe one rule will be on maximum of data, can I switch bettwen rule 1 and rule 2 sets?
Thank


Ath wrote: 26 Feb 2023, 22:59
mra2ko wrote: 26 Feb 2023, 22:48 I would like change on my LCD 2,4" two pages. One minutes rule 1 and one minue rule 2, pls how to set rules, timers and etc... ?
Rule will similar, but inputs different...
Something like this:

Code: Select all

On System#Boot do
  Let,1,0 // Select 1st page 0
  AsyncEvent,Rules#Timer=1 // Initial page display
  LoopTimerSet,1,60 // Schedule a page-switch every minute
Endon

On Rules#Timer=1 do
  If %v1%=0
    Let,1,1 // Next up: 2nd page
    tftcmd,clear
    tft,txtfull,5,5,4,white,black,%systm_hm%
    tft,txtfull,80,55,2,white,black,out
    tft,txtfull,10,80,6,red,black,"[mqtt2#Value8]" 
    tft,txtfull,166,85,2,white,black,°
    tft,txtfull,180,85,4,white,black,C
    tft,txtfull,17,185,2,white,black,well
    tft,txtfull,10,210,3,white,black,"[mqtt3#Value1]cm"
  Else
    Let,1,0 // Next up: 1st page
    tftcmd,clear
    tft,txtfull,5,5,4,white,black,%systm_hm%
    tft,txtfull,80,55,2,white,black,out
    // etc. data for the second page
  Endif
Endon

User avatar
Ath
Normal user
Posts: 3416
Joined: 10 Jun 2018, 12:06
Location: NL

Re: RULE help

#4 Post by Ath » 27 Feb 2023, 11:33

mra2ko wrote: 27 Feb 2023, 11:02 Work well, thnak ! But maybe one rule will be on maximum of data, can I switch bettwen rule 1 and rule 2 sets?
If you are talking about the warning when the rules file is > 2048 bytes, then don't worry, that limit is not really 'enforced', just a remainder of a technical issue that has been solved years ago.

But if you really want to split the rules, it could be like this:

Code: Select all

On Page1 do
  tftcmd,clear
  tft,txtfull,5,5,4,white,black,%systm_hm%
  tft,txtfull,80,55,2,white,black,out
  tft,txtfull,10,80,6,red,black,"[mqtt2#Value8]" 
  tft,txtfull,166,85,2,white,black,°
  tft,txtfull,180,85,4,white,black,C
  tft,txtfull,17,185,2,white,black,well
  tft,txtfull,10,210,3,white,black,"[mqtt3#Value1]cm"
Endon

On Page2 do
  tftcmd,clear
  tft,txtfull,5,5,4,white,black,%systm_hm%
  tft,txtfull,80,55,2,white,black,out
  // etc. data for the second page
Endon

On Rules#Timer=1 do
  If %v1%=0
    Let,1,1 // Next up: 2nd page
    asyncEvent,Page1
  Else
    Let,1,0 // Next up: 1st page
    asyncEvent,Page2
  Endif
Endon

On System#Boot do
  Let,1,0 // Select 1st page 0
  AsyncEvent,Rules#Timer=1 // Initial page display
  LoopTimerSet,1,60 // Schedule a page-switch every minute
Endon

NB:
- Rules that are executed most often should be placed first in the Rules1 file
- Because of Rules-caching this has become somewhat less important
- Rules that are only executed once, like on System#Boot do, can be moved to one of the other Rules files, to reduce the size of the Rules1 file, but effectively, these files will be parsed sequentially from Rules1..Rules4, until the requested rule is found, then scanning will stop immediately. And this scan is also performed on the cache (I expect you didn't disable that option in Tools/Advanced), and then 'jumped' to after opening the file, for speed improvements.
- On ESP32 the entire Rules files are cached in memory, for even better performance (ESP8266 doesn't have enough memory available to do that, that only caches the "on xxx do" line, with the starting-location in the file)
/Ton (PayPal.me)

mra2ko
Normal user
Posts: 80
Joined: 06 Dec 2021, 08:32
Location: Czech Republic

Re: RULE help

#5 Post by mra2ko » 27 Feb 2023, 12:38

Yes, I mean the "warning" Can you ask me, so what is the real limit for one rule ?


Ath wrote: 27 Feb 2023, 11:33
mra2ko wrote: 27 Feb 2023, 11:02 Work well, thnak ! But maybe one rule will be on maximum of data, can I switch bettwen rule 1 and rule 2 sets?
If you are talking about the warning when the rules file is > 2048 bytes, then don't worry, that limit is not really 'enforced', just a remainder of a technical issue that has been solved years ago.

But if you really want to split the rules, it could be like this:

Code: Select all

On Page1 do
  tftcmd,clear
  tft,txtfull,5,5,4,white,black,%systm_hm%
  tft,txtfull,80,55,2,white,black,out
  tft,txtfull,10,80,6,red,black,"[mqtt2#Value8]" 
  tft,txtfull,166,85,2,white,black,°
  tft,txtfull,180,85,4,white,black,C
  tft,txtfull,17,185,2,white,black,well
  tft,txtfull,10,210,3,white,black,"[mqtt3#Value1]cm"
Endon

On Page2 do
  tftcmd,clear
  tft,txtfull,5,5,4,white,black,%systm_hm%
  tft,txtfull,80,55,2,white,black,out
  // etc. data for the second page
Endon

On Rules#Timer=1 do
  If %v1%=0
    Let,1,1 // Next up: 2nd page
    asyncEvent,Page1
  Else
    Let,1,0 // Next up: 1st page
    asyncEvent,Page2
  Endif
Endon

On System#Boot do
  Let,1,0 // Select 1st page 0
  AsyncEvent,Rules#Timer=1 // Initial page display
  LoopTimerSet,1,60 // Schedule a page-switch every minute
Endon

NB:
- Rules that are executed most often should be placed first in the Rules1 file
- Because of Rules-caching this has become somewhat less important
- Rules that are only executed once, like on System#Boot do, can be moved to one of the other Rules files, to reduce the size of the Rules1 file, but effectively, these files will be parsed sequentially from Rules1..Rules4, until the requested rule is found, then scanning will stop immediately. And this scan is also performed on the cache (I expect you didn't disable that option in Tools/Advanced), and then 'jumped' to after opening the file, for speed improvements.
- On ESP32 the entire Rules files are cached in memory, for even better performance (ESP8266 doesn't have enough memory available to do that, that only caches the "on xxx do" line, with the starting-location in the file)

TD-er
Core team member
Posts: 8643
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: RULE help

#6 Post by TD-er » 27 Feb 2023, 13:00

I have tested it with 12k of rules files and that was working fine.
In (really) old builds, the limit of 2k was determined by the practical max. of a HTTP POST on an ESP8266.
A few years ago, we added some JavaScript which essentially makes a call to upload a file, instead of performing a POST.

This does essentially remove the previous limit of the file.

However, rules parsing itself still takes time.

The caching of the rules is done as follows:
Each line in the rules file with "on ... do" is stored in memory, along with some index of where to find this rule.
On ESP8266 we don't keep the rest of the rules in memory, but on ESP32 we do.
So on ESP8266, this index consists of the rules file nr and the position from the start of the file.
On ESP32, this is the line nr kept in memory. (std::vector<String>)

If you have large event blocks, you may still need to parse a lot of lines each time this event is handled.
So keep this in mind when writing rules.

The array of "on ... do" lines, kept in memory, is parsed in the order they appear in the rules file(s).
So put the most frequently occuring ones at the top, but also keep in mind that the more specific ones are put first.

For example:
"on foo#bar=1 do" is more specific than "on foo#bar do", since the latter one matches all occurences of events starting with "foo#bar".

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 23 guests