call a event from a rule
Moderators: grovkillen, Stuntteam, TD-er
call a event from a rule
hi al allready add 3 events, they are working fine
on forward do
if [R2#state]=0
gpio,13,1
gpio,12,1
delay,1000
gpio,13,0
GPIO,14,1
else
gpio,13,0
GPIO,14,1
endif
endon
on stop do
gpio,12,1
gpio,13,1
GPIO,14,0
endon
on reverse do
if [R1#state]=0
gpio,13,1
gpio,12,1
delay,1000
gpio,12,0
GPIO,14,1
else
gpio,12,0
GPIO,14,1
endif
endon
but i want to call any of this event when i push a button
on Button#State do
"call event reverse
endon
is this possible?
Many Thanks
Rira
on forward do
if [R2#state]=0
gpio,13,1
gpio,12,1
delay,1000
gpio,13,0
GPIO,14,1
else
gpio,13,0
GPIO,14,1
endif
endon
on stop do
gpio,12,1
gpio,13,1
GPIO,14,0
endon
on reverse do
if [R1#state]=0
gpio,13,1
gpio,12,1
delay,1000
gpio,12,0
GPIO,14,1
else
gpio,12,0
GPIO,14,1
endif
endon
but i want to call any of this event when i push a button
on Button#State do
"call event reverse
endon
is this possible?
Many Thanks
Rira
Re: call a event from a rule
You're already very close:
Code: Select all
on Button#State do
event reverse // A comma can also be used instead of the space separator
endon
Code: Select all
event,"reverse=1,2,3,4" // just example arguments to have something in %eventvalue1% to %eventvalue4%
Edit: Added the required quotes.
Last edited by Ath on 20 May 2021, 10:24, edited 1 time in total.
/Ton (PayPal.me)
Re: call a event from a rule
You have a delay of 1000 milliseconds, but the documentation states to avoid using the delay command, as delay is blocking execution for the entire unit. 1 second isn't really terrible, but it would be better to use a timer, like this example:
Code: Select all
on reverse do
if [R1#state]=0
gpio,13,1
gpio,12,1
TimerSet_ms,1,1000
else
gpio,12,0
GPIO,14,1
endif
endon
on rules#timer=1 do
gpio,12,0
GPIO,14,1
endon
/Ton (PayPal.me)
Re: call a event from a rule
This particular example given by Ton may need some extra attention.Ath wrote: ↑20 May 2021, 09:35 [...]
and if you need to add %eventvalue% arguments, you should do that like this:[...]Code: Select all
event,reverse=1,2,3,4 // just example arguments to have something in %eventvalue1% to %eventvalue4%
The argument of event is "reverse=1,2,3,4", which contains a comma.
So it should be wrapped in quotes:
Code: Select all
event,"reverse=1,2,3,4" // just example arguments to have something in %eventvalue1% to %eventvalue4%
And the other remark made by Ton should be considered for multiple reasons.
It is better to use asyncevent, compared to event.
Sometimes you need to have an event handled before you can continue, so then you can't use asyncevent, but try to use asyncevent where possible.
'event' is executed immediately, but does require the rules engine to start a new parsing of the rules.
This may need extra resources and may cause a 'stack overflow' (crash) if you call another event from that event, and another one, etc.
Not only does it consume a lot more resources, but also it does take a lot of time in which other jobs of the ESP cannot run (e.g. handling web page, acting on a sensor, taking a reading, etc.)
asyncevent does return immediately, as it only adds the event (+ event values) to a queue of events.
Queued events are handled in the order you create them, but they may be executed out of order compared to "event" calls.
Thus if those are depending on each other, they all should be either "event" or "asyncevent" calls.
Re: call a event from a rule
Hi,
I would add a subsidiary question/note to this one,
I use event call in rules to mimic method/function call.
I noted that if I launch an event, which launch an another one which launch a third one (not sure of the exact depth), the wemos crash and reboot.
If I prevent a direct event call by launching it with a millisecond timer, it would'nt crash anymore.
Is there any limitation here or it's due to the hardware?
I would add a subsidiary question/note to this one,
I use event call in rules to mimic method/function call.
I noted that if I launch an event, which launch an another one which launch a third one (not sure of the exact depth), the wemos crash and reboot.
If I prevent a direct event call by launching it with a millisecond timer, it would'nt crash anymore.
Is there any limitation here or it's due to the hardware?
Re: call a event from a rule
That's (almost) exactly the same effect as calling an asyncevent.
A timer event is a scheduled event. So it is not added to the event queue, but it is also not executed immediately as what the "event" command would do.
See it as a normal stack in programming.
This will create infinite recursion => stack overflow => crash
What our rules parser does each time it calls "event" is it pauses execution of the current rules, call a new rules parser (including all needed variables to keep track of the state of the parser) and start parsing from the beginning of the first rules file.
This does add to the very limited stack we have and on top of that does consume quite a bit of heap memory too.
Not sure what happens first, stack overflow or running out of heap memory.
So this should be avoided by either calling asyncevent, or schedule a timer (which is essentially the same, only executed at some time, instead of from a queue)
A timer event is a scheduled event. So it is not added to the event queue, but it is also not executed immediately as what the "event" command would do.
See it as a normal stack in programming.
Code: Select all
void functionA() {
functionB();
}
void functionB() {
functionC();
}
void functionC() {
functionA();
}
What our rules parser does each time it calls "event" is it pauses execution of the current rules, call a new rules parser (including all needed variables to keep track of the state of the parser) and start parsing from the beginning of the first rules file.
This does add to the very limited stack we have and on top of that does consume quite a bit of heap memory too.
Not sure what happens first, stack overflow or running out of heap memory.
So this should be avoided by either calling asyncevent, or schedule a timer (which is essentially the same, only executed at some time, instead of from a queue)
Re: call a event from a rule
Thanks for the explanation TD-er,
I've take car to avoid any loop in the call,
In my example it calls event from 2/3 different rule file, half of max size each.
I will test the AsyncEvent command i've passed through!
I've take car to avoid any loop in the call,
In my example it calls event from 2/3 different rule file, half of max size each.
I will test the AsyncEvent command i've passed through!
Re: call a event from a rule
Thank you very Much. i allready handled it to call an event! Many Thanks to all!
Who is online
Users browsing this forum: No registered users and 104 guests