rules. how to toggle gpio every 3 hours?

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
diablosss
Normal user
Posts: 11
Joined: 26 Jun 2021, 15:36

rules. how to toggle gpio every 3 hours?

#1 Post by diablosss » 17 Jan 2022, 08:26

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 :)

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

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

#2 Post by Ath » 17 Jan 2022, 09:16

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
/Ton (PayPal.me)

diablosss
Normal user
Posts: 11
Joined: 26 Jun 2021, 15:36

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

#3 Post by diablosss » 17 Jan 2022, 10:41

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 👍🙏

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

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

#4 Post by Ath » 17 Jan 2022, 11:14

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.
/Ton (PayPal.me)

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

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

#5 Post by TD-er » 17 Jan 2022, 11:20

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.

Post Reply

Who is online

Users browsing this forum: No registered users and 18 guests