Skip to content

Commit

Permalink
Adding more preprocessing to rule line evaluation
Browse files Browse the repository at this point in the history
  • Loading branch information
enesbcs committed Apr 11, 2021
1 parent 4affe82 commit 2ec5b36
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
37 changes: 35 additions & 2 deletions commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,19 @@ def gettaskvaluefromname(taskname): # taskname#valuename->value
res=None
return res

def gettaskvaluesfromname(taskname): # taskname->value1,2,3,4
res = []
try:
for s in range(len(Settings.Tasks)):
if type(Settings.Tasks[s]) is not bool:
if Settings.Tasks[s].taskname.lower()==taskname:
for v in range(len(Settings.Tasks[s].valuenames)):
res.append(Settings.Tasks[s].uservar[v])
break
except:
res=[]
return res

suntimesupported = -1

def addtoTime(basetime, deltastr): # -1h +2h -10m +3m ...
Expand Down Expand Up @@ -1107,7 +1120,7 @@ def parseformula(line,value):
fv = parsevalue(l2)
return fv

def rulesProcessing(eventstr,efilter=-1): # fire events
def rulesProcessing(eventstr,efilter=-1,startn=0): # fire events
global GlobalRules, EventValues
rfound = -1
retval = 0
Expand Down Expand Up @@ -1138,7 +1151,24 @@ def rulesProcessing(eventstr,efilter=-1): # fire events
EventValues[1] = str(rpieTime.Timers[tn-1].loopcount)
except:
pass
for r in range(len(GlobalRules)):
else: # hack taskvalues to eventvalues for lazy users
try:
tname = estr
if "=" in tname:
tarr = tname.split("=")
tname = tarr[0].strip()
if '#' in tname:
ev = gettaskvaluefromname(tname)
if ev is not None:
EventValues = [0,0,0,0]
EventValues[0] = str(ev)
else:
ev = gettaskvaluefromnames(tname)
if len(ev) < 1:
EventValues = ev
except:
pass
for r in range(startn,len(GlobalRules)):
if efilter!=-1:
if GlobalRules[r]["ecat"]==efilter: # check event based on filter
if efilter == rpieGlobals.RULE_TIMER:
Expand Down Expand Up @@ -1238,6 +1268,9 @@ def rulesProcessing(eventstr,efilter=-1): # fire events
cret = doExecuteCommand(retval,False) # execute command
except Exception as e:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"Parsed line: "+str(GlobalRules[rfound]["ecode"][rl])+" "+str(e))
if startn < len(GlobalRules)-1:
startn += 1
rulesProcessing(eventstr,efilter,startn) #recursive search for further events with the same name

def comparetime(tstr):
result = True
Expand Down
2 changes: 1 addition & 1 deletion rpieGlobals.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# Copyright (C) 2018-2020 by Alexander Nagy - https://bitekmindenhol.blog.hu/
#
PROGNAME = "RPIEasy"
BUILD = 21094
BUILD = 21101
PROGVER = str(BUILD)[:1]+"."+str(BUILD)[1:2]+"."+str(BUILD)[2:]

gpMenu = []
Expand Down

8 comments on commit 2ec5b36

@clumsy-stefan
Copy link
Contributor

@clumsy-stefan clumsy-stefan commented on 2ec5b36 Apr 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure, if it's my installation, but since this commit no task is automatically executed anymore (at interval).

I see a lot of:
Plugin SendData Exception: can't start new thread ['1', 0, 0, 0]
and
!Parsed line: sendToHTTP,10.0.0.1,8072,/fhem?cmd=setreading%20rpieasy_02_ch05%20speed%20%eventvalue% can't start new thread
in the logfile.

@enesbcs
Copy link
Owner Author

@enesbcs enesbcs commented on 2ec5b36 Apr 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interesting. looks like you find a way to create an endless loop.
please attach full rules.

did you tried to comment out line 1273 in commands.py?

@clumsy-stefan
Copy link
Contributor

@clumsy-stefan clumsy-stefan commented on 2ec5b36 Apr 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commenting out line 1273 seems to solve the issue.

