how to deal with more than 1 rule

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
BartSr
Normal user
Posts: 122
Joined: 27 Sep 2019, 17:45

how to deal with more than 1 rule

#1 Post by BartSr » 13 Nov 2023, 19:05

Hi!
I've written a rule which size is over 2024 bytes.
But as my rule needs more memory I removed all the indents.
So the rule is very hard to read now and all comments are deleted as well
This is unwanted.
As there are four rulesets I think here may be a solution.
Q:
is there a relation between these rules
what is sequence? first ruleset 1,2,3 and four?
Or is there docu I overlooked?

TIA.
Bart

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

Re: how to deal with more than 1 rule

#2 Post by Ath » 13 Nov 2023, 19:24

The warning about the size can be ignored (up to a reasonable limit, but 8 to 10 kB has proven to still work, albeit taking more time to upload). The warning was needed before the 'new' upload code was created, about 4 years ago...

Depending on what you are trying to do, is that code a single rule (on x#y do ... endon) or a group of these event handlers (what they actually are)?

A lot of advice and suggestions are in the Rules page of the documentation, with a lot of technical info and examples.

Parsing the rules (it's an interpreter) starts at line 1 of the rules1.txt file, and continues until a matching rule for the current event is found, continuing via rules2 and rules3 to rules4.txt if they exist when no match is found. It will help performance if the rules that are executed most often are placed as first in rules1.txt, and f.e. the system#boot rule near the end of your code, as it's only executed once ;)

Don't take this the wrong side of the road, it's better to have small rules, and as few as reasonably possible; this is meant as a data-gathering device, not as a number-cruncher, that's better handed to more powerful devices, like RPi, PC or server :?
/Ton (PayPal.me)

BartSr
Normal user
Posts: 122
Joined: 27 Sep 2019, 17:45

Re: how to deal with more than 1 rule

#3 Post by BartSr » 13 Nov 2023, 20:01

Ath,
Here's my rule.
No rocket science just logic.

Spread over the house I have a number of ESP's monitoring the doorlocks,
All these esp's provide via mqtt status to domoticz.
The code downbelow is to get a central panel near bedroom which, once fired the LEDweergave button it make LED's indicate al doors status.
So I don't think more compression of the code is needed.
I'm going to have a look at your link about rules.
Thanks!
-Bart

Code: Select all

on System#Boot do
  LoopTimerSet,1,45
enddo

Let,1,1
Let,2,1
Let,3,1
Let,4,1
Let,5,1

//LED
On Ledweergave#State do
  if [VAR#1]=1
    GPIO,12,0//D6 voordeur
  endif
  if [VAR#2]=1 
    GPIO,4,0//D2 achterdeur
  endif
  if [VAR#3]=1 
    GPIO,13,0//D7 slaapkamer
  endif
  if [VAR#4]=1 
    GPIO,14,0//D5 tuinkast
  endif
  if [VAR#5]=1 
    GPIO,15,0//D8 schuurdeur
    GPIO,16,0//D8 kleine poort
  endif
endon

on Rules#Timer=1 do

  if [Voordeur#Fails] < 7
    Let,1,1
    if [Plugin#GPIO#Pinstate#12]=0
      Publish domoticz/in,'{"idx":1003,"svalue":"OPEN","nvalue":4}'
    else 
      Publish domoticz/in,'{"idx":1003,"svalue":"DICHT","nvalue":1}'
    endif
    if  if [Voordeur#Fails] =>7
      Let,1,0
      Publish domoticz/in,'{"idx":1003,"svalue":"FOUT","nvalue":3}'  
    endif
  endif

  if [Achterdeur#Fails] < 7
    Let,2,1
    if [Plugin#GPIO#Pinstate#4]=0
      Publish domoticz/in,'{"idx":1004,"svalue":"OPEN","nvalue":4}'
    else
      Publish domoticz/in,'{"idx":1004,"svalue":"DICHT","nvalue":1}'
    endif
  endif

  if [Achterdeur#Fails] => 7
    Let2,0,
    Publish domoticz/in,'{"idx":1004,"svalue":"PROBLEEM","nvalue":3}'  
  endif

  if [Slaapkamerdeur#Fails] < 7
    Let,3,1
    if [Plugin#GPIO#Pinstate#13]=0
      Publish domoticz/in,'{"idx":1005,"svalue":"OPEN","nvalue":4}'
    else
      Publish domoticz/in,'{"idx":1005,"svalue":"DICHT","nvalue":1}'
    endif
  else 
    Let,3,0
    Publish domoticz/in,'{"idx":1005,"svalue":"PROBLEEM","nvalue":3}'  
  endif

  if [Tuinkastdeur#Fails] < 7
    Let,4,1
    if [Plugin#GPIO#Pinstate#14]=0   
      Publish domoticz/in,'{"idx":1006,"svalue":"OPEN","nvalue":4}'
    else
      Publish domoticz/in,'{"idx":1006,"svalue":"DICHT","nvalue":1}'
    endif
  else 
    Let,4,0  
    Publish domoticz/in,'{"idx":1006,"svalue":"PROBLEEM","nvalue":4}'  
  endif

  if [Schuurdeur#Fails] < 7
    Let,5,1
    if [Plugin#GPIO#Pinstate#15]=0
      Publish domoticz/in,'{"idx":1007,"svalue":"OPEN","nvalue":4}'
    else
      Publish domoticz/in,'{"idx":1007,"svalue":"DICHT","nvalue":1}'
    endif
  else
    Let,5,0
    Publish domoticz/in,'{"idx":1007,"svalue":"PROBLEEM","nvalue":4}'
  endif
endon

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

Re: how to deal with more than 1 rule

#4 Post by Ath » 13 Nov 2023, 20:42

You have a few typos and syntax errors in your code:
- Syntax error: Any statement outside of and "on event do / endon" is ignored, so the let commands won't do anything
- Typo: "let2,0," won't work as expected
- Syntax error: "if if ..."
- Question: When is "on Ledweergave#State do" called?

I'm not sure why you are polling the GPIO pins, you'd better respond to an event once the state of a pin changes. That requires a complete rewrite of the code, but is more in the spirit of how an event system is intended to be used...
Checking the state and sending the status to Domoticz can be handled in a single rule, making your code much more compact, another way to rewrite the code...

Your code with most issues fixed (no rewrite, yet :))

Code: Select all

on Rules#Timer=1 do
  if [Voordeur#Fails] < 7
    Let,1,1
    if [Plugin#GPIO#Pinstate#12]=0
      Publish domoticz/in,'{"idx":1003,"svalue":"OPEN","nvalue":4}'
    else 
      Publish domoticz/in,'{"idx":1003,"svalue":"DICHT","nvalue":1}'
    endif
  else  // [Voordeur#Fails] =>7
    Let,1,0
    Publish domoticz/in,'{"idx":1003,"svalue":"FOUT","nvalue":3}'  
  endif

  if [Achterdeur#Fails] < 7
    Let,2,1
    if [Plugin#GPIO#Pinstate#4]=0
      Publish domoticz/in,'{"idx":1004,"svalue":"OPEN","nvalue":4}'
    else
      Publish domoticz/in,'{"idx":1004,"svalue":"DICHT","nvalue":1}'
    endif
  else  // [Achterdeur#Fails] => 7
    Let,2,0
    Publish domoticz/in,'{"idx":1004,"svalue":"PROBLEEM","nvalue":3}'  
  endif

  if [Slaapkamerdeur#Fails] < 7
    Let,3,1
    if [Plugin#GPIO#Pinstate#13]=0
      Publish domoticz/in,'{"idx":1005,"svalue":"OPEN","nvalue":4}'
    else
      Publish domoticz/in,'{"idx":1005,"svalue":"DICHT","nvalue":1}'
    endif
  else  // [Slaapkamerdeur#Fails] >= 7
    Let,3,0
    Publish domoticz/in,'{"idx":1005,"svalue":"PROBLEEM","nvalue":3}'  
  endif

  if [Tuinkastdeur#Fails] < 7
    Let,4,1
    if [Plugin#GPIO#Pinstate#14]=0   
      Publish domoticz/in,'{"idx":1006,"svalue":"OPEN","nvalue":4}'
    else
      Publish domoticz/in,'{"idx":1006,"svalue":"DICHT","nvalue":1}'
    endif
  else  // [Tuinkastdeur#Fails] >= 7
    Let,4,0  
    Publish domoticz/in,'{"idx":1006,"svalue":"PROBLEEM","nvalue":4}'  
  endif

  if [Schuurdeur#Fails] < 7
    Let,5,1
    if [Plugin#GPIO#Pinstate#15]=0
      Publish domoticz/in,'{"idx":1007,"svalue":"OPEN","nvalue":4}'
    else
      Publish domoticz/in,'{"idx":1007,"svalue":"DICHT","nvalue":1}'
    endif
  else  // [Schuurdeur#Fails] >= 7
    Let,5,0
    Publish domoticz/in,'{"idx":1007,"svalue":"PROBLEEM","nvalue":4}'
  endif
endon

//LED
On Ledweergave#State do
  if [VAR#1]=1
    GPIO,12,0//D6 voordeur
  endif
  if [VAR#2]=1 
    GPIO,4,0//D2 achterdeur
  endif
  if [VAR#3]=1 
    GPIO,13,0//D7 slaapkamer
  endif
  if [VAR#4]=1 
    GPIO,14,0//D5 tuinkast
  endif
  if [VAR#5]=1 
    GPIO,15,0//D8 schuurdeur
    GPIO,16,0//D0 kleine poort
  endif
endon

on System#Boot do
  LoopTimerSet,1,45
  Let,1,1
  Let,2,1
  Let,3,1
  Let,4,1
  Let,5,1
enddo
/Ton (PayPal.me)

BartSr
Normal user
Posts: 122
Joined: 27 Sep 2019, 17:45

Re: how to deal with more than 1 rule

#5 Post by BartSr » 13 Nov 2023, 21:11

Ath,
I extended the code just today so not already all was tested and also removing all indents did not made the code better to understand.
- Question: When is "on Ledweergave#State do" called?
As the led's are in a bedroom I don't want the led's visible at night. So a button activates the led's.
If an esp shoud be down the ping-routine prevent the led to go on. Hopefully it does what I expect. Has to be checked yet.

The strophe re if if I don't understand. Or do you mean
if [Voordeur#Fails] < 7
Let,1,1
if [Plugin#GPIO#Pinstate#12]=0
This seems to work. Ofcourse with all trailing endif's and endon's.
The local esp's constantly update the GPIO on this main esp and yes, maybe an event-driven solution is better but then I need to check what happens if a local esp gets disconnect (unwated of course)
I indeed felt that the command might be mofre efficiënt but could not find that easy the syntax for a 'how to'.
Remember I quit often need to redig the docu as (maybe it's my age , almost 70). But I love doing this.

And yes typo's like let2,0, should not happen....

Anyway thanks for going quickly through my code.

Bart

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

Re: how to deal with more than 1 rule

#6 Post by Ath » 13 Nov 2023, 21:37

BartSr wrote: 13 Nov 2023, 21:11 The strophe re if if I don't understand. Or do you mean
if [Voordeur#Fails] < 7
Let,1,1
if [Plugin#GPIO#Pinstate#12]=0
I was referring to this part:

Code: Select all

    endif
    if  if [Voordeur#Fails] =>7
      Let,1,0
I changed that to an "else", like you did in the later parts you added for Schuurdeur, Tuinkastdeur and I think Slaapkamerdeur too, and I also fixed that for the Achterdeur.
BartSr wrote: 13 Nov 2023, 21:11 The local esp's constantly update the GPIO on this main esp and yes, maybe an event-driven solution is better but then I need to check what happens if a local esp gets disconnect (unwated of course)
Are you using the "SendTo" command to update the pin state on the main ESP, or using wires to actually change the level on the unit?
When using SendTo, you could use var's (let,x,<state>) instead of setting a GPIO, when using wires, well, you shouldn't need remote ESPs then :lol:
/Ton (PayPal.me)

BartSr
Normal user
Posts: 122
Joined: 27 Sep 2019, 17:45

Re: how to deal with more than 1 rule

#7 Post by BartSr » 13 Nov 2023, 22:26

Ath, indeed I used Sendto for those pins as Led will be connected to it. Once I should more VAR's, dont't we gave a limit if amount? Shouldnot that be 10?

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

Re: how to deal with more than 1 rule

#8 Post by TD-er » 13 Nov 2023, 22:36

Vars don't have a limit...
well you can go upto 2^32 - 1, or a bit over 4'000'000'000

The max. depth of if-statements is 9 or 10 or something like that.
But that's like

Code: Select all

if ...
  if ...
    if ...
    
And then 10 levels deep.
If you really need so many, then I guess it is time to have a good look at your code as it can be optimized for sure.

I don't think I ever needed so many if-levels in my programming in the past 35 years.

Post Reply

Who is online

Users browsing this forum: No registered users and 28 guests