Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Efficiency of indoor grow light timer

by holli (Monsignor)
on Nov 12, 2017 at 19:48 UTC ( #1203233=note: print w/replies, xml ) Need Help??


in reply to Efficiency of indoor grow light timer

You don't need any datetime arithmatic at all. Just loop endlessly and check once a minute wether the lamp state matches the daytime.
use feature 'signatures'; my $on = 0; sub switch_lamps ($switch_to) { state $lamp_state = 0; return if $lamp_state == $switch_to; $lamp_state = $switch_to; # ... } sub is_nighttime { #... } while (1) { switch_lamps( $on && is_nighttime() ); sleep(60); }


holli

You can lead your users to water, but alas, you cannot drown them.

Replies are listed 'Best First'.
Re^2: Efficiency of indoor grow light timer
by stevieb (Abbot) on Nov 12, 2017 at 20:03 UTC

    Thanks holli,

    I can't see how I can avoid arithmetic, but as I said, this is the second full-blown attempt at refactoring this in the last six months, so I could be blinded.

    Somehow I need to know if the on/off time is between two times. So if I am in vegetative "season", my grow lights would be on for 18 hours a day, so I'd have to figure out what time of day it is, and check if we're within that range or not.

    Am I missing something?

      No, you don't, you Stoner :-) But finding out wether a time in between a given interval or not is easy. Doing it this way saves you from having to deal with fixed switch times, which are, as you wrote, unreliable in case of outages or just the computer being shut down at the wrong moment.
      use 5.24.1; use feature qw:signatures:; no warnings qw:experimental:; my @off_from = (22, 0, 0); #22:00:00 my @off_until = (08, 0, 1); #08:00:00 sub cmpt ( $a, $b ) { for ( 0 .. 2 ){ return $a->[$_] <=> $b->[$_] if $a->[$_] != $b->[$_] ; } return 0; } sub is_nighttime( $now ) { my @now = @{$now} || (localtime(time))[2,1,0]; if ( cmpt( \@off_from, \@off_until ) == 1 ) { # over midnight return cmpt(\@now, \@off_from) == 1 || cmpt( \@now, \@off_unti +l ) == -1; } else { # all same day return cmpt(\@now, \@off_from) == 1 && cmpt( \@now, \@off_unti +l ) == -1; } return 0; }
      Edit: Removed C&P remnant
      Edit: added code
      Edit: added <=> (it's been a while)


      holli

      You can lead your users to water, but alas, you cannot drown them.

        Heh ;)

        Full disclaimer and to be fully honest, I've been growing cannabis indoors and out for 25+ years. I have an issue with "Restless Leg Syndrome", and every day, moments before I go to sleep, I vaporize about three puffs of an indica-based strain that eradicates the problem in its entirety. I very rarely smoke or use it other than for that specific purpose (there's the odd time a family member or friend may be over and I'll roll a dube or use the bong, but that's a handful of times a year).

        I'm actually an outcast in my family relative to cannabis use, as many of them use it, some even daily for recreational purposes.

        Not that I need to explain myself, but my point here is that I use it legit for medicine, have for numerous years, and feel it should have been *de-criminalized* a long, long time ago. There are as many medicinal, economical (building materials, rope, clothing etc), ecological (supplants trees; hemp is a yearly reproduceable product, trees are not) and *insert many other benefits here*.

        I just went on a mission to get Perl, my favourite programming language, to be a part of this process.

        Now I can say this is one thing that Perl has done for me (see How has Perl affected you?). It helps with a decades-old issue that I have due to a severe injury I suffered many moons ago, and periodically the odd anxiety bout.

        Stoner? Sure. But a Perl-based Stoner :)

        So after a month of testing, the compare code most definitely breaks after the clock switches past midnight. I hacked and played, but to no avail. I have reverted back to my original idea of comparing DateTime objects, that I documented in Benchmark routines earlier in the thread. Although the modules that 1nickt suggested were good ideas, they did not provide what I needed in the specific manner in which I needed them.

        So right now, I'm doing trials with this code starting at line 114 and ending at line 191. I have left the private functions immediately beneath the main method in this commit for clarity here. I've also made immortal the link by ensuring that the file is referenced to the commit itself.

        In all my testing so far, it works exactly how I need. I have two RPis running this code, both set with different times and time zones (and different "light on" times along with different "light on hours" times). We'll see what happens.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1203233]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2019-06-26 21:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Is there a future for codeless software?



    Results (111 votes). Check out past polls.

    Notices?