In a App, how to create a minimum time before allowing another action

I'm working on a app where:

Read a sensor and create an action based on the sensor reading.
After which I don't want to allow another action for some period of time ( in the order of 30 minutes).

My initial thought is to just use the Epoch time adding a 30 minute offset then testing if 30 minutes has gone by before enabling another action .

Is there a simpler method?

Thanks
John

Depending on your code structure, you'd probably want to use the runIn method.

I though of runIn but I don't want anything to necessarily run, but not run until. I guess I could have a runIn method set a simple flag.

I saw this in the undocumented "Common Methods" but don't know how it works.

  • Long timeOffset(Number minutes) (Common method since 2.1.0)

You just need to run unscheduled() if you have an event come in and want to stop the runIn from running.

Check lines 106-131:

Thanks, couldn't see where the schedule was setup. However since you mentioned it previously the "runIn" method is the cleanest.

runIn is just a method for setting a scheduled job to run whatever handler you pass to it later on. When it sets the job, you can see it either from the app stats page (cog icon for the app) or from the scheduled jobs tab in logs.

Depending on what triggers the reading of sensor, you can save last reading time in a state, then when next reading comes (assuming this reading is based on an event, and not on a fixed schedule) then check the time against the state, and only take action if the last reading was over 30 minutes ago. You don't need to set a schedule for that.

state.lastRun = now()

1 Like