Rules did not change and are more or less identical to what I use on ESPEasy (FIY: currently I have a ESPEasy node running productively which does exact the same as this RPIEasy unit, just that the GPIO's are physically connected to the ESPEasy and, at some point in time, I plan to move them to the RPI, that's why I test quite thouroughly, as I use this node to run my complete pool control/management, and I don't want to get flooded or similar πŸ˜„ ....)

Full Ruleset:

  event,ch14r=0 // Teichpumpe
  event,ch15r=0 // Teichfilter
  event,ch9r=1 // Heizung
  event,ch5r=0 // Ventilator
  event,ch5d=1 // Ventilator Speed
endon

 //  event,ch5d=1 //Min RPM Ventilator

on System#Sleep do
 //  nfx,off,0
 //  rtttl,15:d=4,o=5,b=125:c.,c,8c,c.,d#,8d,d,8c,c,8c,2c.
endon


 // Ruleset 2

on ch1r do
  if [pump#valve]=0
    pcfgpio,1,%eventvalue%
  endif
  TaskRun,17
endon

on ch2r do
  if [pump#valve]=0
    pcfgpio,2,%eventvalue%
  endif
  TaskRun,18
endon

on ch3r do
  if [pump#valve]=0
    pcfgpio,3,%eventvalue%
  endif
  TaskRun,19
endon

on ch4r do
  TaskValueSetAndRun,6,4,1
  TaskRun,6
  pcfgpio,1,1
  TaskRun,17
  pcfgpio,2,1
  TaskRun,18
  pcfgpio,3,1
  TaskRun,19
  pcfgpio,4,%eventvalue%
  TaskRun,20
  timerSet,4,45
endon

on Rules#Timer=4 do
  TaskValueSetAndRun,6,4,0
  TaskRun,6
  event,pump#speed=[pump#speed]
endon

on ch5r do
  pcfgpio,5,%eventvalue%
  TaskRun,21
endon

on ch5d do
  if %eventvalue% < 1
    pcfgpio,5,1
    pwm,18,0
  else
    pcfgpio,5,0
    if %eventvalue% > 100
      pwm,18,100
    else
      pwm,18,%eventvalue%
    endif
  endif
  TaskRun,21
endon

 // Event,pwm#speed=%eventvalue%


on pwm#speed do
  sendToHTTP,10.0.0.1,8072,/fhem?cmd=setreading%20rpieasy_02_ch05%20speed%20%eventvalue%
endon

on ch6r do
  pcfgpio,6,%eventvalue%
  TaskRun,22
endon

on ch7r do
  pcfgpio,7,%eventvalue%
  TaskRun,23
endon

on ch8r do
  pcfgpio,8,%eventvalue%
  TaskRun,24
endon

on ch9r do
  pcfgpio,9,%eventvalue%
  TaskRun,25
endon

on ch10r do
  pcfgpio,10,%eventvalue%
  TaskRun,26
endon

on ch11r do
  pcfgpio,11,%eventvalue%
  TaskRun,27
endon

on ch12r do
  pcfgpio,12,%eventvalue%
  TaskRun,28
endon

on ch13r do
  pcfgpio,13,%eventvalue%
  TaskRun,29
endon

on ch14r do
  pcfgpio,14,%eventvalue%
  TaskRun,30
endon

on ch15r do
  pcfgpio,15,%eventvalue%
  TaskRun,31
endon

on ch16r do
  pcfgpio,16,%eventvalue%
  TaskRun,32
endon

on ch17r do
  pcfgpio,17,%eventvalue%
  TaskRun,33
endon

on ch18r do
  pcfgpio,18,%eventvalue%
  TaskRun,34
endon

on ch19r do
  pcfgpio,19,%eventvalue%
  TaskRun,35
endon

on ch20r do
  pcfgpio,20,%eventvalue%
  TaskRun,36
endon

on ch21r do
  pcfgpio,21,%eventvalue%
  TaskRun,37
endon

on ch22r do
  pcfgpio,22,%eventvalue%
  TaskRun,38
endon

on ch23r do
  pcfgpio,23,%eventvalue%
  TaskRun,39
endon

on ch24r do
  pcfgpio,24,%eventvalue%
  TaskRun,40
endon


 // Ruleset 3

on Clock#Time=All,10:00 do // switch pool UV-lamp on
  event,UV=on
  event,ch11r=0
endon

on Clock#Time=All,16:00 do // switch pool UV-lamp off
  event,UV=off
  event,ch11r=1
endon

on rotate do
  TaskValueSetAndRun,6,3,%eventvalue%
  TimerSet,3,[pump#rotate]
endon

on Rules#Timer=3 do
  if [pump#speed]>0
    if [ch4#switch]=1
      event,ch4r=0
    else
      event,ch4r=1
    endif
  endif
  TimerSet,3,[pump#rotate]
endon

on pump#increase do
  event,speed=[pump#speed]+1
 //  TaskValueSetAndRun,6,1,[pump#speed]+1
endon

on pump#decrease do
  event,speed=[pump#speed]-1
endon

on speed do
  if %eventvalue% > 3
    TaskValueSetAndRun,6,1,0
  else
    if %eventvalue% < 0
      TaskValueSetAndRun,6,1,3
    else
      TaskValueSetAndRun,6,1,%eventvalue%
    endif
  endif
  TaskRun,6
endon




on pump#speed do
  if %eventvalue% = 3 or %eventvalue% < 0
    event,ch3r,0
    event,ch1r,1
    event,ch2r,1
  endif
  if %eventvalue% = 2
    event,ch2r,0
    event,ch1r,1
    event,ch3r,1
  endif
  if %eventvalue% = 1
    event,ch1r,0
    event,ch2r,1
    event,ch3r,1
  endif
  if %eventvalue% = 0 or %eventvalue% > 3
    event,ch1r,1
    event,ch2r,1
    event,ch3r,1
  endif
endon


 // Ruleset 4

 // IR / Licht 
on ch17#switch do
  if %eventvalue% = 1
    event,ch16r=0
  else
    event,ch16r=1
  endif
endon


on sht30#temperature do // Heizung und Lueftung
  if %eventvalue% < 5
    event,ch9r=0
    event,ch5d=1
    TimerSet,2,1800
  endif
  if %eventvalue% > 8
    TimerSet,2,1
  endif
endon

on Rules#Timer=2 do
  event,ch9r=1 // Heizung aus
  if [var#11] < 8
    event,ch5d=0 // Lueftung aus
  endif
endon


on weather#all do
  let,11,%eventvalue1%
  let,12,%eventvalue2%
  let,13,%eventvalue3%
endon

// on weather#temperature do
//   let,11,%eventvalue%
// endon
// on weather#humidity do
//   let,12,%eventvalue%
// endon
// on weather#pressure do
//   let,13,%eventvalue%
// endon


on Clock#Time=All,11:11 do // Leitungen spuelen ein
  event,speed=3
  TimerSet,3,40 // wechsel wasserfall <> massage
endon
on Clock#Time=All,11:12 do // Luftsprudel ein
  event,ch12r=0
endon
on Clock#Time=All,11:13 do // Leitungen spuelen und Luftsprudel aus
  event,ch12r=1
  event,speed=0
endon
on Clock#Time=All,11:15 do // Leitungen spuelen und Luftsprudel aus
  event,ch12r=1
  event,speed=0
endon

on sht30#temperature do
  let,1,exp([sht30#temperature]/6)
  let,2,exp([sht30#humidity]/15)
  if [int#1] > [int#2]
    let,3,[var#1]*10-150
  else
    let,3,[var#2]*10-150
  endif
  // event,ch5d=[int#3]
endon```

@enesbcs
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanx, i will test it!

@clumsy-stefan
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PS: probably a small explanation why I use these ch<nr>r events: as in ESPeasy a change of a physical PCFGPIO (input) switches the assiciated task with that value and generates an event. Now when changing the same PCFGPIO with a command from somewhere else (eg. a rule or form the server) it would generate an endless loop. This is because you can use PCFGPIO's as input and output in parallel.

@enesbcs
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recursion fixed in commit 6586a67

@clumsy-stefan
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did a quick check, seems to be ok again. Thanks for the quick fix!!

@enesbcs
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanx for testing!

Please sign in to comment.