Every 30 mins between 7am and sunset


#1

Hi All!
I would like a piston to run every 30 mins, from 7am until sunset, and run at :00 and :30 hours.
I got this accomplished 3 different ways and it works fine, but I think that there might be a more elegant way of doing it than 3 nested conditions… Any thoughts?


#2

Sounds similar to what is being done on this thread…


#3

I see. The “only at :00 or :30” only appears as an option if the measure for every x is set to seconds. I had it as “every 30 minutes” and so that was not an option.

That said, I don’t quite follow the logic of “every 60 seconds but only at :00 or :30”. There is nothing that would happen every 60 seconds, or any other second frequency, only twice an hour?


#4

Would something like this work? The trigger would be the 7:00AM, the issue with this is if for some reason the 7:00 am fails to trigger, then the rest fails.

Edit: If something needs to happen at 7:00, you might have to adjust the trigger time to 6:30?


#5

I know I’m a little late but I use this formula in my every statement:

(Interval-($minute+offset-Interval*floor(($minute+offset)/Interval)))*60-$second+5

Offset = offset from end of interval period for minute to run = (interval-minute)

So to run every 30 minutes at 0 and 30 minutes past the hour, offset is zero and you have:

(30-($minute-30*floor($minute/30)))*60-$second+5

NOTE: The +5 ensures with the variation of execution, you will always be within the right minute.

This formula will always execute you piston at the desired minute within the hour no matter when you start execution.

For you specific example, I would suggest something like:

every  (30-($minute-30*floor($minute/30)))*60-$second+5 seconds
  if time is between 7 am and sunset then
     do stuff ...
  end if
end every

This will run every 30 minutes but only do what you want within your limits.

Enjoy!


#6

Thanks! This took me a minute to follow, but it makes sense now.

On a related note, how would I write something like this in an expression:
‘if($now > 7:00 && $now < addMinutes($sunset, -60) ? “do this” : “do that”’

Thank you!


#7

Answered my own question. Landed like this:

(((time($now)/3600000 > 7.00) &&
(time($now)/3600000 < time(addMinutes($sunset, -60))/3600000))
? ‘a’ : ‘b’


#8

I ended up using this solution for this particular scenario:


#9

There is still something wrong with my piston. This is what I have:

It runs every 30 mins, not just between 7am and 70 mins before sunset. The piston state is updated every half hour (that is my test). I would have expected the last update to be at 5pm.

Sunset here is 6:28, so 70 mins before is 5:18, and since this should only run at :00 and :30, the update should have happened at 5pm. Then not run again until 7am.


#10

Timer blocks (every) operate outside the normal flow of pistons. When they are fired just the every block gets executed.


#11

So basically restrictions don’t work with “every”, correct? (I can confirm: i replaced the piston state line above with a send SMS, and I am getting a text every half hour, all night).

I have to find another way to do this then, I don’t want this piston to execute outside of those hours.
Any better solutions to achieve this?


#12

Maybe drop the restriction, and check the time in an if statement after the every. I’ll try that tomorrow.


#13

Actually, this is super weird. Last night I got the last text at 22:00 and haven’t gotten any yet this morning (it is 9:30). It is almost if the timezone randomly shifted by 5 hours? I’m waiting to see if it restarts at noon. Strange.


#14

Correct, The ‘every’ works independently of everything else and will always execute what is inside. Rather than using a restriction I suggest using an 'if which will be more straightforward.

every 60 seconds, but only at :00 or :30 minutes
   if time is between 7 am and 70 minutes to sunset
     set piston state
   end if
end every

So the piston will run every 30 minutes 24 hours but your desired actions will only occur in the limited timeframe.