Repeat an event x times (like a loop) (for-while)

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
rira2005
Normal user
Posts: 30
Joined: 25 Jun 2017, 21:09

Repeat an event x times (like a loop) (for-while)

#1 Post by rira2005 » 20 May 2021, 19:46

hi

so a add some events (forward,reverse,stop)
i want to repeat the
start forward event for 5 min
then reverse event for 5 min
then forward event for 5 min
then reverse event for 5 min
.....
maybe 10 times or more...

an when the pseudoloop is done
i want to stop with the event stop

the trigger i want to use to start this repeating events i a do on button....

to understand i want to clean my pool and the poolcleaner goes 5 min forward und 5 min backward... and so on...

May anyone have an idea.

TIA
Rira

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

Re: Repeat an event x times (like a loop) (for-while)

#2 Post by TD-er » 20 May 2021, 21:22

Have you looked at the looptimer? https://espeasy.readthedocs.io/en/lates ... oop-timers
This can be used to set a loop timer and also set the number of loops.

To switch between "forward" and "reverse", you could set a variable (see the "let" command) and set the looptimer for 5 minutes.
Every loop you just toggle the variable to switch between forward and reverse mode.

rira2005
Normal user
Posts: 30
Joined: 25 Jun 2017, 21:09

Re: Repeat an event x times (like a loop) (for-while)

#3 Post by rira2005 » 20 May 2021, 22:59

hi
many thx for your help... but i didnt understand the exact commands

tried this one;

on cleanstart do
looptimerset,3,15,10 // loop 1 forward 15 sec / 10 times repead
looptimerset,4,15,10 // loop 2 reverse 15 sec / 10 times repead
endon

On Rules#Timer=3 do
if %eventvalue2% >= 5 // -> what does the >=5 mean ??!
timerSet,3,0 // Stop timer 3 /why the timer will stoped there
endif
event forward
logentry,%eventvalue2% // log the loop count
endon

On Rules#Timer=4 do
if %eventvalue2% = 2 // -> what does the =2 mean ??!
loopTimerSet,4,15 // Restart loop timer 4 (thus clearing loop count)
timerResume,3 // why this what du this
event reverse
else
timerPause,3 // why this what du this
endif
endon

..........................................................
on cleanstop do
event stop
timerSet,3,0
timerSet,4,0
endon


but it didnt works it olny starts one time forward und this was it...
Sorry for my poor programing skills but my last time i program something it was in pascal in the 1990s....




to understand

i only want to do a loop with
--------------------------------------------------------
15 sec the event forward |
then 15 sec the event reverse |
-------------------------------------------------------
-----> do this 10 times....

tia
Rira

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

Re: Repeat an event x times (like a loop) (for-while)

#4 Post by TD-er » 21 May 2021, 09:24

rira2005 wrote: 20 May 2021, 22:59 hi
many thx for your help... but i didnt understand the exact commands

tried this one;

Code: Select all

on cleanstart do
 looptimerset,3,15,10 // loop 1 forward 15 sec / 10 times repead
 looptimerset,4,15,10 // loop 2 reverse 15 sec / 10 times repead
endon

On Rules#Timer=3 do
if %eventvalue2% >= 5              // -> what does the >=5 mean ??!
   timerSet,3,0  // Stop timer 3      /why the timer will stoped there
endif
   event forward
  logentry,%eventvalue2%  // log the loop count
endon

On Rules#Timer=4 do
if %eventvalue2% = 2 // -> what does the =2 mean ??!
    loopTimerSet,4,15  // Restart loop timer 4 (thus clearing loop count)
    timerResume,3    // why this what du this
    event reverse
  else
    timerPause,3 // why this what du this
  endif
endon

..........................................................
on cleanstop do
event stop
timerSet,3,0
timerSet,4,0
endon
but it didnt works it olny starts one time forward und this was it...
Sorry for my poor programing skills but my last time i program something it was in pascal in the 1990s....

to understand

i only want to do a loop with
--------------------------------------------------------
15 sec the event forward |
then 15 sec the event reverse |
-------------------------------------------------------
-----> do this 10 times....

