Page 1 of 1

rules. how to toggle gpio every 3 hours?

Posted: 17 Jan 2022, 08:26
by diablosss
Hello. I want gpio 4 switch on (for 50sec and turn off ) every 3 hours.
I wrote code in rules. All work perfect. But my code is too big and eat much memory of rules :( im sure must be shorter code to do same actions.
This is my code example

Code: Select all

On Clock#Time=All, 00:01 do
  gpio,4,0
  timerSet,1,50
endon

On Clock#Time=All, 03:01 do
  gpio,5,0
  timerSet,1,50
endon

Etc etc etc many times till 
On Clock#Time=All, 21:01 do
  gpio,4,0
  timerSet,1,50
endon

on Rules#Timer=1 do
  gpio,4,1
endon
Help please :)

Re: rules. how to toggle gpio every 3 hours?

Posted: 17 Jan 2022, 09:16
by Ath
Your 3 hour timer could be set using a LoopTimerSet command, unless you need to have it run at an exact minute after the hour, then the "on Clock#Time do" is the most reliable solution.

Code: Select all

On System#Boot do
  LoopTimerSet,2,10800 // 60*60*3
endon

On System#Timer=2 do
  gpio,4,0 // or gpio,5,0?
  TimerSet,1,50
endon

On System#Timer=1 do
  gpio,4,1 // or gpio,5,1?
endon
When using an exact "n minute after the hour" schedule, you could do like this (instead of "On System#Timer=2 do")

Code: Select all

On Clock#Time=All,**:01 do // check once per hour, at 1 minute after
  if %syshour%=0 or %syshour%=3 or %syshour%=6 or %syshour%=9 // split in 2 to avoid calculation issues
    gpio,4,0
    TimerSet,1,50
  else
    if %syshour%=12 or %syshour%=15 or %syshour%=18 or %syshour%=21
      gpio,4,0
      TimerSet,1,50
    endif
  endif
endon

Re: rules. how to toggle gpio every 3 hours?

Posted: 17 Jan 2022, 10:41
by diablosss
Ath wrote: 17 Jan 2022, 09:16

When using an exact "n minute after the hour" schedule, you could do like this (instead of "On System#Timer=2 do")

Code: Select all

On Clock#Time=All,**:01 do // check once per hour, at 1 minute after
  if %syshour%=0 or %syshour%=3 or %syshour%=6 or %syshour%=9 // split in 2 to avoid calculation issues
    gpio,4,0
    TimerSet,1,50
  else
    if %syshour%=12 or %syshour%=15 or %syshour%=18 or %syshour%=21
      gpio,4,0
      TimerSet,1,50
    endif
  endif
endon
Thank you very much. Exact same what i need 👍🙏

Re: rules. how to toggle gpio every 3 hours?

Posted: 17 Jan 2022, 11:14
by Ath
Great, you're welcome, thanks for the reply :)

For rules in general it is advised to have the most often executed rule(s) (the event "clock#time" is executed every minute...) at the top of the rules1 file, and the least often used, like "system#boot", can be near the end of the file. The limit stated on the page had to do with the web-editor not handling 'largish' files nicely, but that has been resolved, so that doesn't matter much anymore.
You should not keep a bunch of no longer needed event handlers in these files, as that will slow down processing.

When on Windows: There is a rules' User Defined Language configuration for Notepad++ available in the Github repository, to make editing a bit more comfortable compared to using the web editor.

Re: rules. how to toggle gpio every 3 hours?

Posted: 17 Jan 2022, 11:20
by TD-er
When editing large rules files, it is however advised to edit them in another editor, preferably Notepad++ as it has the option to color highlight based on the syntax as Ath also suggested.
It might happen that saving the rules file via the editor fails without you noticing it, when saving large rules files.
By editing in the external notepad and then copy/paste it, you don't loose your edits. (or part of the rules file)
This rarely happens, but when it happens, it is rather annoying.