Rule optimization

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
Rampler
Normal user
Posts: 95
Joined: 31 Dec 2016, 07:13

Rule optimization

#1 Post by Rampler » 29 Apr 2023, 18:18

Hi together,
i have the following working rules..
Is that ok from a performance point of view, or can I simplify it too?

Rule 1:

Code: Select all

on System#Boot do
 timerSet,1,1           // Init Display after 1 Seconds
 lcdcmd,off 
 pwm,5,20
 Let 1,20
endon

on Rules#Timer=1 do     // Wait until MQTT is connected
 lcdcmd,on                  
 lcd,1,1,Waiting... 
 lcd,2,1,Waiting...
endon

on MQTT#Connected do     // Start Display wrap around 
  timerSet,2,3
  notify 1
endon

on MQTT#Disconnected do  // Reset Display when ESP are disconnected ..
 timerSet,2,0
 timerSet,3,0
 timerSet,4,0
 timerSet,5,0
 timerSet,6,0
 notify 1
 lcd,1,1,'MQTT disconnect!'
 lcd,2,1,'be cool ..   '
endon

on Rules#Timer=2  do
 lcd,1,1,'Wind [MQTT_IMPORT1#ws] km/h     x' 
 lcd,2,1,'%sysweekday_s% %sysday%-%sysmonth%-%sysyear%     x'
 timerSet,3,3
endon

on Rules#Timer=3  do
 lcd,1,1,'Out T:[MQTT_IMPORT1#out_temp] H:[MQTT_IMPORT1#out_hum]   x'
 lcd,2,1,'In  T:[T&H#in_temp] H:[T&H#in_hum#D2.0]    x'
 timerSet,4,3
endon

on Rules#Timer=4  do
 lcd,1,1,'Rain [MQTT_IMPORT1#Raintoday] l/qm   x '  
 lcd,2,1,'CO2  [CO2#CO2] ppm     x'
 timerSet,5,3
endon

on Rules#Timer=5  do
 lcd,1,1,'APS-A [MQTT_IMPORT2#Pressure] hPA      x'
 lcd,2,1,'Time  %systime%     x'
  timerSet,6,3
endon

on Rules#Timer=6  do
   if [MQTT_IMPORT2#Tendency] > 0
    lcd,1,1,'APS-T +[MQTT_IMPORT2#Tendency] (3h)    x'
  else
    lcd,1,1,'APS-T [MQTT_IMPORT2#Tendency] (3h)    x'
  endif
  lcd,2,1,'SR %sunrise% SS %sunset% '
  timerSet,2,3
endon


Rule2: (gpio 5 controls the brightness of the lcd display via a light dependent resisitor)

Code: Select all

on LDR#Light do
 If [LDR#Light] >0   and  [LDR#Light] <20 and [VAR#1] != 1
  pwm,5,1,3000
  Let 1,1
 endif
 If [LDR#Light] >20   and  [LDR#Light] <100 and [VAR#1] != 5
  pwm,5,5,3000
  Let 1,5
 endif
 If [LDR#Light] >100 and  [LDR#Light] <550 and [VAR#1] != 10
  pwm,5,10,3000
  Let 1,10
 endif
 If [LDR#Light] >550 and  [LDR#Light] <650 and [VAR#1] != 20
  pwm,5,20,3000
  Let 1,20
 endif
 If [LDR#Light] >650 and  [LDR#Light] <750 and [VAR#1] != 40
  pwm,5,40,3000
  Let 1,40
 endif
 If [LDR#Light] >750 and  [LDR#Light] <900 and [VAR#1] != 50
  pwm,5,50,3000
  Let 1,50
 endif
 If [LDR#Light] >900 and  [LDR#Light] <1010 and [VAR#1] != 100
  pwm,5,100,3000
  Let 1,100
 endif
 If [LDR#Light] >=1010  and  [VAR#1] != 800
  pwm,5,800,3000
  Let 1,800
 endif
endon

on Clock#Time=All,**:00 do   //**:** ==> jede Minute  **:00 ==> jede Stunde
  If %systime% > 05:00:00 and %systime% < 23:00:00
    rtttl,4:d=1000,o=8,b=6,c,,,,,,,,c
  Endif
endon

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

Re: Rule optimization

#2 Post by TD-er » 29 Apr 2023, 21:51

You're in the "on LDR#Light do" block, so the value of this [LDR#Light] is also present as %eventvalue1%

It's better to use %eventvalue1% instead of [LDR#Light] as that was the actual value when the event was generated.

So I advice you to replace [LDR#Light] with %eventvalue1% in your rules2.

Another "bug" in your code is that on some discrete values of 20, 100, 550, etc. the rules will do nothing.
You should not compare with < 20 but <= 20, etc.



The timers you're using in rules1 will work just fine.
If you only use them to "switch pages" on your display, you could also have used a variable.
Only advantage of using a variable is that you don't need to reset all timers and can continue on the same page as you left off when MQTT got disconnected.
Disadvantage is that there are more lines to process in the rules and if you like to show some 'pages' for a different duration, using timers is by far the easiest way to handle this.
I think this approach using timers is perfectly fine for this use case.

Rampler
Normal user
Posts: 95
Joined: 31 Dec 2016, 07:13

Re: Rule optimization

#3 Post by Rampler » 30 Apr 2023, 00:53

:D
Thank you for your comment
:D

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 47 guests