Notice: This forum has been recovered from an old backup, so some content, links, and dates may be outdated. The forum is currently read-only while we restore sign-in and registration functionality. Details

If you find this forum valuable and would like to help keep it online, donations to help cover hosting and domain costs are greatly appreciated, but never expected. You can support the forum through Buy Me a Coffee or Ko-fi. Thank you for helping preserve the EventGhost community.

Allow script to run only once in a given period of time.

Do you have questions about writing plugins or scripts in Python? Meet the coders here.
Post Reply
pablumatic
Posts: 2
Joined: Mon Nov 24, 2014 7:12 pm

Allow script to run only once in a given period of time.

Post by pablumatic »

I'm a novice in coding, but I've figured out a few simple Python scripts to keep EventGhost doing most of what I want it to do. I got stumped at my attempts in timing the running of scripts from event triggers.

What I would like to do is take this below code and make it so that once its called from an event trigger that it can only be triggered again after a specific amount of time like 60 seconds. I don't want the script to use Python's sleep function since that keeps EventGhost from doing anything else during the sleep timer period.

Code: Select all

if "string" in eg.event.payload[1]:
    eg.TriggerEvent("event")
krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

Re: Allow script to run only once in a given period of time.

Post by krambriw »

Provided you have that python script in a macro do as follows:

Put the following in the macro in this order
- the event that shall trigger
- your script
- an action from SchedulGhost to start an egg timer 60 sec
- an action that disables the macro 'itself'

Make a second macro and put in
- the event that is generated by the egg timer after 60 sec
- an action that enables the previous macro
User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: Allow script to run only once in a given period of time.

Post by Pako »

I hope I have understood correctly and that the situation in the figure corresponds to the desired situation.
DemoPablumatic.png
DemoPablumatic.png (7.8 KiB) Viewed 5476 times
Then Python Script may look like this:

Code: Select all

timeout = 60
if not "myFilterVariableString" in eg.globals.__dict__:
    eg.globals.myFilterVariableString = 0
pld = eg.event.payload
if pld and isinstance(pld, (list, tuple)) and len(pld) > 1 and "string" in pld[1]:    
    new = eg.event.time
    if new - eg.globals.myFilterVariableString > timeout:
        eg.globals.myFilterVariableString = new
        eg.TriggerEvent("event")
Pako
User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: Allow script to run only once in a given period of time.

Post by Pako »

krambriw wrote:- an action from SchedulGhost to start an egg timer 60 sec
Dear Walter,
I think it is totally unnecessary to use SchedulGhost for this purpose.
Look at EventGhost plugin and action TriggerEvent.
There you can set the desired timeout.
Otherwise, of course, your solution is correct.

Best regards,
Luboš
piert
Experienced User
Posts: 321
Joined: Tue Jun 14, 2011 2:53 pm

Re: Allow script to run only once in a given period of time.

Post by piert »

Pako, interesting script to keep everything contained within one macro.

I think that the "myFilterVariableString", since it is part of eg.globals, can only be used for one single instance, right?

In other words; if you would need to have the same function in a different macro, you would have to use a different variable in each?
User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: Allow script to run only once in a given period of time.

Post by Pako »

piert wrote:I think that the "myFilterVariableString", since it is part of eg.globals, can only be used for one single instance, right?
Correctly.

Pako
piert
Experienced User
Posts: 321
Joined: Tue Jun 14, 2011 2:53 pm

Re: Allow script to run only once in a given period of time.

Post by piert »

Would it be possible to use a non-global variable instead. And, if so, would that mean that you don't have to worry about using different variable names in each new Macro?
User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: Allow script to run only once in a given period of time.

Post by Pako »

piert wrote:Would it be possible to use a non-global variable instead.
I am afraid that it is impossible (or prohibitively complicated).
You still need to have some reference to a particular "instance" macros.

Pako
piert
Experienced User
Posts: 321
Joined: Tue Jun 14, 2011 2:53 pm

Re: Allow script to run only once in a given period of time.

Post by piert »

Thank you for the reply.
So it is important to keep track of the variables in use if one uses this piece of python in multiple instances.
Post Reply