tia
Rira
I think it can be as simple as this:

Code: Select all

on cleanstart do
 looptimerset,3,15,20 // every 15 seconds, repeat 20 times (10 times forward, 10 reverse)
endon

On Rules#Timer=3 do
  let,1,%eventvalue2%  // Store the loop count in a variable
  if [int#1]%2=0       // Check if the loop count modulo (%) 2 = 0
    asyncevent,forward
  else
    asyncevent,reverse
  endif
endon
What it does:
It does start a single loop timer for 20 loops (10 forward, 10 reverse) with 15 sec interval.
I store the loop counter in a variable first since I need to use the "%" operator and I was not sure if that would interfere with the % in %eventvalue2%. It may be possible to have it even shorter.
What the modulo (%) operator does is, it is computing what is 'left' if you divide an integer number.
e.g. 9%2 = 1 as you can four times subtract '2' from '9' and then are left with '1'
Thus:
- N%2=0 -> even numbers
- N%2=1 -> odd numbers

So on the 'even loops' you call the event "forward" and on the odd loops you call the even "reverse".
I changed "event" to "asyncevent" to add the event to an event queue so the ESP will not be too long in processing the rules.

rira2005
Normal user
Posts: 30
Joined: 25 Jun 2017, 21:09

Re: Repeat an event x times (like a loop) (for-while)

#5 Post by rira2005 » 04 Jun 2021, 11:24

hi i done it lite this, many thanks to all!

on cleanstart do
Publish domoticz/in,'{"idx":158,"nvalue":1,"svalue":""}' //mqtt info to start the event
asyncevent,reverse //start yet not to wait 30 seconds
let,2,1 //forward and reverse var to for the if set to 1 --> forward
looptimerset,3,30,200 // every 30 seconds, repeat 200 times (100 times forward, 100 reverse)
endon

On Rules#Timer=3 do
logentry,%eventvalue2% // log the loop count
if %eventvalue2% >= 200 //check the loop count is more the 200 if
asyncevent,cleanstop //call the event cleanstop
elseif [int#2]=1 // if direction var is 1(reverse) do the event forward
asyncevent,forward //call the forward event
let,2,0 //forward and reverse var to for the if set to 0 --> reverse
else // else direction var is 0(forward) do the event reverse
asyncevent,reverse //call the reverse event //call the forward event
let,2,1 //forward and reverse var to for the if set to 0 --> reverse
endif
endon

on cleanstop do
Publish domoticz/in,'{"idx":158,"nvalue":0,"svalue":""}' //mqtt info to stop the event
event,stop //call the stop event
looptimerset,3,0 //stop the timer before the loops ends (to stop it with buttoon or http req. via domoticz)
endon

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
on forward do
if [R2#state]=0
gpio,13,1
gpio,12,1
TimerSet_ms,1,1000
else
gpio,13,0
GPIO,14,1
let,1,1
endif
endon

on stop do
gpio,12,1
gpio,13,1
GPIO,14,0
let,1,0
endon

on reverse do
if [R1#state]=0
gpio,13,1
gpio,12,1
TimerSet_ms,2,1000
else
gpio,12,0
GPIO,14,1
let,1,1
endif
endon

on rules#timer=1 do
gpio,13,0
GPIO,14,1
let,1,1
endon

on rules#timer=2 do
gpio,12,0
GPIO,14,1
let,1,1
endon
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

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

Re: Repeat an event x times (like a loop) (for-while)

#6 Post by Ath » 05 Jun 2021, 13:30

TD-er wrote: 21 May 2021, 09:24 I store the loop counter in a variable first since I need to use the "%" operator and I was not sure if that would interfere with the % in %eventvalue2%. It may be possible to have it even shorter.
The expression processing actually handles that correctly, so this code:

Code: Select all

  if %eventvalue2%%2=0       // Check if the loop count modulo (%) 2 = 0
works as expected.
/Ton (PayPal.me)

Post Reply

Who is online

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